Skip to content

Commit 4af8cae

Browse files
author
Vincent Moens
committed
amend
1 parent dc86259 commit 4af8cae

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

intermediate_source/pinmem_nonblock.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,19 @@ def timer(cmd):
157157

158158
fig, ax = plt.subplots()
159159

160-
xlabels = ["Pageable Tensor", "Pinned tensor", "Pageable Tensor with pin"]
160+
xlabels = [0, 1, 2]
161161
bar_labels = [
162162
"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)",
165165
]
166166
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)
169169

170170
ax.set_ylabel("Runtime (ms)")
171171
ax.set_title("Device casting runtime (pin-memory)")
172+
ax.set_xticks([])
172173
ax.legend()
173174

174175
plt.show()
@@ -195,14 +196,14 @@ def timer(cmd):
195196
#
196197

197198

198-
def copy_to_device(*tensors, display_peak_mem=False):
199+
def copy_to_device(*tensors):
199200
result = []
200201
for tensor in tensors:
201202
result.append(tensor.to("cuda:0"))
202203
return result
203204

204205

205-
def copy_to_device_nonblocking(*tensors, display_peak_mem=False):
206+
def copy_to_device_nonblocking(*tensors):
206207
result = []
207208
for tensor in tensors:
208209
result.append(tensor.to("cuda:0", non_blocking=True))
@@ -215,16 +216,20 @@ def copy_to_device_nonblocking(*tensors, display_peak_mem=False):
215216
to_device = timer("copy_to_device(*tensors)")
216217
to_device_nonblocking = timer("copy_to_device_nonblocking(*tensors)")
217218

219+
r1 = to_device_nonblocking / to_device
220+
218221
fig, ax = plt.subplots()
219222

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"]
222226
values = [to_device, to_device_nonblocking]
223227

224-
ax.bar(xlabels, values, label=bar_labels)
228+
ax.bar(xlabels, values, label=bar_labels, color=colors)
225229

226230
ax.set_ylabel("Runtime (ms)")
227231
ax.set_title("Device casting runtime (non-blocking)")
232+
ax.set_xticks([])
228233
ax.legend()
229234

230235
plt.show()
@@ -283,24 +288,24 @@ def pin_copy_to_device_nonblocking(*tensors):
283288
return result
284289

285290

291+
tensors_pinned = [torch.randn(1000, pin_memory=True) for _ in range(1000)]
292+
286293
pin_and_copy = timer("pin_copy_to_device(*tensors)")
287294
pin_and_copy_nb = timer("pin_copy_to_device_nonblocking(*tensors)")
288295

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)")
293298

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)")
296301

297302
strategies = ("pageable copy", "pinned copy", "pin and copy")
298303
blocking = {
299304
"blocking": [page_copy, pinned_copy, pin_and_copy],
300305
"non-blocking": [page_copy_nb, pinned_copy_nb, pin_and_copy_nb],
301306
}
302307

303-
x = [0, 1, 2]
308+
x = torch.arange(3)
304309
width = 0.25
305310
multiplier = 0
306311

@@ -310,15 +315,14 @@ def pin_copy_to_device_nonblocking(*tensors):
310315
for attribute, runtimes in blocking.items():
311316
offset = width * multiplier
312317
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")
314319
multiplier += 1
315320

316321
# Add some text for labels, title and custom x-axis tick labels, etc.
317322
ax.set_ylabel("Runtime (ms)")
318323
ax.set_title("Runtime (pin-mem and non-blocking)")
319-
ax.set_xticks(x + width, strategies)
324+
ax.set_xticks([])
320325
ax.legend(loc="upper left", ncols=3)
321-
ax.set_ylim(0, 250)
322326

323327
plt.show()
324328

@@ -431,16 +435,18 @@ def pin_copy_to_device_nonblocking(*tensors):
431435
xlabels = [0, 1, 2, 3]
432436
bar_labels = [
433437
"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)",
437441
]
438442
values = [copy_blocking, copy_non_blocking, copy_pin_nb, copy_pin_multithread_nb]
443+
colors = ["tab:blue", "tab:red", "tab:orange", "tab:green"]
439444

440-
ax.bar(xlabels, values, label=bar_labels)
445+
ax.bar(xlabels, values, label=bar_labels, color=colors)
441446

442447
ax.set_ylabel("Runtime (ms)")
443448
ax.set_title("Device casting runtime")
449+
ax.set_xticks([])
444450
ax.legend()
445451

446452
plt.show()

0 commit comments

Comments
 (0)