@@ -157,18 +157,19 @@ def timer(cmd):
157
157
158
158
fig , ax = plt .subplots ()
159
159
160
- xlabels = ["Pageable Tensor" , "Pinned tensor" , "Pageable Tensor with pin" ]
160
+ xlabels = [0 , 1 , 2 ]
161
161
bar_labels = [
162
162
"pageable_tensor.to(device) (1x)" ,
163
- f"pinned_tensor.to(device) ({ r1 :4.4f } x)" ,
164
- f"pageable_tensor.pin_memory().to(device) ({ r2 :4.4f } x)" ,
163
+ f"pinned_tensor.to(device) ({ r1 :4.2f } x)" ,
164
+ f"pageable_tensor.pin_memory().to(device) ({ r2 :4.2f } x)" ,
165
165
]
166
166
values = [pageable_to_device , pinned_to_device , pin_mem_to_device ]
167
-
168
- ax .bar (xlabels , values , label = bar_labels )
167
+ colors = [ "tab:blue" , "tab:red" , "tab:orange" ]
168
+ ax .bar (xlabels , values , label = bar_labels , color = colors )
169
169
170
170
ax .set_ylabel ("Runtime (ms)" )
171
171
ax .set_title ("Device casting runtime (pin-memory)" )
172
+ ax .set_xticks ([])
172
173
ax .legend ()
173
174
174
175
plt .show ()
@@ -195,14 +196,14 @@ def timer(cmd):
195
196
#
196
197
197
198
198
- def copy_to_device (* tensors , display_peak_mem = False ):
199
+ def copy_to_device (* tensors ):
199
200
result = []
200
201
for tensor in tensors :
201
202
result .append (tensor .to ("cuda:0" ))
202
203
return result
203
204
204
205
205
- def copy_to_device_nonblocking (* tensors , display_peak_mem = False ):
206
+ def copy_to_device_nonblocking (* tensors ):
206
207
result = []
207
208
for tensor in tensors :
208
209
result .append (tensor .to ("cuda:0" , non_blocking = True ))
@@ -215,16 +216,20 @@ def copy_to_device_nonblocking(*tensors, display_peak_mem=False):
215
216
to_device = timer ("copy_to_device(*tensors)" )
216
217
to_device_nonblocking = timer ("copy_to_device_nonblocking(*tensors)" )
217
218
219
+ r1 = to_device_nonblocking / to_device
220
+
218
221
fig , ax = plt .subplots ()
219
222
220
- xlabels = ["to(device)" , "to(device, non_blocking=True)" ]
221
- bar_labels = xlabels
223
+ xlabels = [0 , 1 ]
224
+ bar_labels = [f"to(device) (1x)" , f"to(device, non_blocking=True) ({ r1 :4.2f} x)" ]
225
+ colors = ["tab:blue" , "tab:red" ]
222
226
values = [to_device , to_device_nonblocking ]
223
227
224
- ax .bar (xlabels , values , label = bar_labels )
228
+ ax .bar (xlabels , values , label = bar_labels , color = colors )
225
229
226
230
ax .set_ylabel ("Runtime (ms)" )
227
231
ax .set_title ("Device casting runtime (non-blocking)" )
232
+ ax .set_xticks ([])
228
233
ax .legend ()
229
234
230
235
plt .show ()
@@ -283,24 +288,24 @@ def pin_copy_to_device_nonblocking(*tensors):
283
288
return result
284
289
285
290
291
+ tensors_pinned = [torch .randn (1000 , pin_memory = True ) for _ in range (1000 )]
292
+
286
293
pin_and_copy = timer ("pin_copy_to_device(*tensors)" )
287
294
pin_and_copy_nb = timer ("pin_copy_to_device_nonblocking(*tensors)" )
288
295
289
- page_copy = timer ("copy_to_device(*tensors" )
290
- page_copy_nb = timer ("copy_to_device_nonblocking(*tensors_pinned))" )
291
-
292
- tensors_pinned = [torch .randn (1000 , pin_memory = True ) for _ in range (1000 )]
296
+ page_copy = timer ("copy_to_device(*tensors)" )
297
+ page_copy_nb = timer ("copy_to_device_nonblocking(*tensors)" )
293
298
294
- pinned_copy = timer ("copy_to_device(*tensors " )
295
- pinned_copy_nb = timer ("copy_to_device_nonblocking(*tensors_pinned)) " )
299
+ pinned_copy = timer ("copy_to_device(*tensors_pinned) " )
300
+ pinned_copy_nb = timer ("copy_to_device_nonblocking(*tensors_pinned)" )
296
301
297
302
strategies = ("pageable copy" , "pinned copy" , "pin and copy" )
298
303
blocking = {
299
304
"blocking" : [page_copy , pinned_copy , pin_and_copy ],
300
305
"non-blocking" : [page_copy_nb , pinned_copy_nb , pin_and_copy_nb ],
301
306
}
302
307
303
- x = [ 0 , 1 , 2 ]
308
+ x = torch . arange ( 3 )
304
309
width = 0.25
305
310
multiplier = 0
306
311
@@ -310,15 +315,14 @@ def pin_copy_to_device_nonblocking(*tensors):
310
315
for attribute , runtimes in blocking .items ():
311
316
offset = width * multiplier
312
317
rects = ax .bar (x + offset , runtimes , width , label = attribute )
313
- ax .bar_label (rects , padding = 3 )
318
+ ax .bar_label (rects , padding = 3 , fmt = "%.2f" )
314
319
multiplier += 1
315
320
316
321
# Add some text for labels, title and custom x-axis tick labels, etc.
317
322
ax .set_ylabel ("Runtime (ms)" )
318
323
ax .set_title ("Runtime (pin-mem and non-blocking)" )
319
- ax .set_xticks (x + width , strategies )
324
+ ax .set_xticks ([] )
320
325
ax .legend (loc = "upper left" , ncols = 3 )
321
- ax .set_ylim (0 , 250 )
322
326
323
327
plt .show ()
324
328
@@ -431,16 +435,18 @@ def pin_copy_to_device_nonblocking(*tensors):
431
435
xlabels = [0 , 1 , 2 , 3 ]
432
436
bar_labels = [
433
437
"Blocking copy (1x)" ,
434
- f"Non-blocking copy ({ r1 :4.4f } x)" ,
435
- f"Blocking pin, non-blocking copy ({ r2 :4.4f } x)" ,
436
- f"Non-blocking pin, non-blocking copy ({ r3 :4.4f } x)" ,
438
+ f"Non-blocking copy ({ r1 :4.2f } x)" ,
439
+ f"Blocking pin, non-blocking copy ({ r2 :4.2f } x)" ,
440
+ f"Non-blocking pin, non-blocking copy ({ r3 :4.2f } x)" ,
437
441
]
438
442
values = [copy_blocking , copy_non_blocking , copy_pin_nb , copy_pin_multithread_nb ]
443
+ colors = ["tab:blue" , "tab:red" , "tab:orange" , "tab:green" ]
439
444
440
- ax .bar (xlabels , values , label = bar_labels )
445
+ ax .bar (xlabels , values , label = bar_labels , color = colors )
441
446
442
447
ax .set_ylabel ("Runtime (ms)" )
443
448
ax .set_title ("Device casting runtime" )
449
+ ax .set_xticks ([])
444
450
ax .legend ()
445
451
446
452
plt .show ()
0 commit comments