blob: 24c9d96611e3df1f412a1a85fc7fb3d2a9a50957 [file] [log] [blame]
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +01001/*
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002 * Copyright (c) 2017-2018 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);
190 }
191
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000192 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 +0000193 {
194 // Create tensors
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000195 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
196 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000197 TensorType lhs_reshaped;
198 TensorType rhs_reshaped;
199 TensorType dst;
200
201 const unsigned int M = lhs_shape[1];
202 const unsigned int N = rhs_shape[0];
203 const unsigned int K = lhs_shape[0];
204
205 // The output tensor will be auto-initialized within the function
206
207 // Create and configure function
208 ReshapeLHSFunctionType reshape_lhs;
209 ReshapeRHSFunctionType reshape_rhs;
210 GEMMFunctionType gemm;
211 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
212 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000213 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000214
215 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
216 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
217
218 // Allocate tensors
219 lhs.allocator()->allocate();
220 rhs.allocator()->allocate();
221 lhs_reshaped.allocator()->allocate();
222 rhs_reshaped.allocator()->allocate();
223 dst.allocator()->allocate();
224
225 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
226 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
227 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
228 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
229 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
230
231 // Fill tensors
232 fill(AccessorType(lhs), 0);
233 fill(AccessorType(rhs), 1);
234
235 // Compute GEMM
236 reshape_lhs.run();
237 reshape_rhs.run();
238 gemm.run();
239
240 return dst;
241 }
242
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000243 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 +0000244 {
245 TensorShape dst_shape = lhs_shape;
246 dst_shape[0] = rhs_shape[0];
247 dst_shape[1] = lhs_shape[1];
248
249 // Create reference
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000250 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
251 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
252 SimpleTensor<T> c{ dst_shape, data_type, 1 };
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000253
254 // Fill reference
255 fill(lhs, 0);
256 fill(rhs, 1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000257
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000258 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000259 }
260
Gian Marco Iodice9382ab32018-12-17 15:12:07 +0000261 TensorType _target{};
262 SimpleTensor<T> _reference{};
263};
264
265template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
266class GEMMMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture
267{
268public:
269 template <typename...>
270 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,
271 bool interleave_lhs,
272 bool interleave_rhs, DataType data_type, float alpha)
273 {
274 GEMMLHSMatrixInfo lhs_info;
275 lhs_info.m0 = m0;
276 lhs_info.k0 = k0;
277 lhs_info.v0 = v0;
278 lhs_info.interleave = interleave_lhs;
279 lhs_info.transpose = false;
280
281 GEMMRHSMatrixInfo rhs_info;
282 rhs_info.n0 = n0;
283 rhs_info.k0 = k0;
284 rhs_info.h0 = h0;
285 rhs_info.interleave = interleave_rhs;
286 rhs_info.transpose = true;
287
288 // In case of GEMM3D, m is the product between m_w and m_h
289 const unsigned int m = m_w * m_h;
290
291 // Set the tensor shapes for LHS and RHS matrices
292 const TensorShape lhs_shape(k, m, batch_size);
293 const TensorShape rhs_shape(n, k, batch_size);
294
295 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h);
296 _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h);
297 }
298
299protected:
300 template <typename U>
301 void fill(U &&tensor, int i)
302 {
303 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
304 library->fill(tensor, distribution, i);
305 }
306
307 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha,
308 unsigned int m_h)
309 {
310 // Create tensors
311 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
312 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
313 TensorType lhs_reshaped;
314 TensorType rhs_reshaped;
315 TensorType dst;
316
317 const unsigned int M = lhs_shape[1];
318 const unsigned int N = rhs_shape[0];
319 const unsigned int K = lhs_shape[0];
320
321 // The output tensor will be auto-initialized within the function
322
323 // Create and configure function
324 ReshapeLHSFunctionType reshape_lhs;
325 ReshapeRHSFunctionType reshape_rhs;
326 GEMMFunctionType gemm;
327 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
328 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
329 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
330
331 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
332 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
333
334 // Allocate tensors
335 lhs.allocator()->allocate();
336 rhs.allocator()->allocate();
337 lhs_reshaped.allocator()->allocate();
338 rhs_reshaped.allocator()->allocate();
339 dst.allocator()->allocate();
340
341 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
342 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
343 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
344 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
345 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
346
347 // Fill tensors
348 fill(AccessorType(lhs), 0);
349 fill(AccessorType(rhs), 1);
350
351 // Compute GEMM
352 reshape_lhs.run();
353 reshape_rhs.run();
354 gemm.run();
355
356 return dst;
357 }
358
359 SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h)
360 {
361 TensorShape dst_shape = lhs_shape;
362 dst_shape.set(0, rhs_shape[0]);
363 dst_shape.set(1, lhs_shape[1] / m_h);
364 dst_shape.set(2, m_h);
365 dst_shape.set(3, lhs_shape[2]);
366
367 // Create reference
368 SimpleTensor<T> lhs{ lhs_shape, data_type, 1 };
369 SimpleTensor<T> rhs{ rhs_shape, data_type, 1 };
370 SimpleTensor<T> c{ dst_shape, data_type, 1 };
371
372 // Fill reference
373 fill(lhs, 0);
374 fill(rhs, 1);
375
376 return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f);
377 }
378
379 TensorType _target{};
380 SimpleTensor<T> _reference{};
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000381};
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100382} // namespace validation
383} // namespace test
384} // namespace arm_compute
385#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */