blob: a6a3b67785c1e0d9aff3e2eebcc78381812c2fe2 [file] [log] [blame]
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +01001/*
Gian Marco Iodiceb87b95e2019-01-21 17:14:31 +00002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +01003 *
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_GEMM_FIXTURE
25#define ARM_COMPUTE_TEST_GEMM_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/GEMM.h"
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
Pablo Tello0e37b5c2018-10-30 11:18:37 +000045template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool disable_c = false, bool reinterpret_input_as_3d = false, bool reinterpret_ouput_as_3d = false>
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010046class GEMMValidationFixture : public framework::Fixture
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010047{
48public:
49 template <typename...>
Pablo Tello0e37b5c2018-10-30 11:18:37 +000050 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, bool pretranspose, DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010051 {
Pablo Tello0e37b5c2018-10-30 11:18:37 +000052 _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, pretranspose, data_type);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010053 _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010054 }
55
56protected:
57 template <typename U>
Pablo Tello0e37b5c2018-10-30 11:18:37 +000058 void fill(U &&tensor, int i, float lo = -1.f, float hi = 1.f)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010059 {
60 switch(tensor.data_type())
61 {
62 case DataType::F16:
63 case DataType::F32:
64 {
Pablo Tello0e37b5c2018-10-30 11:18:37 +000065 std::uniform_real_distribution<> distribution(lo, hi);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010066 library->fill(tensor, distribution, i);
67 break;
68 }
69 default:
70 library->fill_tensor_uniform(tensor, i);
71 }
72 }
73
74 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta,
Pablo Tello0e37b5c2018-10-30 11:18:37 +000075 bool pretranspose, DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010076 {
77 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010078 TensorType a = create_tensor<TensorType>(shape_a, data_type, 1);
79 TensorType b = create_tensor<TensorType>(shape_b, data_type, 1);
80 TensorType c = create_tensor<TensorType>(shape_c, data_type, 1);
81 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010082
83 // Create and configure function
84 FunctionType gemm;
Isabella Gottardi8e74f442018-03-01 16:42:00 +000085 // The GEMMinfo includes the values of the depth in case of reinterpreted 3d output.
Gian Marco Iodice3139f032018-11-05 14:26:32 +000086 // If the output shape has the same number of dimensions of the input the method called is a 2D matrix multiplication (depth_output_reinterpreted_as_3D = 0),
Isabella Gottardi8e74f442018-03-01 16:42:00 +000087 // in the other case we have to use the reinterpreted version of GEMM (depth_output_reinterpreted_as_3D = depth of the 3D output).
Pablo Tello0e37b5c2018-10-30 11:18:37 +000088 gemm.configure(&a, &b, (disable_c) ? nullptr : &c, &dst, alpha, beta, GEMMInfo(false, false, false, (reinterpret_ouput_as_3d ? output_shape[2] : 0), reinterpret_input_as_3d));
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010089 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
90 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
91 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 a.allocator()->allocate();
96 b.allocator()->allocate();
97 c.allocator()->allocate();
98 dst.allocator()->allocate();
99
100 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
104
105 // Fill tensors
106 fill(AccessorType(a), 0);
107 fill(AccessorType(b), 1);
Pablo Tello0e37b5c2018-10-30 11:18:37 +0000108 if(!disable_c)
109 {
110 fill(AccessorType(c), 2);
111 }
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100112
113 // Compute GEMM function
114 gemm.run();
115
116 return dst;
117 }
118
119 SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100120 DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100121 {
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100122 TensorShape shape_a_to_use = shape_a;
123 if(reinterpret_input_as_3d)
124 {
125 // Collapse the second and third dimension if the input is 3D
126 shape_a_to_use.collapse(2U, 1U);
127 }
128
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100129 // Create reference
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100130 SimpleTensor<T> a{ shape_a_to_use, data_type, 1 };
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100131 SimpleTensor<T> b{ shape_b, data_type, 1 };
132 SimpleTensor<T> c{ shape_c, data_type, 1 };
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100133
134 // Fill reference
135 fill(a, 0);
136 fill(b, 1);
Pablo Tello0e37b5c2018-10-30 11:18:37 +0000137 if(!disable_c)
138 {
139 fill(c, 2);
140 return reference::gemm<T>(a, b, c, alpha, beta);
141 }
142 else
143 {
144 // Setting beta to 0 will effectively disable C for the
145 // computation of the reference: alpha * A * B + 0 * C
146 return reference::gemm<T>(a, b, c, alpha, 0.f);
147 }
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100148 }
149
150 TensorType _target{};
151 SimpleTensor<T> _reference{};
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100152};
153
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000154template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000155class GEMMMatrixMultiplyReshapedValidationFixture : public framework::Fixture
156{
157public:
158 template <typename...>
159 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0, bool interleave_lhs,
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000160 bool interleave_rhs, DataType data_type, float alpha)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000161 {
162 GEMMLHSMatrixInfo lhs_info;
163 lhs_info.m0 = m0;
164 lhs_info.k0 = k0;
165 lhs_info.v0 = v0;
166 lhs_info.interleave = interleave_lhs;
167 lhs_info.transpose = false;
168
169 GEMMRHSMatrixInfo rhs_info;
170 rhs_info.n0 = n0;
171 rhs_info.k0 = k0;
172 rhs_info.h0 = h0;
173 rhs_info.interleave = interleave_rhs;
174 rhs_info.transpose = true;
175
176 // Set the tensor shapes for LHS and RHS matrices
177 const TensorShape lhs_shape(k, m, batch_size);
178 const TensorShape rhs_shape(n, k, batch_size);
179
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000180 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha);
181 _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000182 }
183
184protected:
185 template <typename U>
186 void fill(U &&tensor, int i)
187 {
188 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
189 library->fill(tensor, distribution, i);
Gian Marco Iodiceb87b95e2019-01-21 17:14:31 +0000190
191 // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0)
192 std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
193 library->fill_borders_with_garbage(tensor, distribution_inf, i);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000194 }
195
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000196 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000197 {
198 // Create tensors
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000199 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
200 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000201 TensorType lhs_reshaped;
202 TensorType rhs_reshaped;
203 TensorType dst;
204
205 const unsigned int M = lhs_shape[1];
206 const unsigned int N = rhs_shape[0];
207 const unsigned int K = lhs_shape[0];
208
209 // The output tensor will be auto-initialized within the function
210
211 // Create and configure function
212 ReshapeLHSFunctionType reshape_lhs;
213 ReshapeRHSFunctionType reshape_rhs;
214 GEMMFunctionType gemm;
215 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
216 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000217 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000218
219 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
220 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
221
222 // Allocate tensors
223 lhs.allocator()->allocate();
224 rhs.allocator()->allocate();
225 lhs_reshaped.allocator()->allocate();
226 rhs_reshaped.allocator()->allocate();
227 dst.allocator()->allocate();
228
229 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
230 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
231 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
232 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
233 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
234
235 // Fill tensors
236 fill(AccessorType(lhs), 0);
237 fill(AccessorType(rhs), 1);
238
239 // Compute GEMM
240 reshape_lhs.run();
241 reshape_rhs.run();
242 gemm.run();
243
244 return dst;
245 }
246
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000247 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000248 {
249 TensorShape dst_shape = lhs_shape;
250 dst_shape[0] = rhs_shape[0];
251 dst_shape[1] = lhs_shape[1];
252
253 // Create reference
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000254 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
255 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
256 SimpleTensor<T> c{ dst_shape, data_type, 1 };
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000257
258 // Fill reference
259 fill(lhs, 0);
260 fill(rhs, 1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000261
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000262 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000263 }
264
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000265 TensorType _target{};
266 SimpleTensor<T> _reference{};
267};
268
269template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
270class GEMMMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture
271{
272public:
273 template <typename...>
274 void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0,
275 bool interleave_lhs,
276 bool interleave_rhs, DataType data_type, float alpha)
277 {
278 GEMMLHSMatrixInfo lhs_info;
279 lhs_info.m0 = m0;
280 lhs_info.k0 = k0;
281 lhs_info.v0 = v0;
282 lhs_info.interleave = interleave_lhs;
283 lhs_info.transpose = false;
284
285 GEMMRHSMatrixInfo rhs_info;
286 rhs_info.n0 = n0;
287 rhs_info.k0 = k0;
288 rhs_info.h0 = h0;
289 rhs_info.interleave = interleave_rhs;
290 rhs_info.transpose = true;
291
292 // In case of GEMM3D, m is the product between m_w and m_h
293 const unsigned int m = m_w * m_h;
294
295 // Set the tensor shapes for LHS and RHS matrices
296 const TensorShape lhs_shape(k, m, batch_size);
297 const TensorShape rhs_shape(n, k, batch_size);
298
299 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h);
300 _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h);
301 }
302
303protected:
304 template <typename U>
305 void fill(U &&tensor, int i)
306 {
307 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
308 library->fill(tensor, distribution, i);
309 }
310
311 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha,
312 unsigned int m_h)
313 {
314 // Create tensors
315 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
316 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
317 TensorType lhs_reshaped;
318 TensorType rhs_reshaped;
319 TensorType dst;
320
321 const unsigned int M = lhs_shape[1];
322 const unsigned int N = rhs_shape[0];
323 const unsigned int K = lhs_shape[0];
324
325 // The output tensor will be auto-initialized within the function
326
327 // Create and configure function
328 ReshapeLHSFunctionType reshape_lhs;
329 ReshapeRHSFunctionType reshape_rhs;
330 GEMMFunctionType gemm;
331 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
332 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
333 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
334
335 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
336 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
337
338 // Allocate tensors
339 lhs.allocator()->allocate();
340 rhs.allocator()->allocate();
341 lhs_reshaped.allocator()->allocate();
342 rhs_reshaped.allocator()->allocate();
343 dst.allocator()->allocate();
344
345 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
346 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
347 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
348 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
349 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
350
351 // Fill tensors
352 fill(AccessorType(lhs), 0);
353 fill(AccessorType(rhs), 1);
354
355 // Compute GEMM
356 reshape_lhs.run();
357 reshape_rhs.run();
358 gemm.run();
359
360 return dst;
361 }
362
363 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h)
364 {
365 TensorShape dst_shape = lhs_shape;
366 dst_shape.set(0, rhs_shape[0]);
367 dst_shape.set(1, lhs_shape[1] / m_h);
368 dst_shape.set(2, m_h);
369 dst_shape.set(3, lhs_shape[2]);
370
371 // Create reference
372 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
373 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
374 SimpleTensor<T> c{ dst_shape, data_type, 1 };
375
376 // Fill reference
377 fill(lhs, 0);
378 fill(rhs, 1);
379
380 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
381 }
382
383 TensorType _target{};
384 SimpleTensor<T> _reference{};
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000385};
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100386} // namespace validation
387} // namespace test
388} // namespace arm_compute
389#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */