Antoni Viros i Martin explains a common PyTorch performance trap in this four-minute IBM Technology whiteboard lesson.
One scalar read crosses two boundaries
- A tensor holds numerical data. Calling
.item()requests one value as an ordinary Python number. torch.compileoptimizes stretches of model code together, combining operations and planning memory use.- Pulling a computed value into Python can split that stretch into smaller compiled regions, with Python between them.
- If the value lives on an accelerator, such as a GPU, the host must also wait until the value is ready.
Diagnose the costs separately
- Graph break: the compiler loses opportunities to optimize across the boundary and has more separate pieces to run.
- Synchronization: Python waits for a device result, then decides what work to enqueue next; the device can sit idle.
- A graph break splits the captured computation. It differs from recompiling the model or restarting its execution.
Keep frequent logging out of the critical path
- Inspect scalar reads used for thresholds, printing and logging inside training loops.
- Where the application allows it, collect a needed value every few hundred steps or move the read outside the busy loop.
- Caveat: the slowdown depends on device placement, compiler settings and surrounding work. The video gives no measured speedup.
“One line, two boundaries. The compiler splits and the chip waits.” — Antoni Viros i Martin, 3:45–3:50