blob: 77d025ec8e7f7f655a926b85f6a633c3a5d2fd29 [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#include "GEMM.h"
25
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/validation/FixedPoint.h"
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +010028
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
38SimpleTensor<T> gemm(const SimpleTensor<T> &a, const SimpleTensor<T> &b, const SimpleTensor<T> &c, float alpha, float beta)
39{
40 // Create reference
41 SimpleTensor<T> dst{ c.shape(), c.data_type(), 1, c.fixed_point_position() };
42
43 // Compute reference
44 const int M = dst.shape().y();
45 const int N = dst.shape().x();
46 const int K = a.shape().x();
47
48 for(int row = 0; row < M; ++row)
49 {
50 for(int col = 0; col < N; ++col)
51 {
52 T acc(0);
53
54 for(int k = 0; k < K; ++k)
55 {
56 acc += a[row * K + k] * b[k * N + col];
57 }
58
59 // Finalize the result: alpha * A * B + beta * C
60 dst[col + row * N] = alpha * acc + beta * c[col + row * N];
61 }
62 }
63
64 return dst;
65}
66
67template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
68SimpleTensor<T> gemm(const SimpleTensor<T> &a, const SimpleTensor<T> &b, const SimpleTensor<T> &c, float alpha, float beta)
69{
70 using namespace fixed_point_arithmetic;
71
72 // Create reference
73 SimpleTensor<T> dst{ c.shape(), c.data_type(), 1, c.fixed_point_position() };
74
75 // Compute reference
76 using promoted_type = fixed_point_arithmetic::traits::promote_t<T>;
77
78 const int M = dst.shape().y();
79 const int N = dst.shape().x();
80 const int K = a.shape().x();
81 const int fixed_point_position = a.fixed_point_position();
82
83 const fixed_point<T> alpha_q(alpha, fixed_point_position);
84 const fixed_point<T> beta_q(beta, fixed_point_position);
85
86 for(int row = 0; row < M; ++row)
87 {
88 for(int col = 0; col < N; ++col)
89 {
90 fixed_point<promoted_type> acc_q(0, fixed_point_position);
91
92 for(int k = 0; k < K; ++k)
93 {
94 const fixed_point<promoted_type> a0_q(a[row * K + k], fixed_point_position, true);
95 const fixed_point<promoted_type> b0_q(b[k * N + col], fixed_point_position, true);
96
97 acc_q = acc_q + (a0_q * b0_q);
98 }
99
100 // Finalize the result: alpha * A * B + beta * C
101 const fixed_point<T> c0_q(c[col + row * N], fixed_point_position, true);
102
103 fixed_point<T> res_q(acc_q);
104 res_q = alpha_q * res_q;
105 res_q = res_q + (beta_q * c0_q);
106
107 // Store the result
108 dst[col + row * N] = res_q.raw();
109 }
110 }
111
112 return dst;
113}
114
115template SimpleTensor<float> gemm(const SimpleTensor<float> &a, const SimpleTensor<float> &b, const SimpleTensor<float> &c, float alpha, float beta);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100116template SimpleTensor<half> gemm(const SimpleTensor<half> &a, const SimpleTensor<half> &b, const SimpleTensor<half> &c, float alpha, float beta);
Moritz Pflanzer4dfc2352017-08-02 14:51:36 +0100117template SimpleTensor<qint8_t> gemm(const SimpleTensor<qint8_t> &a, const SimpleTensor<qint8_t> &b, const SimpleTensor<qint8_t> &c, float alpha, float beta);
118template SimpleTensor<qint16_t> gemm(const SimpleTensor<qint16_t> &a, const SimpleTensor<qint16_t> &b, const SimpleTensor<qint16_t> &c, float alpha, float beta);
119} // namespace reference
120} // namespace validation
121} // namespace test
122} // namespace arm_compute