tqdm in PyTorch Training Loops | Generated by AI

Home 2026.07

Question: What does tqdm do in a PyTorch/LLM training loop?

Answer: tqdm is just a progress bar wrapper around any iterable. In training loops it wraps the dataloader (or range of steps) so you get iteration speed, ETA, and a live-updating postfix for loss/lr/grad-norm — nothing more, no framework magic.

from tqdm import tqdm

for epoch in range(epochs):
    pbar = tqdm(train_loader, desc=f"epoch {epoch}")
    for batch in pbar:
        x, y = batch
        optimizer.zero_grad()
        logits = model(x)
        loss = F.cross_entropy(logits.view(-1, vocab_size), y.view(-1))
        loss.backward()
        optimizer.step()

        pbar.set_postfix(loss=loss.item(), lr=optimizer.param_groups[0]['lr'])

Key mechanics:

It’s purely cosmetic/observability — has zero effect on gradients, on distributed comms, or on the actual training math. If it’s slowing you down at high step-rate (rare, but happens on very fast H100 loops with tiny batches), disable with disable=True or bump mininterval.

References:


Generated by AI. Curating and sharing still takes effort. If you find it useful, feel free to donate. WeChat: @lzwjavaWeChat QR · X: @lzwjava · Say hi 👋

Back Donate