All articles

Why your GPU training run is slower than expected, and how to find the bottleneck

A slow training run is usually waiting on the data loader, the CPU or the disk, not the GPU. Find which one with nvidia-smi, py-spy and the PyTorch profiler. Checked against PyTorch 2.15 docs, 27 November 2026.
Lium team6 min read
pytorch
training
performance
troubleshooting
Short answer: before you rent a bigger GPU, check whether the one you have is busy. Run nvidia-smi dmon -s u while training. If the sm column sits well below 90% or swings between 0 and 100, the GPU is waiting for something, almost always the input pipeline. Then:
  1. Data loader: set num_workers above 0, pin_memory=True and persistent_workers=True on your DataLoader. The defaults are 0, False and False, which load every batch in the training process itself.
  2. CPU: run py-spy top on the training process. If most of the time is in decoding, augmentation or tokenization, move that work into the workers or do it once ahead of time.
  3. Disk: if the workers are busy but mostly waiting on reads, copy the dataset to the pod's local disk or into fewer, larger files before training.
  4. The GPU itself: only when sm stays high and steps are still slow, profile with torch.profiler and look at the top CUDA kernels. That is the point where a faster card actually helps.
Everything below is checked against the PyTorch 2.15 torch.utils.data docs, the PyTorch profiler recipe (page dated 19 July 2026), the py-spy README and NVIDIA's nvidia-smi documentation, all read 27 November 2026.

Step 1: is the GPU actually busy?

nvidia-smi dmon prints one line per second per GPU. -s u selects the utilization group: SM, memory, encoder and decoder utilization in percent (NVIDIA nvidia-smi docs).
bash
nvidia-smi dmon -s u
text
# gpu     sm    mem    enc    dec
    0     38     12      0      0
    0     97     41      0      0
    0      0      0      0      0
    0     41     14      0      0
Read the sm column with one caveat in mind. NVIDIA defines GPU utilization as the "percent of time over the past sample period during which one or more kernels was executing on the GPU". A single small kernel counts as busy, so 100% does not mean the card is saturated. A low or bursty number, though, reliably means the GPU spent that time idle. A pattern like the one above, high for a moment and then near zero, is a GPU finishing each batch quickly and then waiting for the next.
Add -s um to see memory use next to it, and -s ut to see PCIe throughput if you suspect host-to-device copies.

Step 2: fix the data loader first

PyTorch's DataLoader defaults are num_workers=0, pin_memory=False and persistent_workers=False (PyTorch 2.15 data docs). With num_workers=0, the training process loads and transforms every batch itself, and the GPU waits while it does.
python
loader = DataLoader(
    dataset,
    batch_size=256,
    shuffle=True,
    num_workers=8,            # worker processes; start near the pod's CPU core count, then tune
    pin_memory=True,          # page-locked host memory for faster copies to the GPU
    persistent_workers=True,  # keep workers alive between epochs
    prefetch_factor=4,        # batches each worker loads ahead (default 2 when num_workers > 0)
)
 
for x, y in loader:
    x = x.cuda(non_blocking=True)
    y = y.cuda(non_blocking=True)
What each setting does, per the docs:
  • num_workers moves loading into separate processes, so batches are prepared while the GPU computes.
  • pin_memory=True copies tensors into pinned memory before returning them, which makes the transfer to a CUDA GPU faster. Pair it with non_blocking=True on the .cuda() call.
  • persistent_workers=True stops the loader from shutting its workers down after each pass over the dataset, so each epoch does not pay the startup cost again.
  • prefetch_factor defaults to 2 batches per worker once num_workers > 0.
Check nproc on the pod before choosing num_workers. More workers than cores just adds contention. Re-run nvidia-smi dmon -s u after each change: if sm rises and steadies, that was the bottleneck.

Step 3: find where the CPU time goes with py-spy

If more workers did not help, find out what the CPU is doing. py-spy is a sampling profiler that reads a running Python process from outside, so you do not change your code.
bash
pip install py-spy
 
# live view of the hottest functions in the training process and its workers
py-spy top --subprocesses --pid <PID>
 
# a flame graph over 60 seconds
py-spy record --subprocesses -d 60 -o profile.svg --pid <PID>
--subprocesses matters here because DataLoader workers are child processes; without it you only see the main process. The README notes that attaching to an existing PID on Linux usually needs root, and that inside Docker the default seccomp profile blocks it unless the container has SYS_PTRACE. If the attach is refused, start the run under py-spy instead:
bash
py-spy record --subprocesses -o profile.svg -- python train.py
What to look for in the flame graph:
  • Image decoding, resizing, augmentation: the workers are CPU-bound. Add workers if cores are free, cache decoded or resized data once, or move augmentation to the GPU.
  • Tokenization: tokenize the dataset once and save it, rather than in __getitem__ on every epoch.
  • read, open or file-system calls: the disk is the limit (step 4).
  • Your training loop itself, for example .item() or print of a tensor every step: each forces a GPU-to-CPU sync. Log every N steps instead.

Step 4: rule out the disk

Thousands of small files on network storage are a common cause of a slow first epoch. Two quick checks on the pod:
bash
df -h /workspace /root          # where the dataset actually lives
time cat /path/to/dataset/* > /dev/null   # rough read speed for one pass
If one cold pass over the files takes longer than an epoch should, the fix is on the data side: copy the dataset to local disk at the start of the run, or pack it into fewer large files (for example WebDataset tar shards or a single Arrow or Parquet file) so each read is sequential.

Step 5: only now, profile the GPU

When sm stays high and steps are still slower than you expect, look at the GPU work itself with the PyTorch profiler. The profiler recipe uses a schedule so you profile a few steady-state steps, not the warm-up:
python
from torch.profiler import profile, ProfilerActivity, schedule
 
with profile(
    activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
    schedule=schedule(wait=1, warmup=1, active=3),
    on_trace_ready=lambda p: p.export_chrome_trace(f"/workspace/trace_{p.step_num}.json"),
) as prof:
    for step, (x, y) in enumerate(loader):
        train_step(x, y)
        prof.step()
        if step >= 5:
            break
 
print(prof.key_averages().table(sort_by="cuda_time_total", row_limit=15))
Open the trace in Perfetto or chrome://tracing. Gaps between GPU kernels mean the GPU is still waiting on the CPU. Long matmul and attention kernels with no gaps mean the work really is GPU-bound. At that point the usual levers are mixed precision (bf16), a larger batch if memory allows, torch.compile, or a faster card.

Running this on Lium

Each check above runs inside the pod over SSH or with lium exec, so you can measure before deciding on hardware. Start the run as in Train a PyTorch model on a Lium GPU, then:
bash
lium exec resnet "nvidia-smi dmon -s u -c 20"
lium exec resnet "nproc"
-c 20 stops dmon after 20 samples, so lium exec returns. If the GPU turns out to be idle most of the time, a bigger card will not help; fix the loader first. If it is genuinely saturated, compare cards on the H100 and H200 pages and in B200 vs H200 vs H100 for training. lium.io/stats shows which GPUs are available on the network right now. Pods bill by the second, so a 10-minute profiling session costs only those 10 minutes.
Get new posts by e-mail: Subscribe to the Lium newsletter