-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfbgemm_tests.h
161 lines (133 loc) · 3.97 KB
/
fbgemm_tests.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "aligned.h"
#include "libs/FBGEMM/include/fbgemm/Fbgemm.h"
//#include "libs/FBGEMM/include/fbgemm/FbgemmSpMM.h"
#include <chrono>
//copy/paste from fbgemm source code
namespace fbgemm {
template <typename T>
void transpose_matrix(
int M,
int N,
const T* src,
int ld_src,
T* dst,
int ld_dst) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
dst[i * ld_dst + j] = src[i + j * ld_src];
}
} // for each output row
}
/*
std::chrono::duration<double> fbgemmSPMTimes(alloc::AlignedVector<uint8_t>& A,
alloc::AlignedVector<int8_t>& B,
alloc::AlignedVector<int32_t>& C,
int m,
int n,
int k) {
auto aptr = A.get();
auto bptr = B.get();
int ldat = m;
int ldbt = k;
int ldct = m;
// Transpose as if A is float so 4 columns are interleaved
// transpose_matrix(m, k / 4, aData.data(), k / 4, atData.data(), ldat);
// aligned_vector<float> btData(n * ldbt);
// auto btptr = reinterpret_cast<int8_t*>(btData.data());
// transpose_matrix(k, n, bptr, n, btptr, ldbt);
// auto cDataJIT = getRandomSparseVector(n * ldct);
// auto cptrJIT = reinterpret_cast<int32_t*>(cDataJIT.data());
auto fn = generateSpMM<int32_t>(n, m, k, bptr, ldbt, ldat, ldct);
auto start = std::chrono::system_clock::now();
fn(aptr, C.get(), 0 ); // accum_flag
auto end = std::chrono::system_clock::now();
return (end-start);
}*/
inline void col_offsets_with_zero_pt_s8acc32(
bool transpose,
int K,
int N,
const int8_t* Bint8,
const int32_t* B_zero_point,
int32_t* col_offsets,
int ncols_per_quant_group) {
for (int n = 0; n < N; ++n) {
int32_t sum = 0;
for (int k = 0; k < K; ++k) {
sum += transpose ? Bint8[k + n * K] : Bint8[k * N + n];
}
col_offsets[n] = sum - B_zero_point[n / ncols_per_quant_group] * K;
}
}
std::chrono::duration<double> fbgemmPackedTimes(alloc::AlignedVector<uint8_t>& A,
alloc::AlignedVector<int8_t>& B,
alloc::AlignedVector<int32_t>& C,
int m,
int n,
int k) {
auto aptr = A.get();
auto bptr = B.get();
int ldat = m;
int ldbt = k;
int ldct = m;
matrix_op_t atrans = matrix_op_t::NoTranspose;
matrix_op_t btrans = matrix_op_t::NoTranspose; //matrix_op_t::NoTranspose;
PackAWithRowOffset<uint8_t> packAN(
atrans,
m,
k,
A.get(),
(atrans == matrix_op_t::Transpose) ? m : k);
PackBMatrix<int8_t> packedBN(
btrans,
k,
n,
B.get(),
(btrans == matrix_op_t::Transpose) ? k : n);
//Some crap bogus bulshit values
int32_t Aint8_zero_point = 43;
float Aint8_scale = 0.11;
alloc::AlignedVector<int32_t> Bint8_zero_point(n, 256);
for (int i = 0; i < n; i++) {
Bint8_zero_point[i] = 23;
}
alloc::AlignedVector<float> Bint8_scale(n, 256);
for (int i = 0; i < n; i++) {
Bint8_scale[i] = 0.43;
}
alloc::AlignedVector<int32_t> col_offsets(n, 256);
col_offsets_with_zero_pt_s8acc32(
false,
k,
n,
B.get(),
Bint8_zero_point.get(),
col_offsets.get(),
1);
DoNothing<float, float> doNothingObj{};
ReQuantizeForFloat<false> outputProcObj(
doNothingObj,
Aint8_scale,
Bint8_scale.get(),
Aint8_zero_point,
Bint8_zero_point.get(),
packAN.getRowOffsetBuffer(),
col_offsets.get(),
nullptr,
n);
//DoNothing<int32_t, int32_t> doNothingObj{};
//memCopy<> outputProcObj(doNothingObj);
auto start = std::chrono::system_clock::now();
fbgemmPacked(
packAN,
packedBN,
reinterpret_cast<float*>(C.get()),
(int32_t*)C.get(),
(int32_t)n,
outputProcObj,
0,
1);
auto end = std::chrono::system_clock::now();
return (end-start);
}
}//namespace fbgemm