FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlprodict/_doc/examples/plot_parallelism.py at loop · sdpython/mlprodict · GitHub
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
sdpython
mlprodict
Repository navigation
Code
Issues
14
(14)
Pull requests
5
(5)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mlprodict
/
_doc
/
examples
/
plot_parallelism.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
346 lines (276 loc) · 9.41 KB
Breadcrumbs
mlprodict
/
_doc
/
examples
/
plot_parallelism.py
Copy path
File metadata and controls
346 lines (276 loc) · 9.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
.. _l-example-parallelism:
When to parallelize?
====================
That is the question. Parallize computation
takes some time to set up, it is not the right
solution in every case. The following example studies
the parallelism introduced into the runtime of
*TreeEnsembleRegressor* to see when it is best
to do it.
.. contents::
:local:
"""
from
pprint
import
pprint
import
numpy
from
pandas
import
DataFrame
import
matplotlib
.
pyplot
as
plt
from
tqdm
import
tqdm
from
sklearn
import
config_context
from
sklearn
.
datasets
import
make_regression
from
sklearn
.
ensemble
import
HistGradientBoostingRegressor
from
sklearn
.
model_selection
import
train_test_split
from
cpyquickhelper
.
numbers
import
measure_time
from
pyquickhelper
.
pycode
.
profiling
import
profile
from
mlprodict
.
onnx_conv
import
to_onnx
,
register_rewritten_operators
from
mlprodict
.
onnxrt
import
OnnxInference
from
mlprodict
.
tools
.
model_info
import
analyze_model
#####################################
# Available optimisations on this machine.
from
mlprodict
.
testing
.
experimental_c_impl
.
experimental_c
import
code_optimisation
print
(
code_optimisation
())
########################################
# Training and converting a model
# +++++++++++++++++++++++++++++++
data
=
make_regression
(
50000
,
20
)
X
,
y
=
data
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
)
hgb
=
HistGradientBoostingRegressor
(
max_iter
=
100
,
max_depth
=
6
)
hgb
.
fit
(
X_train
,
y_train
)
print
(
hgb
)
########################################
# Let's get more statistics about the model itself.
pprint
(
analyze_model
(
hgb
))
#################################
# And let's convert it.
register_rewritten_operators
()
onx
=
to_onnx
(
hgb
,
X_train
[:
1
].
astype
(
numpy
.
float32
))
oinf
=
OnnxInference
(
onx
,
runtime
=
'python_compiled'
)
print
(
oinf
)
################################
# The runtime of the forest is in the following object.
print
(
oinf
.
sequence_
[
0
].
ops_
)
print
(
oinf
.
sequence_
[
0
].
ops_
.
rt_
)
########################################
# And the threshold used to start parallelizing
# based on the number of observations.
print
(
oinf
.
sequence_
[
0
].
ops_
.
rt_
.
omp_N_
)
######################################
# Profiling
# +++++++++
#
# This step involves :epkg:`pyinstrument` to measure
# where the time is spent. Both :epkg:`scikit-learn`
# and :epkg:`mlprodict` runtime are called so that
# the prediction times can be compared.
X32
=
X_test
.
astype
(
numpy
.
float32
)
def
runlocal
():
with
config_context
(
assume_finite
=
True
):
for
i
in
range
(
0
,
100
):
oinf
.
run
({
'X'
:
X32
[:
1000
]})
hgb
.
predict
(
X_test
[:
1000
])
print
(
"profiling..."
)
txt
=
profile
(
runlocal
,
pyinst_format
=
'text'
)
print
(
txt
[
1
])
##########################################
# Now let's measure the performance the average
# computation time per observations for 2 to 100
# observations. The runtime implemented in
# :epkg:`mlprodict` parallizes the computation
# after a given number of observations.
obs
=
[]
for
N
in
tqdm
(
list
(
range
(
2
,
21
))):
m
=
measure_time
(
"oinf.run({'X': x})"
,
{
'oinf'
:
oinf
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
20
)
m
[
'N'
]
=
N
m
[
'RT'
]
=
'ONNX'
obs
.
append
(
m
)
with
config_context
(
assume_finite
=
True
):
m
=
measure_time
(
"hgb.predict(x)"
,
{
'hgb'
:
hgb
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
15
)
m
[
'N'
]
=
N
m
[
'RT'
]
=
'SKL'
obs
.
append
(
m
)
df
=
DataFrame
(
obs
)
num
=
[
'min_exec'
,
'average'
,
'max_exec'
]
for
c
in
num
:
df
[
c
]
/=
df
[
'N'
]
df
.
head
()
#######################################
# Graph.
fig
,
ax
=
plt
.
subplots
(
1
,
2
,
figsize
=
(
10
,
4
))
df
[
df
.
RT
==
'ONNX'
].
set_index
(
'N'
)[
num
].
plot
(
ax
=
ax
[
0
])
ax
[
0
].
set_title
(
"Average ONNX prediction time per observation in a batch."
)
df
[
df
.
RT
==
'SKL'
].
set_index
(
'N'
)[
num
].
plot
(
ax
=
ax
[
1
])
ax
[
1
].
set_title
(
"Average scikit-learn prediction time
\n
per observation in a batch."
)
######################################
# Gain from parallelization
# +++++++++++++++++++++++++
#
# There is a clear gap between after and before 10 observations
# when it is parallelized. Does this threshold depends on the number
# of trees in the model?
# For that we compute for each model the average prediction time
# up to 10 and from 10 to 20.
def
parallized_gain
(
df
):
df
=
df
[
df
.
RT
==
'ONNX'
]
df10
=
df
[
df
.
N
<=
10
]
t10
=
sum
(
df10
[
'average'
])
/
df10
.
shape
[
0
]
df10p
=
df
[
df
.
N
>
10
]
t10p
=
sum
(
df10p
[
'average'
])
/
df10p
.
shape
[
0
]
return
t10
/
t10p
print
(
'gain'
,
parallized_gain
(
df
))
#################################
# Measures based on the number of trees
# +++++++++++++++++++++++++++++++++++++
#
# We trained many models with different number
# of trees to see how the parallelization gain
# is moving. One models is trained for every
# distinct number of trees and then the prediction
# time is measured for different number of observations.
tries_set
=
[
2
,
5
,
8
]
+
list
(
range
(
10
,
50
,
5
))
+
list
(
range
(
50
,
101
,
10
))
tries
=
[(
nb
,
N
)
for
N
in
range
(
2
,
21
,
2
)
for
nb
in
tries_set
]
###########################
# training
models
=
{
100
: (
hgb
,
oinf
)}
for
nb
in
tqdm
(
set
(
_
[
0
]
for
_
in
tries
)):
if
nb
not
in
models
:
hgb
=
HistGradientBoostingRegressor
(
max_iter
=
nb
,
max_depth
=
6
)
hgb
.
fit
(
X_train
,
y_train
)
onx
=
to_onnx
(
hgb
,
X_train
[:
1
].
astype
(
numpy
.
float32
))
oinf
=
OnnxInference
(
onx
,
runtime
=
'python_compiled'
)
models
[
nb
]
=
(
hgb
,
oinf
)
###########################
# prediction time
obs
=
[]
for
nb
,
N
in
tqdm
(
tries
):
hgb
,
oinf
=
models
[
nb
]
m
=
measure_time
(
"oinf.run({'X': x})"
,
{
'oinf'
:
oinf
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
50
)
m
[
'N'
]
=
N
m
[
'nb'
]
=
nb
m
[
'RT'
]
=
'ONNX'
obs
.
append
(
m
)
df
=
DataFrame
(
obs
)
num
=
[
'min_exec'
,
'average'
,
'max_exec'
]
for
c
in
num
:
df
[
c
]
/=
df
[
'N'
]
df
.
head
()
##########################
# Let's compute the gains.
gains
=
[]
for
nb
in
set
(
df
[
'nb'
]):
gain
=
parallized_gain
(
df
[
df
.
nb
==
nb
])
gains
.
append
(
dict
(
nb
=
nb
,
gain
=
gain
))
dfg
=
DataFrame
(
gains
)
dfg
=
dfg
.
sort_values
(
'nb'
).
reset_index
(
drop
=
True
).
copy
()
dfg
##########################################
# Graph.
ax
=
dfg
.
set_index
(
'nb'
).
plot
()
ax
.
set_title
(
"Parallelization gain depending
\n
on the number of trees
\n
(max_depth=6)."
)
##############################
# That does not answer the question we are looking for
# as we would like to know the best threshold *th*
# which defines the number of observations for which
# we should parallelized. This number depends on the number
# of trees. A gain > 1 means the parallization should happen
# Here, even two observations is ok.
# Let's check with lighter trees (``max_depth=2``),
# maybe in that case, the conclusion is different.
models
=
{
100
: (
hgb
,
oinf
)}
for
nb
in
tqdm
(
set
(
_
[
0
]
for
_
in
tries
)):
if
nb
not
in
models
:
hgb
=
HistGradientBoostingRegressor
(
max_iter
=
nb
,
max_depth
=
2
)
hgb
.
fit
(
X_train
,
y_train
)
onx
=
to_onnx
(
hgb
,
X_train
[:
1
].
astype
(
numpy
.
float32
))
oinf
=
OnnxInference
(
onx
,
runtime
=
'python_compiled'
)
models
[
nb
]
=
(
hgb
,
oinf
)
obs
=
[]
for
nb
,
N
in
tqdm
(
tries
):
hgb
,
oinf
=
models
[
nb
]
m
=
measure_time
(
"oinf.run({'X': x})"
,
{
'oinf'
:
oinf
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
50
)
m
[
'N'
]
=
N
m
[
'nb'
]
=
nb
m
[
'RT'
]
=
'ONNX'
obs
.
append
(
m
)
df
=
DataFrame
(
obs
)
num
=
[
'min_exec'
,
'average'
,
'max_exec'
]
for
c
in
num
:
df
[
c
]
/=
df
[
'N'
]
df
.
head
()
###################################
# Measures.
gains
=
[]
for
nb
in
set
(
df
[
'nb'
]):
gain
=
parallized_gain
(
df
[
df
.
nb
==
nb
])
gains
.
append
(
dict
(
nb
=
nb
,
gain
=
gain
))
dfg
=
DataFrame
(
gains
)
dfg
=
dfg
.
sort_values
(
'nb'
).
reset_index
(
drop
=
True
).
copy
()
dfg
#######################################
# Graph.
ax
=
dfg
.
set_index
(
'nb'
).
plot
()
ax
.
set_title
(
"Parallelization gain depending
\n
on the number of trees
\n
(max_depth=3)."
)
#########################################
# The conclusion is somewhat the same but
# it shows that the bigger the number of trees is
# the bigger the gain is and under the number of
# cores of the processor.
#
# Moving the theshold
# +++++++++++++++++++
#
# The last experiment consists in comparing the prediction
# time with or without parallelization for different
# number of observation.
hgb
=
HistGradientBoostingRegressor
(
max_iter
=
40
,
max_depth
=
6
)
hgb
.
fit
(
X_train
,
y_train
)
onx
=
to_onnx
(
hgb
,
X_train
[:
1
].
astype
(
numpy
.
float32
))
oinf
=
OnnxInference
(
onx
,
runtime
=
'python_compiled'
)
obs
=
[]
for
N
in
tqdm
(
list
(
range
(
2
,
51
))):
oinf
.
sequence_
[
0
].
ops_
.
rt_
.
omp_N_
=
100
m
=
measure_time
(
"oinf.run({'X': x})"
,
{
'oinf'
:
oinf
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
20
)
m
[
'N'
]
=
N
m
[
'RT'
]
=
'ONNX'
m
[
'PARALLEL'
]
=
False
obs
.
append
(
m
)
oinf
.
sequence_
[
0
].
ops_
.
rt_
.
omp_N_
=
1
m
=
measure_time
(
"oinf.run({'X': x})"
,
{
'oinf'
:
oinf
,
'x'
:
X32
[:
N
]},
div_by_number
=
True
,
number
=
50
)
m
[
'N'
]
=
N
m
[
'RT'
]
=
'ONNX'
m
[
'PARALLEL'
]
=
True
obs
.
append
(
m
)
df
=
DataFrame
(
obs
)
num
=
[
'min_exec'
,
'average'
,
'max_exec'
]
for
c
in
num
:
df
[
c
]
/=
df
[
'N'
]
df
.
head
()
##########################################
# Graph.
piv
=
df
[[
'N'
,
'PARALLEL'
,
'average'
]].
pivot
(
'N'
,
'PARALLEL'
,
'average'
)
ax
=
piv
.
plot
(
logy
=
True
)
ax
.
set_title
(
"Prediction time with and without parallelization."
)
###############################
# Parallelization is working.
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL