blob: e807ad8c063516381933e36a170a3e98e41c789a [file] [log] [blame]
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +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_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{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class GEMMValidationFixedPointFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type, int fractional_bits)
51 {
52 _fractional_bits = fractional_bits;
53 _data_type = data_type;
54
55 _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits);
56 _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits);
57 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor, int i)
62 {
63 switch(tensor.data_type())
64 {
65 case DataType::F16:
66 case DataType::F32:
67 {
68 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
69 library->fill(tensor, distribution, i);
70 break;
71 }
72 default:
73 library->fill_tensor_uniform(tensor, i);
74 }
75 }
76
77 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta,
78 DataType data_type, int fixed_point_position)
79 {
80 // Create tensors
81 TensorType a = create_tensor<TensorType>(shape_a, data_type, 1, fixed_point_position);
82 TensorType b = create_tensor<TensorType>(shape_b, data_type, 1, fixed_point_position);
83 TensorType c = create_tensor<TensorType>(shape_c, data_type, 1, fixed_point_position);
84 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position);
85
86 // Create and configure function
87 FunctionType gemm;
88 gemm.configure(&a, &b, &c, &dst, alpha, beta);
89
90 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
91 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
94
95 // Allocate tensors
96 a.allocator()->allocate();
97 b.allocator()->allocate();
98 c.allocator()->allocate();
99 dst.allocator()->allocate();
100
101 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
105
106 // Fill tensors
107 fill(AccessorType(a), 0);
108 fill(AccessorType(b), 1);
109 fill(AccessorType(c), 2);
110
111 // Compute GEMM function
112 gemm.run();
113
114 return dst;
115 }
116
117 SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta,
118 DataType data_type, int fixed_point_position)
119 {
120 // Create reference
121 SimpleTensor<T> a{ shape_a, data_type, 1, fixed_point_position };
122 SimpleTensor<T> b{ shape_b, data_type, 1, fixed_point_position };
123 SimpleTensor<T> c{ shape_c, data_type, 1, fixed_point_position };
124
125 // Fill reference
126 fill(a, 0);
127 fill(b, 1);
128 fill(c, 2);
129
130 return reference::gemm<T>(a, b, c, alpha, beta);
131 }
132
133 TensorType _target{};
134 SimpleTensor<T> _reference{};
135 int _fractional_bits{};
136 DataType _data_type{};
137};
138
139template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
140class GEMMValidationFixture : public GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
141{
142public:
143 template <typename...>
144 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type)
145 {
146 GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, 0);
147 }
148};
149} // namespace validation
150} // namespace test
151} // namespace arm_compute
152#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */