forked from mratsim/laser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench_exp.nim
324 lines (263 loc) · 10.1 KB
/
bench_exp.nim
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Apache v2 License
# Mamy Ratsimbazafy
# ##########################################
# Tensor primitives
import
../../laser/strided_iteration/foreach,
../../laser/tensor/[allocator, datatypes, initialization],
../../laser/[compiler_optim_hints, dynamic_stack_arrays],
../../laser/simd,
../../laser/primitives/reductions
withCompilerOptimHints()
proc randomTensor*[T](shape: openarray[int], valrange: Slice[T]): Tensor[T] =
var size: int
initTensorMetadata(result, size, shape)
allocCpuStorage(result.storage, size)
forEachContiguousSerial val in result:
val = T(rand(valrange))
func transpose*(t: Tensor): Tensor =
t.shape.reversed(result.shape)
t.strides.reversed(result.strides)
result.offset = t.offset
result.storage = t.storage
func getIndex[T](t: Tensor[T], idx: varargs[int]): int =
## Convert [i, j, k, l ...] to the memory location referred by the index
result = t.offset
for i in 0 ..< t.shape.len:
result += t.strides[i] * idx[i]
func `[]`*[T](t: Tensor[T], idx: varargs[int]): T {.inline.}=
## Index tensor
t.storage.raw_data[t.getIndex(idx)]
# ##########################################
# Benchmarking tools
import random, times, stats, strformat, math, sequtils
proc warmup() =
# Warmup - make sure cpu is on max perf
let start = epochTime()
var foo = 123
for i in 0 ..< 300_000_000:
foo += i*i mod 456
foo = foo mod 789
# Compiler shouldn't optimize away the results as cpuTime rely on sideeffects
let stop = epochTime()
echo &"Warmup: {stop - start:>4.4f} s, result {foo} (displayed to avoid compiler optimizing warmup away)"
template printStats(name: string, output: typed) {.dirty.} =
echo "\n" & name
echo &"Collected {stats.n} samples in {global_stop - global_start:>4.3f} seconds"
echo &"Average time: {stats.mean * 1000 :>4.3f} ms"
echo &"Stddev time: {stats.standardDeviationS * 1000 :>4.3f} ms"
echo &"Min time: {stats.min * 1000 :>4.3f} ms"
echo &"Max time: {stats.max * 1000 :>4.3f} ms"
echo &"Perf: {req_ops.float / stats.mean / float(10^9):>4.3f} GEXPOP/s"
echo "\nDisplay output[0] to make sure it's not optimized away"
echo output[0] # Prevents compiler from optimizing stuff away
template bench(name: string, initialisation, body: untyped) {.dirty.}=
block: # Actual bench
var stats: RunningStat
let global_start = epochTime()
for _ in 0 ..< nb_samples:
initialisation
let start = epochTime()
body
let stop = epochTime()
stats.push stop - start
let global_stop = epochTime()
printStats(name, output)
# #############################################
# Params
const
N = 100*50000 # For example for use in softmax for a batch of 100 with dictionary size of 50000 words
NbSamples = 100
let req_ops = N
let req_bytes = sizeof(float32) * N
# #############################################
import ../../laser/simd
func round_down_power_of_2(x: Natural, step: static Natural): int {.inline.} =
static: assert (step and (step - 1)) == 0, "Step must be a power of 2"
result = x and not(step - 1)
import ospaths, strutils
from os import DirSep
const cSourcesPath = currentSourcePath.rsplit(DirSep, 1)[0] & '/'
{.passC: "-I" & cSourcesPath .}
proc benchBaseline(a: Tensor[float32], nb_samples: int) =
var output = newTensor[float32](a.shape)
bench("Baseline <math.h>"):
# Initialisation, not measured apart for the "Collected n samples in ... seconds"
output.setZero() # We zero memory between computation
do:
# Main work
for i in 0 ..< a.size:
output.storage.raw_data[i] = exp(a.storage.raw_data[i])
template vectorize(
wrapped_func,
funcname,
simd_load,
simd_store: untyped,
unroll_factor: int) =
proc funcname(dst, src: ptr UncheckedArray[float32], len: Natural) =
let unroll_stop = len.round_down_power_of_2(unroll_factor)
for i in countup(0, unroll_stop - 1, unroll_factor):
dst[i].addr.simd_store src[i].addr.simd_load.wrapped_func
for i in unroll_stop ..< len:
dst[i] = src[i]
{.passC: "-DUSE_SSE2".}
proc sse_mathfun_exp_ps(x: m128): m128 {.importc: "exp_ps", header: cSourcesPath & "lib_sse_mathfun.h".}
vectorize(sse_mathfun_exp_ps, sse_mathfun_exp_ps, mm_load_ps, mm_store_ps, 4)
proc benchSSEMathfun(a: Tensor[float32], nb_samples: int) =
var output = newTensor[float32](a.shape)
bench("SSE mathfun"):
# Initialisation, not measured apart for the "Collected n samples in ... seconds"
output.setZero() # We zero memory between computation
do:
# Main work
sse_mathfun_exp_ps(output.storage.raw_data, a.storage.raw_data, a.size)
proc sse_fmath_exp_ps(x: m128): m128 {.importcpp: "fmath::exp_ps(@)", header: cSourcesPath & "lib_fmath.hpp".}
vectorize(sse_fmath_exp_ps, sse_fmath_exp_ps, mm_load_ps, mm_store_ps, 4)
proc benchSSE_fmath(a: Tensor[float32], nb_samples: int) =
var output = newTensor[float32](a.shape)
bench("SSE fmath"):
# Initialisation, not measured apart for the "Collected n samples in ... seconds"
output.setZero() # We zero memory between computation
do:
# Main work
sse_fmath_exp_ps(output.storage.raw_data, a.storage.raw_data, a.size)
{.compile: "lib_sse_exp.c".}
proc fast_exp_sse(x: m128): m128 {.importc.}
vectorize(fast_exp_sse, fast_exp_sse, mm_load_ps, mm_store_ps, 4)
proc benchSSE_fast_exp_sse(a: Tensor[float32], nb_samples: int) =
var output = newTensor[float32](a.shape)
bench("SSE fast_exp_sse (low order polynomial)"):
# Initialisation, not measured apart for the "Collected n samples in ... seconds"
output.setZero() # We zero memory between computation
do:
# Main work
fast_exp_sse(output.storage.raw_data, a.storage.raw_data, a.size)
import ../../laser/primitives/simd_math/exp_log_sse2
vectorize(exp, exp_float32x4_sse2, mm_load_ps, mm_store_ps, 4)
proc benchProdImplSSE2(a: Tensor[float32], nb_samples: int) =
var output = newTensor[float32](a.shape)
bench("SSE2 Prod implementation"):
# Initialisation, not measured apart for the "Collected n samples in ... seconds"
output.setZero() # We zero memory between computation
do:
# Main work
exp_float32x4_sse2(output.storage.raw_data, a.storage.raw_data, a.size)
# ###########################################
when defined(fast_math):
{.passC:"-ffast-math".}
when defined(march_native):
{.passC:"-march=native".}
when isMainModule:
randomize(42) # For reproducibility
warmup()
echo ""
echo &"A - tensor shape: [{N}]"
echo &"Required number of operations: {req_ops.float / float(10^6):>9.3f} millions"
echo &"Required bytes: {req_bytes.float / float(10^6):>9.3f} MB"
echo &"Arithmetic intensity: {req_ops.float / req_bytes.float:>9.3f} FLOP/byte"
block:
let a = randomTensor([N], -1.0'f32 .. 1.0'f32)
echo "a[0]: " & $a[0]
benchBaseline(a, NbSamples)
benchSSEMathfun(a, NbSamples)
benchSSE_fmath(a, NbSamples)
benchSSE_fast_exp_sse(a, NbSamples)
benchProdImplSSE2(a, NbSamples)
######################################################
## Bench on i5-5257U Broadwell - serial implementation
# Warmup: 1.2104 s, result 224 (displayed to avoid compiler optimizing warmup away)
# A - tensor shape: [5000000]
# Required number of operations: 5.000 millions
# Required bytes: 20.000 MB
# Arithmetic intensity: 0.250 FLOP/byte
# a[0]: -0.9999997019767761
# Baseline <math.h>
# Collected 100 samples in 2.625 seconds
# Average time: 24.991 ms
# Stddev time: 0.675 ms
# Min time: 24.576 ms
# Max time: 29.511 ms
# Perf: 0.200 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647
# SSE mathfun
# Collected 100 samples in 1.131 seconds
# Average time: 10.069 ms
# Stddev time: 0.253 ms
# Min time: 9.948 ms
# Max time: 11.963 ms
# Perf: 0.497 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647
# SSE fmath
# Collected 100 samples in 0.726 seconds
# Average time: 6.027 ms
# Stddev time: 0.433 ms
# Min time: 5.700 ms
# Max time: 8.140 ms
# Perf: 0.830 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647
# SSE fast_exp_sse (low order polynomial)
# Collected 100 samples in 0.599 seconds
# Average time: 4.759 ms
# Stddev time: 0.173 ms
# Min time: 4.618 ms
# Max time: 5.791 ms
# Perf: 1.051 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3682391047477722
######################################################
## Bench on i9-9980XE Skylake-X - serial implementation
## OC @ 4.1 GHz
# Warmup: 0.9044 s, result 224 (displayed to avoid compiler optimizing warmup away)
# A - tensor shape: [5000000]
# Required number of operations: 5.000 millions
# Required bytes: 20.000 MB
# Arithmetic intensity: 0.250 FLOP/byte
# a[0]: -0.9999997019767761
# Baseline <math.h>
# Collected 100 samples in 1.718 seconds
# Average time: 16.515 ms
# Stddev time: 0.094 ms
# Min time: 15.974 ms
# Max time: 16.608 ms
# Perf: 0.303 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647
# SSE mathfun
# Collected 100 samples in 0.937 seconds
# Average time: 8.699 ms
# Stddev time: 0.073 ms
# Min time: 8.403 ms
# Max time: 8.813 ms
# Perf: 0.575 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795695304871
# SSE fmath
# Collected 100 samples in 0.589 seconds
# Average time: 5.218 ms
# Stddev time: 0.022 ms
# Min time: 5.179 ms
# Max time: 5.279 ms
# Perf: 0.958 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647
# SSE fast_exp_sse (low order polynomial)
# Collected 100 samples in 0.396 seconds
# Average time: 3.287 ms
# Stddev time: 0.026 ms
# Min time: 3.192 ms
# Max time: 3.376 ms
# Perf: 1.521 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3682391047477722
# SSE2 Prod implementation
# Collected 100 samples in 0.673 seconds
# Average time: 6.064 ms
# Stddev time: 0.024 ms
# Min time: 6.017 ms
# Max time: 6.158 ms
# Perf: 0.825 GEXPOP/s
# Display output[0] to make sure it's not optimized away
# 0.3678795397281647