Profiling is a critical step in optimizing PyTorch models, and torch.profiler is the built-in tool for the job. A new guide from Hugging Face introduces beginners to this utility, explaining how it captures detailed performance metrics such as execution time, memory allocation, and kernel activity on both CPU and GPU.

The guide demonstrates how to wrap training or inference code with the profiler, then inspect the resulting trace to see which operations dominate runtime. It also covers interpreting the table of results and the timeline view, making it easier to spot unexpected slowdowns or memory spikes.

For newcomers, the key takeaway is that profiling doesn't require complex setup—just a few lines of code. By regularly using torch.profiler, developers can make data-driven decisions about where to focus optimization efforts, whether that means fusing operations, reducing memory transfers, or choosing different layer implementations.