blob: e4762cc5be2c8f4f425f15ceaa8d291985235c14 [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{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class GEMMValidationFixedPointFixture : public framework::Fixture
47{
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).
90 bool is_output_reinterpreted_as_3D = output_shape.num_dimensions() > shape_a.num_dimensions();
91 gemm.configure(&a, &b, &c, &dst, alpha, beta,
92 GEMMInfo(false, false, false, is_output_reinterpreted_as_3D ? output_shape[2] : 1));
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010093
94 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
97 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
98
99 // Allocate tensors
100 a.allocator()->allocate();
101 b.allocator()->allocate();
102 c.allocator()->allocate();
103 dst.allocator()->allocate();
104
105 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
108 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
109
110 // Fill tensors
111 fill(AccessorType(a), 0);
112 fill(AccessorType(b), 1);
113 fill(AccessorType(c), 2);
114
115 // Compute GEMM function
116 gemm.run();
117
118 return dst;
119 }
120
121 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 +0100122 DataType data_type)
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100123 {
124 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100125 SimpleTensor<T> a{ shape_a, data_type, 1 };
126 SimpleTensor<T> b{ shape_b, data_type, 1 };
127 SimpleTensor<T> c{ shape_c, data_type, 1 };
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100128
129 // Fill reference
130 fill(a, 0);
131 fill(b, 1);
132 fill(c, 2);
133
134 return reference::gemm<T>(a, b, c, alpha, beta);
135 }
136
137 TensorType _target{};
138 SimpleTensor<T> _reference{};
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100139 DataType _data_type{};
140};
141
142template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
143class GEMMValidationFixture : public GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
144{
145public:
146 template <typename...>
147 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type)
148 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100149 GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100150 }
151};
152} // namespace validation
153} // namespace test
154} // namespace arm_compute
155#endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */