Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →
InfrastructureOpen Weights · ActionableHugging Face Blog→ original

How to Speed Up PyTorch Models: A Practical Guide to torch.profiler

Hugging Face has published the first part of its guide to torch.profiler, the performance analysis tool built into PyTorch. It tracks every operation on the GPU and CPU, exposes bottlenecks in computation and helps cut model training time. The guide is useful for ML engineers and researchers who want to understand where their compute goes and speed up their projects.

AI-processed from Hugging Face Blog; edited by Hamidun News
How to Speed Up PyTorch Models: A Practical Guide to torch.profiler
Source: Hugging Face Blog. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHugging Face Blog ↗
Editor-in-Chief
Exclusive timethe time the operation took on its own (excluding nested operations)
Inclusive timethe time of the operation together with all nested calls
Memorypeak GPU memory usage during the operation

PyTorch includes a built-in tool, torch.profiler, for analyzing model performance. It tracks every operation on the GPU and CPU, helping you find computational bottlenecks and optimize training time.

Why profile models

Without profiling, a developer sees only the final number: "the epoch trained for 2 hours." But why 2 hours? Where did the time go? Maybe the dataloader is slow? Or an operation on the GPU is inefficient? Or the GPU sits idle waiting for data from the CPU?

Optimizing blindly is guesswork. You change the batch size, the loading speed, the numerical precision, and the result doesn't improve, because you are optimizing the wrong bottleneck. torch.profiler spares you this waste of time: it shows the exact distribution of time and memory across operations.

How torch.profiler works

The tool traces code execution at the level of CUDA kernels and CPU threads. For each operation, such as a matrix multiplication in a Linear layer or a convolution in Conv2d, it records the start time, end time and memory used. Results can be exported to a format compatible with Chrome DevTools and visualized as a timeline.

The profiler's main metrics:

  • Exclusive time: the time the operation took on its own (excluding nested operations)
  • Inclusive time: the time of the operation together with all nested calls
  • Memory: peak GPU memory usage during the operation
  • Sync time: time spent on synchronization between CPU and GPU (the number one bottleneck)

When torch.profiler uncovers problems

A typical scenario: a model trains slowly, you assume you need a faster GPU, but the profiler shows that 60% of the epoch is spent waiting for data from the dataloader. The problem is not the GPU but data loading. The fix: increase num_workers in DataLoader or use pinned memory.

Another example: a model trains on a distributed system (several GPUs), and the profiler shows that a lot of time goes to synchronizing gradients between devices. That is a signal to optimize the communication graph or rethink the parallelism strategy.

An important caveat: profiling itself adds overhead. If you run the profiler over the entire training loop, it can slow computation by 10–30%. For accurate measurements, it is better to profile only the block of code you care about (for example, a single forward-backward pass).

What this means

torch.profiler is the first step before any optimization. Don't guess where the bottleneck is: profile, read the results, and optimize exactly what is slowing you down. That can save weeks of experiments and direct effort to where it will actually pay off. For ML engineers who want to speed up model training, this is a basic tool.

Story

Key Questions Answered

Why profile models?

Without profiling, a developer sees only the final number: "the epoch trained for 2 hours." But why 2 hours? Where did the time go? Maybe the dataloader is slow? Or an operation on the GPU is inefficient? Or the GPU sits idle waiting for data from the CPU? Optimizing blindly is guesswork. You change the batch size, the loading speed, the numerical precision, and the result doesn't improve, because you are optimizing the wrong bottleneck.

How torch.profiler works?

The tool traces code execution at the level of CUDA kernels and CPU threads. For each operation, such as a matrix multiplication in a Linear layer or a convolution in Conv2d, it records the start time, end time and memory used. Results can be exported to a format compatible with Chrome DevTools and visualized as a timeline.

When torch.profiler uncovers problems?

A typical scenario: a model trains slowly, you assume you need a faster GPU, but the profiler shows that 60% of the epoch is spent waiting for data from the dataloader. The problem is not the GPU but data loading. The fix: increase num_workers in DataLoader or use pinned memory. Another example: a model trains on a distributed system (several GPUs), and the profiler shows that a lot of time goes to synchronizing gradients between devices.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — How to Speed Up PyTorch Models: A Practical Guide to torch.profiler https://news.hamidun.com/en/news/10271/how-to-speed-up-pytorch-models-a-practical-guide-to-torch-pr

What do you think?
Loading comments…