blob: 255b12c0edf1e819faf0a47665bfabcd58e37a17 [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{
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010045template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool reinterpret_input_as_3d = false, bool reinterpret_ouput_as_3d = false>
46class GEMMValidationFixture : public framework::Fixture
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010047{
48public:
49 template <typename...>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010051 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010052 _data_type = data_type;
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010053
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010054 _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type);
55 _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010056 }
57
58protected:
59 template <typename U>
60 void fill(U &&tensor, int i)
61 {
62 switch(tensor.data_type())
63 {
64 case DataType::F16:
65 case DataType::F32:
66 {
67 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
68 library->fill(tensor, distribution, i);
69 break;
70 }
71 default:
72 library->fill_tensor_uniform(tensor, i);
73 }
74 }
75
76 TensorType compute_target(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 +010077 DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010078 {
79 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010080 TensorType a = create_tensor<TensorType>(shape_a, data_type, 1);
81 TensorType b = create_tensor<TensorType>(shape_b, data_type, 1);
82 TensorType c = create_tensor<TensorType>(shape_c, data_type, 1);
83 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010084
85 // Create and configure function
86 FunctionType gemm;
Isabella Gottardi8e74f442018-03-01 16:42:00 +000087 // The GEMMinfo includes the values of the depth in case of reinterpreted 3d output.
88 // 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 = 1),
89 // in the other case we have to use the reinterpreted version of GEMM (depth_output_reinterpreted_as_3D = depth of the 3D output).
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010090 gemm.configure(&a, &b, &c, &dst, alpha, beta, GEMMInfo(false, false, false, (reinterpret_ouput_as_3d ? output_shape[2] : 1), reinterpret_input_as_3d));
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010091 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
95
96 // Allocate tensors
97 a.allocator()->allocate();
98 b.allocator()->allocate();
99 c.allocator()->allocate();
100 dst.allocator()->allocate();
101
102 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
106
107 // Fill tensors
108 fill(AccessorType(a), 0);
109 fill(AccessorType(b), 1);
110 fill(AccessorType(c), 2);
111
112 // Compute GEMM function
113 gemm.run();
114
115 return dst;
116 }
117
118 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 +0100119 DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100120 {
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100121 TensorShape shape_a_to_use = shape_a;
122 if(reinterpret_input_as_3d)
123 {
124 // Collapse the second and third dimension if the input is 3D
125 shape_a_to_use.collapse(2U, 1U);
126 }
127
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100128 // Create reference
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100129 SimpleTensor<T> a{ shape_a_to_use, data_type, 1 };
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100130 SimpleTensor<T> b{ shape_b, data_type, 1 };
131 SimpleTensor<T> c{ shape_c, data_type, 1 };
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100132
133 // Fill reference
134 fill(a, 0);
135 fill(b, 1);
136 fill(c, 2);
137
138 return reference::gemm<T>(a, b, c, alpha, beta);
139 }
140
141 TensorType _target{};
142 SimpleTensor<T> _reference{};
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100143 DataType _data_type{};
144};
145
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100146} // namespace validation
147} // namespace test
148} // namespace arm_compute
149#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */