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
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.
- Dec 2, 2026Amazon to Speed Up Ergo From 400 to 2,200 Tokens per Second and Buy 2 Million More Nvidia GPUs
- Dec 2, 2026Canonical Releases Ubuntu 26.04 for RTX Spark Laptops with 128 GB of Shared Memory
- Dec 2, 2026Vidu S2 streams AI video in real time: 720p, 25–42 fps, and on-the-fly outfit swaps
- Dec 2, 2026AI-Written Rust Driver for NVIDIA Boosts Tesla V100 Memory by 26.8%
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.
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.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
