3.3.6 · D3Deep Learning Frameworks

Worked examples — GPU acceleration and device management

2,770 words13 min readBack to topic

This page is a companion to the parent note on GPU acceleration and device management. There we built the ideas: why GPUs win (thousands of small cores doing multiply-adds in parallel), the movement tax (copying data between CPU and GPU is slow), and the device-aware training loop. Here we do the arithmetic — every case class the topic can throw at you, worked end-to-end.

Before any symbol appears, here is the vocabulary, in plain words:

The three time-quantities from the parent note, restated so this page stands alone. For multiplying an matrix by a matrix, the number of multiply-add operations is (each output entry is a sum of products, and there are outputs, and each product-then-add is two flops). Dividing that work by "how fast can we do flops" gives time:

Here = clock frequency (cycles per second, in Hz), = number of cores. The GPU line has the extra term because the numbers have to get there first.

Two more formulas we will lean on, restated inline so this page needs no other tab open:


The scenario matrix

Every GPU-timing / device-management problem you meet is one of these cells. Each worked example below is tagged with the cell it fills, and all eight examples appear on this page.

# Case class The question it answers Example
A Compute-bound, no transfer Best-case speedup, data already on GPU Ex 1
B Transfer-dominated Speedup collapses when we copy every op Ex 2
C Break-even point How much work makes the GPU trip worth it? Ex 3
D Degenerate: tiny tensor GPU slower than CPU — the zero-benefit case Ex 4
E Limiting case: transfer → 0 What is the theoretical ceiling? Ex 5
F Multi-GPU split Batch of across GPUs, communication cost Ex 6
G Real-world word problem Training-loop transfer budget over an epoch Ex 7
H Exam twist: async timing bug Why a "0.0001 s GPU" measurement is a lie Ex 8

The two axes of this matrix: how big is the work (rows A→D→E move along "amount of compute") and where does the time actually go (compute vs transfer vs communication). Together the eight examples touch every corner.

The figure below is the map you should hold in your head for the whole page. Read it like this: the blue curve is GPU compute time, which grows like (steeper); the orange curve is transfer time, which grows only like (flatter). Because the steeper curve starts below the flatter one for small and overtakes it later, there is exactly one crossing — the red dashed line at (Cell C, Ex 3). Left of the crossing (gray region) transfer dominates and the GPU can lose (Cell D, Ex 4); far right (green region) compute dominates and you approach the full 312× ceiling (Cells A & E, Ex 1 and Ex 5). Every numeric example on this page is a single point somewhere on this picture.

Figure — GPU acceleration and device management

Example 1 — Cell A: compute-bound, best case


Example 2 — Cell B: transfer-dominated, the collapse


Example 3 — Cell C: the break-even point


Example 4 — Cell D: degenerate tiny tensor, GPU loses


Example 5 — Cell E: limiting case, transfer → 0


Example 6 — Cell F: multi-GPU batch split


Example 7 — Cell G: real-world epoch transfer budget


Example 8 — Cell H: exam twist, the async timing trap


Recall Self-test: does each cell click?

Which cell is "GPU slower than CPU"? ::: Cell D — tiny tensor, transfer buries the trivial compute. As matrix size , real speedup approaches what? ::: The theoretical ceiling (transfer loses to compute ). Why can a "0.0002 s" GPU timing be a lie? ::: GPU launches are asynchronous; without torch.cuda.synchronize() you time the launch, not the finish. Splitting a batch across more GPUs changes per-GPU communication how? ::: Not at all — all-reduce moves gradients ( parameter count ), which is independent of batch size.

See also: 3.2.03-Backpropagation (the .backward() whose gradients Ex 6 all-reduces) and 3.3.07-Distributed-training-strategies (when Cell F's communication wall forces DistributedDataParallel).