blob: 34f9bd848cf1b3133066f1c77851d3ffba45135c [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};
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000386
387template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
388class GEMMMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture
389{
390public:
391 template <typename...>
392 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 h0,
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100393 bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha, float beta, bool broadcast_bias)
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000394 {
395 GEMMLHSMatrixInfo lhs_info;
396 lhs_info.m0 = m0;
397 lhs_info.k0 = k0;
398
399 GEMMRHSMatrixInfo rhs_info;
400 rhs_info.n0 = n0;
401 rhs_info.k0 = k0;
402 rhs_info.h0 = h0;
403 rhs_info.interleave = interleave_rhs;
404 rhs_info.transpose = transpose_rhs;
405
406 // Set the tensor shapes for LHS and RHS matrices
407 const TensorShape lhs_shape(k, m, batch_size);
408 const TensorShape rhs_shape(n, k, batch_size);
409
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100410 TensorShape bias_shape;
411 if(broadcast_bias)
412 {
413 bias_shape = TensorShape(n, 1, 1);
414 }
415 else
416 {
417 bias_shape = TensorShape(n, m, batch_size);
418 }
419
420 _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias);
421 _reference = compute_reference(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, broadcast_bias);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000422 }
423
424protected:
425 template <typename U>
426 void fill(U &&tensor, int i)
427 {
428 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
429 library->fill(tensor, distribution, i);
430
431 // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0)
432 std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
433 library->fill_borders_with_garbage(tensor, distribution_inf, i);
434 }
435
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100436 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info,
437 DataType data_type, float alpha, float beta, bool broadcast_bias)
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000438 {
439 // Create tensors
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100440 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
441 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
442 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000443 TensorType rhs_reshaped;
444 TensorType dst;
445
446 const unsigned int M = lhs_shape[1];
447 const unsigned int N = rhs_shape[0];
448 const unsigned int K = lhs_shape[0];
449
450 // The output tensor will be auto-initialized within the function
451
452 // Create and configure function
453 ReshapeRHSFunctionType reshape_rhs;
454 GEMMFunctionType gemm;
455 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100456 gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, 0, false, broadcast_bias));
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000457
458 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
459 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
460
461 // Allocate tensors
462 lhs.allocator()->allocate();
463 rhs.allocator()->allocate();
464 rhs_reshaped.allocator()->allocate();
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100465 bias.allocator()->allocate();
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000466 dst.allocator()->allocate();
467
468 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
469 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
470 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
471 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
472
473 // Fill tensors
474 fill(AccessorType(lhs), 0);
475 fill(AccessorType(rhs), 1);
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100476 fill(AccessorType(bias), 2);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000477
478 // Compute GEMM
479 reshape_rhs.run();
480 gemm.run();
481
482 return dst;
483 }
484
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100485 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, bool broadcast_bias)
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000486 {
487 TensorShape dst_shape = lhs_shape;
488 dst_shape[0] = rhs_shape[0];
489 dst_shape[1] = lhs_shape[1];
490
491 // Create reference
492 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
493 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100494 SimpleTensor<T> bias{ dst_shape, data_type, 1 };
495
496 const int n = rhs_shape[0];
497 const int m = lhs_shape[1];
498 const int batch_size = lhs_shape[2];
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000499
500 // Fill reference
501 fill(lhs, 0);
502 fill(rhs, 1);
503
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100504 if(broadcast_bias)
505 {
506 SimpleTensor<T> tmp{ bias_shape, data_type, 1 };
507 fill(tmp, 2);
508 for(int i = 0; i < m * batch_size; i++)
509 {
510 memcpy(bias.data() + i * n, tmp.data(), n * sizeof(T));
511 }
512 }
513 else
514 {
515 fill(bias, 2);
516 }
517
518 return (reference::gemm<T>(lhs, rhs, bias, alpha, beta));
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000519 }
520
521 TensorType _target{};
522 SimpleTensor<T> _reference{};
523};
524
giuros01b3204e72019-04-01 13:50:22 +0100525template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType>
526class GEMMMatrixMultiplyNativeValidationFixture : public framework::Fixture
527{
528public:
529 template <typename...>
530 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, DataType data_type, float alpha)
531 {
532 GEMMLHSMatrixInfo lhs_info;
533 lhs_info.m0 = m0;
534 lhs_info.k0 = k0;
535
536 GEMMRHSMatrixInfo rhs_info;
537 rhs_info.n0 = n0;
538 rhs_info.k0 = k0;
539
540 // Set the tensor shapes for LHS and RHS matrices
541 const TensorShape lhs_shape(k, m, batch_size);
542 const TensorShape rhs_shape(n, k, batch_size);
543
544 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha);
545 _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha);
546 }
547
548protected:
549 template <typename U>
550 void fill(U &&tensor, int i)
551 {
552 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
553 library->fill(tensor, distribution, i);
554
555 // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0)
556 std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
557 library->fill_borders_with_garbage(tensor, distribution_inf, i);
558 }
559
560 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha)
561 {
562 // Create tensors
563 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
564 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
565 TensorType dst;
566
567 const unsigned int M = lhs_shape[1];
568 const unsigned int N = rhs_shape[0];
569 const unsigned int K = lhs_shape[0];
570
571 // Create and configure function
572 GEMMFunctionType gemm;
573 gemm.configure(&lhs, &rhs, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
574
575 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
576 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
577
578 // Allocate tensors
579 lhs.allocator()->allocate();
580 rhs.allocator()->allocate();
581 dst.allocator()->allocate();
582
583 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
584 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
585 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
586
587 // Fill tensors
588 fill(AccessorType(lhs), 0);
589 fill(AccessorType(rhs), 1);
590
591 // Compute GEMM
592 gemm.run();
593
594 return dst;
595 }
596
597 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha)
598 {
599 TensorShape dst_shape = lhs_shape;
600 dst_shape[0] = rhs_shape[0];
601 dst_shape[1] = lhs_shape[1];
602
603 // Create reference
604 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
605 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
606 SimpleTensor<T> c{ dst_shape, data_type, 1 };
607
608 // Fill reference
609 fill(lhs, 0);
610 fill(rhs, 1);
611
612 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
613 }
614
615 TensorType _target{};
616 SimpleTensor<T> _reference{};
617};
618
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000619template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
620class GEMMMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture
621{
622public:
623 template <typename...>
624 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 h0,
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100625 bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha, float beta)
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000626 {
627 GEMMLHSMatrixInfo lhs_info;
628 lhs_info.m0 = m0;
629 lhs_info.k0 = k0;
630
631 GEMMRHSMatrixInfo rhs_info;
632 rhs_info.n0 = n0;
633 rhs_info.k0 = k0;
634 rhs_info.h0 = h0;
635 rhs_info.interleave = interleave_rhs;
636 rhs_info.transpose = transpose_rhs;
637
638 // In case of GEMM3D, m is the product between m_w and m_h
639 const unsigned int m = m_w * m_h;
640
641 // Set the tensor shapes for LHS and RHS matrices
642 const TensorShape lhs_shape(k, m, batch_size);
643 const TensorShape rhs_shape(n, k, batch_size);
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100644 const TensorShape bias_shape(n, 1, 1);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000645
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100646 _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h);
647 _reference = compute_reference(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, m_h);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000648 }
649
650protected:
651 template <typename U>
652 void fill(U &&tensor, int i)
653 {
654 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
655 library->fill(tensor, distribution, i);
656 }
657
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100658 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info,
659 DataType data_type, float alpha, float beta,
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000660 unsigned int m_h)
661 {
662 // Create tensors
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100663 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
664 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
665 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000666 TensorType rhs_reshaped;
667 TensorType dst;
668
669 const unsigned int M = lhs_shape[1];
670 const unsigned int N = rhs_shape[0];
671 const unsigned int K = lhs_shape[0];
672
673 // The output tensor will be auto-initialized within the function
674
675 // Create and configure function
676 ReshapeRHSFunctionType reshape_rhs;
677 GEMMFunctionType gemm;
678 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100679 gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h, false, true));
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000680
681 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
682 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
683
684 // Allocate tensors
685 lhs.allocator()->allocate();
686 rhs.allocator()->allocate();
687 rhs_reshaped.allocator()->allocate();
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100688 bias.allocator()->allocate();
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000689 dst.allocator()->allocate();
690
691 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
692 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
693 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
694 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
695
696 // Fill tensors
697 fill(AccessorType(lhs), 0);
698 fill(AccessorType(rhs), 1);
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100699 fill(AccessorType(bias), 2);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000700
701 // Compute GEMM
702 reshape_rhs.run();
703 gemm.run();
704
705 return dst;
706 }
707
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100708 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, unsigned int m_h)
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000709 {
710 TensorShape dst_shape = lhs_shape;
711 dst_shape.set(0, rhs_shape[0]);
712 dst_shape.set(1, lhs_shape[1] / m_h);
713 dst_shape.set(2, m_h);
714 dst_shape.set(3, lhs_shape[2]);
715
716 // Create reference
717 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
718 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100719 SimpleTensor<T> bias{ dst_shape, data_type, 1 };
720
721 const int n = rhs_shape[0];
722 const int m = lhs_shape[1];
723 const int batch_size = lhs_shape[2];
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000724
725 // Fill reference
726 fill(lhs, 0);
727 fill(rhs, 1);
728
Georgios Pinitasb0f342e2019-05-21 13:32:43 +0100729 SimpleTensor<T> tmp{ bias_shape, data_type, 1 };
730 fill(tmp, 2);
731 for(int i = 0; i < m * batch_size; i++)
732 {
733 memcpy(bias.data() + i * n, tmp.data(), n * sizeof(T));
734 }
735
736 return reference::gemm<T>(lhs, rhs, bias, alpha, beta);
Gian Marco Iodiceadc53952019-02-15 11:10:31 +0000737 }
738
739 TensorType _target{};
740 SimpleTensor<T> _reference{};
741};
giuros01b3204e72019-04-01 13:50:22 +0100742
743template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType>
744class GEMMMatrixMultiplyNative3DValidationFixture : public framework::Fixture
745{
746public:
747 template <typename...>
748 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, DataType data_type, float alpha)
749 {
750 GEMMLHSMatrixInfo lhs_info;
751 lhs_info.m0 = m0;
752 lhs_info.k0 = k0;
753
754 GEMMRHSMatrixInfo rhs_info;
755 rhs_info.n0 = n0;
756 rhs_info.k0 = k0;
757
758 // In case of GEMM3D, m is the product between m_w and m_h
759 const unsigned int m = m_w * m_h;
760
761 // Set the tensor shapes for LHS and RHS matrices
762 const TensorShape lhs_shape(k, m, batch_size);
763 const TensorShape rhs_shape(n, k, batch_size);
764
765 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h);
766 _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h);
767 }
768
769protected:
770 template <typename U>
771 void fill(U &&tensor, int i)
772 {
773 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
774 library->fill(tensor, distribution, i);
775 }
776
777 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha,
778 unsigned int m_h)
779 {
780 // Create tensors
781 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
782 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
783 TensorType rhs_reshaped;
784 TensorType dst;
785
786 const unsigned int M = lhs_shape[1];
787 const unsigned int N = rhs_shape[0];
788 const unsigned int K = lhs_shape[0];
789
790 // The output tensor will be auto-initialized within the function
791
792 // Create and configure function
793 GEMMFunctionType gemm;
794 gemm.configure(&lhs, &rhs, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
795
796 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
797 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
798
799 // Allocate tensors
800 lhs.allocator()->allocate();
801 rhs.allocator()->allocate();
802 rhs_reshaped.allocator()->allocate();
803 dst.allocator()->allocate();
804
805 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
806 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
807 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
808 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
809
810 // Fill tensors
811 fill(AccessorType(lhs), 0);
812 fill(AccessorType(rhs), 1);
813
814 // Compute GEMM
815 gemm.run();
816
817 return dst;
818 }
819
820 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h)
821 {
822 TensorShape dst_shape = lhs_shape;
823 dst_shape.set(0, rhs_shape[0]);
824 dst_shape.set(1, lhs_shape[1] / m_h);
825 dst_shape.set(2, m_h);
826 dst_shape.set(3, lhs_shape[2]);
827
828 // Create reference
829 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
830 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
831 SimpleTensor<T> c{ dst_shape, data_type, 1 };
832
833 // Fill reference
834 fill(lhs, 0);
835 fill(rhs, 1);
836
837 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
838 }
839
840 TensorType _target{};
841 SimpleTensor<T> _reference{};
842};
843
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100844} // namespace validation
845} // namespace test
846} // namespace arm_compute
847#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */