blob: d6b94a197d27cc57d4f13d3a758d5e5120d23be8 [file] [log] [blame]
Pablo Tello181e6512017-11-15 13:28:27 +00001/*
David Mansell101de502018-02-06 17:11:21 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tello181e6512017-11-15 13:28:27 +00003 *
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_GEMMLOWP_ASSEMBLY_FIXTURE
25#define ARM_COMPUTE_TEST_GEMMLOWP_ASSEMBLY_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Pablo Tello181e6512017-11-15 13:28:27 +000034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/GEMMLowp.h"
Pablo Tello181e6512017-11-15 13:28:27 +000036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000045template <typename TensorType, typename AccessorType, typename FunctionType, typename T2>
Pablo Tello181e6512017-11-15 13:28:27 +000046class GEMMLowpAssemblyFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(size_t m, size_t n, size_t k)
51 {
52 const TensorShape shape_a(k, m);
53 const TensorShape shape_b(n, k);
54 const TensorShape shape_c(n, m);
55 _target = compute_target(shape_a, shape_b, shape_c);
56 _reference = compute_reference(shape_a, shape_b, shape_c);
57 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor, int i, int lo, int hi)
62 {
63 std::uniform_int_distribution<> distribution(lo, hi);
64 library->fill(tensor, distribution, i);
65 }
66
67 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c)
68 {
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000069 DataType dt_in = std::is_same<T2, int8_t>::value ? DataType::S8 : DataType::U8;
70
Pablo Tello181e6512017-11-15 13:28:27 +000071 // Create tensors
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000072 TensorType a = create_tensor<TensorType>(shape_a, dt_in, 1);
73 TensorType b = create_tensor<TensorType>(shape_b, dt_in, 1);
Pablo Tello181e6512017-11-15 13:28:27 +000074 TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1);
75
76 // Create and configure function
77 FunctionType gemmlowp;
78 gemmlowp.configure(&a, &b, &c);
79
80 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
81 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
82 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
83
84 // Allocate tensors
85 a.allocator()->allocate();
86 b.allocator()->allocate();
87 c.allocator()->allocate();
88
89 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
93 // Fill tensors
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000094 if(dt_in == DataType::S8)
95 {
96 fill(AccessorType(a), 0, -128, 127);
97 fill(AccessorType(b), 1, -128, 127);
98 }
99 else
100 {
David Mansell101de502018-02-06 17:11:21 +0000101 fill(AccessorType(a), 0, 0, 255);
102 fill(AccessorType(b), 1, 0, 255);
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000103 }
Pablo Tello181e6512017-11-15 13:28:27 +0000104 fill(AccessorType(c), 2, 0, 0);
105
106 // Compute GEMM function
107 gemmlowp.run();
108 return c;
109 }
110
111 SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c)
112 {
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000113 DataType dt = std::is_same<T2, int8_t>::value ? DataType::S8 : DataType::U8;
114
Pablo Tello181e6512017-11-15 13:28:27 +0000115 // Create reference
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000116 SimpleTensor<T2> a{ shape_a, dt, 1 };
117 SimpleTensor<T2> b{ shape_b, dt, 1 };
Pablo Tello181e6512017-11-15 13:28:27 +0000118
119 // Fill reference
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000120 if(dt == DataType::S8)
121 {
122 fill(a, 0, -128, 127);
123 fill(b, 1, -128, 127);
124 }
125 else
126 {
David Mansell101de502018-02-06 17:11:21 +0000127 fill(a, 0, 0, 255);
128 fill(b, 1, 0, 255);
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000129 }
Pablo Tello181e6512017-11-15 13:28:27 +0000130
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000131 return reference::gemmlowp<int32_t, T2>(a, b);
Pablo Tello181e6512017-11-15 13:28:27 +0000132 }
133
134 TensorType _target{};
135 SimpleTensor<int32_t> _reference{};
136};
137
138} // namespace validation
139} // namespace test
140} // namespace arm_compute
141#endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */