forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseDenseMMFP32Benchmark.cc
131 lines (115 loc) · 3.83 KB
/
SparseDenseMMFP32Benchmark.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "bench/BenchUtils.h"
#include "fbgemm/FbgemmSparse.h"
#include "fbgemm/Utils.h"
#include "fbgemm/spmmUtils.h"
#include "src/RefImplementations.h"
#include <iomanip>
#include <iostream>
using namespace std;
using namespace fbgemm;
int main(int, char**) {
vector<vector<int>> shapes = getSparseMatrixShapes();
// C is MxN -> CT is NxM
// A is MxK -> AT is KxM
// B is KxN -> BT is NxK
cout << setw(7) << "index" << setw(7) << "m" << setw(7) << "n" << setw(7)
<< "k" << setw(7) << "fnz" << setw(15) << "eff_GFLOPS" << setw(15)
<< "real_GFLOPS" << endl;
int index = 0;
// for (int s = 64; s <= 128; s *= 2)
for (auto const& s : shapes) {
int m = s[0];
int n = s[1];
int k = s[2];
for (float fnz = 0.20; fnz >= 0.20; fnz -= 0.01) {
auto aData = getRandomSparseVector(m * k);
auto bData = getRandomSparseVector(k * n, fnz);
auto cData = getRandomSparseVector(m * n);
aligned_vector<float> atData(k * m);
aligned_vector<float> btData(n * k);
aligned_vector<float> ctData(n * m);
aligned_vector<float> ctDataRef(n * m);
aligned_vector<float> ctDataIntrin(n * m);
transpose_matrix(m, k, aData.data(), k, atData.data(), m);
transpose_matrix(k, n, bData.data(), n, btData.data(), k);
unique_ptr<CSRMatrix<float>> csr = fbgemmDenseToCSR(n, k, btData.data());
// We calculate C^T = B^T x A^T
int ldat = m;
// int ldbt = k;
int ldct = m;
double effective_flop = m * n * k * 2;
constexpr int NWARMUP = 20;
constexpr int NITER = 100;
auto secs_intrin = measureWithWarmup(
[&]() {
SparseDenseMM(
n,
m,
csr->rowPtr.data(),
csr->colIdx.data(),
csr->values.data(),
atData.data(),
ldat,
ctDataIntrin.data(),
ldct);
},
NWARMUP,
NITER,
[&]() {
cache_evict(atData);
cache_evict(csr->rowPtr);
cache_evict(csr->colIdx);
cache_evict(csr->values);
cache_evict(ctDataIntrin);
});
// printMatrix(matrix_op_t::NoTranspose, btData.data(), n, k, k,
// "btData");
// printMatrix(matrix_op_t::NoTranspose, atData.data(), k, m, m,
// "atData");
// printMatrix(matrix_op_t::NoTranspose, ctData.data(), n, m, m,
// "ctData");
cblas_sgemm_ref(
matrix_op_t::NoTranspose,
matrix_op_t::NoTranspose,
m,
n,
k,
1.0f,
aData.data(),
k,
bData.data(),
n,
0.0f,
cData.data(),
n);
transpose_matrix(m, n, cData.data(), n, ctDataRef.data(), m);
// printMatrix(matrix_op_t::NoTranspose, ctDataRef.data(), n, m, m,
// "ctData_Ref");
//
// Compare results
for (size_t i = 0; i < ctDataRef.size(); i++) {
if (std::abs(ctDataRef[i] - ctDataIntrin[i]) > 1e-3) {
fprintf(
stderr,
"Error: Results differ ref %f and test %f at %ld\n",
ctDataRef[i],
ctDataIntrin[i],
i);
return 1;
}
}
double effective_gflops_intrin = effective_flop / secs_intrin / 1e9;
cout << "[" << setw(5) << index << "]" << setw(7) << m << setw(7) << n
<< setw(7) << k << fixed << setw(7) << setprecision(2) << fnz
<< setw(15) << setprecision(5) << effective_gflops_intrin << setw(15)
<< setprecision(5) << fnz * effective_gflops_intrin << endl;
++index;
}
}
}