Welcome to mirror list, hosted at ThFree Co, Russian Federation.

biasmultiply.cc « benchmarks - github.com/marian-nmt/intgemm/intgemm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65deadb7f970dadda6534f1d2de1873b1f677f57 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "../intgemm/intgemm.h"
#include "../intgemm/aligned.h"
#include <chrono>
#include <random>
#include <iostream>

using namespace intgemm;

template <class Routine>
void testOld(Index /*rows*/, Index /*cols*/) {
}

template <class Routine>
std::chrono::duration<double> testNew(Index A_rows, Index width, Index B_cols) {
  AlignedVector<float> A(A_rows * width);
  AlignedVector<float> B(width * B_cols);
  AlignedVector<float> bias(B_cols);
  std::mt19937 gen;
  std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
  for (auto& it : A) {
    it = dist(gen);
  }
  for (auto& it : B) {
    it = dist(gen);
  }
  for (auto& it : bias) {
    it = dist(gen);
  }

  float alpha = 2.0f;
  float quant_mult = 127.0f / alpha;
  float unquant_mult = 1.0f / (quant_mult*quant_mult);

  AlignedVector<uint8_t> A_prep(A.size());
  AlignedVector<int8_t> B_prep(B.size());
  Routine::PrepareA(A.begin(), A_prep.begin(), quant_mult, A_rows, width);
  Routine::PrepareB(B.begin(), B_prep.begin(), quant_mult, width, B_cols);

  AlignedVector<float> test_C(A_rows * B_cols);

  float unquant_mult_forprep = (-1)*(alpha)*(alpha)/(127.0f); //Minus one to invert add_ps later on
  Routine::PrepareBias(B_prep.begin(), width, B_cols, callbacks::UnquantizeAndAddBiasAndWrite(unquant_mult_forprep, bias.begin(), bias.begin()));
  auto start = std::chrono::system_clock::now();
  Routine::Multiply8Shift(A_prep.begin(), B_prep.begin(), A_rows, width, B_cols, callbacks::UnquantizeAndAddBiasAndWrite(unquant_mult, bias.begin(), test_C.begin()));
  auto end = std::chrono::system_clock::now();

  std::chrono::duration<double> elapsed_seconds = end-start;
  return elapsed_seconds;

}

template <class Routine>
std::chrono::duration<double> testOld(Index A_rows, Index width, Index B_cols) {
  AlignedVector<float> A(A_rows * width);
  AlignedVector<float> B(width * B_cols);
  AlignedVector<float> bias(B_cols);
  std::mt19937 gen;
  std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
  for (auto& it : A) {
    it = dist(gen);
  }
  for (auto& it : B) {
    it = dist(gen);
  }
  for (auto& it : bias) {
    it = dist(gen);
  }

  float alpha = 2.0f;
  float quant_mult = 127.0f / alpha;
  float unquant_mult = 1.0f / (quant_mult*quant_mult);

  AlignedVector<int8_t> A_prep(A.size());
  AlignedVector<int8_t> B_prep(B.size());
  Routine::PrepareA(A.begin(), A_prep.begin(), quant_mult, A_rows, width);
  Routine::PrepareB(B.begin(), B_prep.begin(), quant_mult, width, B_cols);

  AlignedVector<float> test_C(A_rows * B_cols);

  auto start = std::chrono::system_clock::now();
  Routine::Multiply(A_prep.begin(), B_prep.begin(), A_rows, width, B_cols, callbacks::UnquantizeAndAddBiasAndWrite(unquant_mult, bias.begin(), test_C.begin()));
  auto end = std::chrono::system_clock::now();

  std::chrono::duration<double> elapsed_seconds = end-start;
  return elapsed_seconds;

}

template <class Routine>
std::chrono::duration<double> testOld_nobias(Index A_rows, Index width, Index B_cols) {
  AlignedVector<float> A(A_rows * width);
  AlignedVector<float> B(width * B_cols);
  std::mt19937 gen;
  std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
  for (auto& it : A) {
    it = dist(gen);
  }
  for (auto& it : B) {
    it = dist(gen);
  }

  float alpha = 2.0f;
  float quant_mult = 127.0f / alpha;
  float unquant_mult = 1.0f / (quant_mult*quant_mult);

  AlignedVector<int8_t> A_prep(A.size());
  AlignedVector<int8_t> B_prep(B.size());
  Routine::PrepareA(A.begin(), A_prep.begin(), quant_mult, A_rows, width);
  Routine::PrepareB(B.begin(), B_prep.begin(), quant_mult, width, B_cols);

  AlignedVector<float> test_C(A_rows * B_cols);

  auto start = std::chrono::system_clock::now();
  Routine::Multiply(A_prep.begin(), B_prep.begin(), A_rows, width, B_cols, callbacks::UnquantizeAndWrite(unquant_mult, test_C.begin()));
  auto end = std::chrono::system_clock::now();

  std::chrono::duration<double> elapsed_seconds = end-start;
  return elapsed_seconds;

}

int main(int argc, char ** argv) {
	int repeat = 1000;
	if (argc > 1) {
		repeat = atoi(argv[1]);
	}

	std::chrono::duration<double> oldSSSE3_nobias = testOld_nobias<ssse3::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(8, 256, 256);
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(8, 2048, 256);
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(320, 256, 256);
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(472, 256, 256);
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(248, 256, 256);
		oldSSSE3_nobias += testOld_nobias<ssse3::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of SSSE3 without bias took: " << oldSSSE3_nobias.count() << " seconds." << std::endl;

	std::chrono::duration<double> oldSSSE3 = testOld<ssse3::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldSSSE3 += testOld<ssse3::Kernels8>(8, 256, 256);
		oldSSSE3 += testOld<ssse3::Kernels8>(8, 2048, 256);
		oldSSSE3 += testOld<ssse3::Kernels8>(320, 256, 256);
		oldSSSE3 += testOld<ssse3::Kernels8>(472, 256, 256);
		oldSSSE3 += testOld<ssse3::Kernels8>(248, 256, 256);
		oldSSSE3 += testOld<ssse3::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of SSSE3 took: " << oldSSSE3.count() << " seconds." << std::endl;

	std::chrono::duration<double> newTimeSSSE3 = testOld<ssse3::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		newTimeSSSE3 += testNew<ssse3::Kernels8>(8, 256, 256);
		newTimeSSSE3 += testNew<ssse3::Kernels8>(8, 2048, 256);
		newTimeSSSE3 += testNew<ssse3::Kernels8>(320, 256, 256);
		newTimeSSSE3 += testNew<ssse3::Kernels8>(472, 256, 256);
		newTimeSSSE3 += testNew<ssse3::Kernels8>(248, 256, 256);
		newTimeSSSE3 += testNew<ssse3::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of Shifted SSSE3 took: " << newTimeSSSE3.count() << " seconds." << std::endl;

#ifdef INTGEMM_COMPILER_SUPPORTS_AVX2
	std::chrono::duration<double> oldAVX2_nobias = testOld_nobias<avx2::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(8, 256, 256);
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(8, 2048, 256);
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(320, 256, 256);
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(472, 256, 256);
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(248, 256, 256);
		oldAVX2_nobias += testOld_nobias<avx2::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of AVX2 without bias took: " << oldAVX2_nobias.count() << " seconds." << std::endl;

	std::chrono::duration<double> oldAVX2 = testOld<avx2::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldAVX2 += testOld<avx2::Kernels8>(8, 256, 256);
		oldAVX2 += testOld<avx2::Kernels8>(8, 2048, 256);
		oldAVX2 += testOld<avx2::Kernels8>(320, 256, 256);
		oldAVX2 += testOld<avx2::Kernels8>(472, 256, 256);
		oldAVX2 += testOld<avx2::Kernels8>(248, 256, 256);
		oldAVX2 += testOld<avx2::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of AVX2 took: " << oldAVX2.count() << " seconds." << std::endl;

	std::chrono::duration<double> newTimeAVX2 = testOld<avx2::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		newTimeAVX2 += testNew<avx2::Kernels8>(8, 256, 256);
		newTimeAVX2 += testNew<avx2::Kernels8>(8, 2048, 256);
		newTimeAVX2 += testNew<avx2::Kernels8>(320, 256, 256);
		newTimeAVX2 += testNew<avx2::Kernels8>(472, 256, 256);
		newTimeAVX2 += testNew<avx2::Kernels8>(248, 256, 256);
		newTimeAVX2 += testNew<avx2::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of Shifted AVX2 took: " << newTimeAVX2.count() << " seconds." << std::endl;
#endif
#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512BW
	if (kCPU < CPUType::AVX512BW) return 0;
	std::chrono::duration<double> oldAVX512_nobias = testOld_nobias<avx512bw::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(8, 256, 256);
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(8, 2048, 256);
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(320, 256, 256);
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(472, 256, 256);
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(248, 256, 256);
		oldAVX512_nobias += testOld_nobias<avx512bw::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of AVX512 without bias took: " << oldAVX512_nobias.count() << " seconds." << std::endl;

	std::chrono::duration<double> oldAVX512 = testOld<avx512bw::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		oldAVX512 += testOld<avx512bw::Kernels8>(8, 256, 256);
		oldAVX512 += testOld<avx512bw::Kernels8>(8, 2048, 256);
		oldAVX512 += testOld<avx512bw::Kernels8>(320, 256, 256);
		oldAVX512 += testOld<avx512bw::Kernels8>(472, 256, 256);
		oldAVX512 += testOld<avx512bw::Kernels8>(248, 256, 256);
		oldAVX512 += testOld<avx512bw::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of AVX512 took: " << oldAVX512.count() << " seconds." << std::endl;

	std::chrono::duration<double> newTimeAVX512 = testOld<avx512bw::Kernels8>(1, 64, 8);
	for (int i = 0; i<repeat; i++) {
		newTimeAVX512 += testNew<avx512bw::Kernels8>(8, 256, 256);
		newTimeAVX512 += testNew<avx512bw::Kernels8>(8, 2048, 256);
		newTimeAVX512 += testNew<avx512bw::Kernels8>(320, 256, 256);
		newTimeAVX512 += testNew<avx512bw::Kernels8>(472, 256, 256);
		newTimeAVX512 += testNew<avx512bw::Kernels8>(248, 256, 256);
		newTimeAVX512 += testNew<avx512bw::Kernels8>(200, 256, 256);
	}

	std::cout << repeat << " iterations of Shifted AVX512 took: " << newTimeAVX512.count() << " seconds." << std::endl;
#endif
#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512VNNI
  if (kCPU < CPUType::AVX512VNNI) return 0;
  std::chrono::duration<double> oldAVX512VNNI_nobias = testOld_nobias<avx512bw::Kernels8>(1, 64, 8);
  for (int i = 0; i<repeat; i++) {
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(8, 256, 256);
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(8, 2048, 256);
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(320, 256, 256);
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(472, 256, 256);
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(248, 256, 256);
          oldAVX512VNNI_nobias += testOld_nobias<avx512vnni::Kernels8>(200, 256, 256);
  }

  std::cout << repeat << " iterations of AVX512VNNI without bias took: " << oldAVX512VNNI_nobias.count() << " seconds." << std::endl;

  std::chrono::duration<double> oldAVX512VNNI = testOld<avx512bw::Kernels8>(1, 64, 8);
  for (int i = 0; i<repeat; i++) {
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(8, 256, 256);
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(8, 2048, 256);
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(320, 256, 256);
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(472, 256, 256);
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(248, 256, 256);
          oldAVX512VNNI += testOld<avx512vnni::Kernels8>(200, 256, 256);
  }

  std::cout << repeat << " iterations of AVX512VNNI took: " << oldAVX512VNNI.count() << " seconds." << std::endl;

  std::chrono::duration<double> newTimeAVX512VNNI = testOld<avx512bw::Kernels8>(1, 64, 8);
  for (int i = 0; i<repeat; i++) {
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(8, 256, 256);
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(8, 2048, 256);
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(320, 256, 256);
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(472, 256, 256);
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(248, 256, 256);
    newTimeAVX512VNNI += testNew<avx512vnni::Kernels8>(200, 256, 256);
  }

  std::cout << repeat << " iterations of Shifted AVX512VNNI took: " << newTimeAVX512VNNI.count() << " seconds." << std::endl;
#endif

}