Introduction
In our last session, we successfully configured the torch.profiler to generate a detailed execution trace with memory information and launched TensorBoard to get our first look at the results. We now have the raw data at our fingertips. This lesson will guide you through the process of transforming that data into actionable insights.
Our focus today will be entirely within the "Memory View" of the TensorBoard profiler plugin. You will learn to interpret the memory consumption timeline, a graph that illustrates the dynamic memory usage of your model. We will practice using TensorBoard's interactive tools to isolate the exact moment of peak memory consumption and identify the specific operations responsible for it. This skill is fundamental for diagnosing and resolving memory-related performance bottlenecks.
The Anatomy of the Memory Timeline
Once you navigate to the "Memory View" in TensorBoard, the most prominent feature is the timeline graph at the top. This graph plots memory usage against time, providing a visual narrative of your model's memory behavior.

As you'll recall from our first module, PyTorch uses a caching memory allocator to improve performance.
- Allocated Memory is the memory actually in use by tensors at any given moment. This is the blue curve and is our primary focus for analysis.
- Reserved Memory is the total amount of memory held by PyTorch from the GPU. The allocator reserves larger blocks (chunks) and sub-allocates from them, which is why this red line is a high-water mark and doesn't fluctuate as rapidly as the allocated memory.
The "Allocated" memory curve has a characteristic shape during a typical training iteration. The video below provides a concise explanation of this pattern.
Lightning Talk: Profiling and Memory Debugging Tools for Distributed ML Workloads on GPUs- Aaron Shi
In this short clip from a talk at a PyTorch conference, the speaker walks through a memory snapshot, explaining the typical memory dynamics of a training loop.
Watch the segment from five iterations. Notice how he describes the rise in memory during the forward pass (allocating activations) and the fall during the backward pass (releasing activations and creating gradients).
This repeating "hump" or "sawtooth" pattern—rising during the forward pass and falling during the backward pass—is the signature of a well-behaved training loop. The highest point of this hump represents the peak memory usage for that iteration. Our goal is to find this peak and understand what caused it.
Interactive Analysis: Pinpointing the Peak
TensorBoard's Memory View is not just a static image; it's an interactive tool. You can zoom in on specific time ranges to get a more detailed breakdown of events. This is how we pinpoint the cause of peak memory usage.
The process is straightforward:
- Locate the highest point on the blue "Allocated" memory curve.
- Left-click and drag your mouse to create a selection box around that peak.
When you release the mouse button, the entire view—both the graph and the tables below it—will update to show only the events that occurred within your selected time window. This allows you to filter out the noise and focus on the exact moment the memory footprint was largest.
The following reading from the official PyTorch documentation describes the Memory View and its components in detail.
PyTorch Profiler With TensorBoard
This is the official tutorial for the TensorBoard profiler plugin. We will focus on the section describing the memory view.
Please read section 6. Analyze performance with other advanced features, focusing on the part about the Memory view. Pay close attention to the description of the three components (curve, events table, statistics table) and how you can interact with the graph by selecting a time range.
Interpreting the Memory Event Tables
After you've zoomed in on the peak, your attention should shift to the tables below the graph. These tables provide the granular data you need to identify the root cause.

There are two key tables:
-
Memory Events Table (top): This table lists individual allocation/deallocation events. When you've selected a tiny window around the peak, this table will show you the exact operator that performed the allocation that pushed the memory usage to its maximum. Key columns include:
- Operator: The name of the operator that triggered the allocation. As the documentation noted, the profiler provides helpful context, showing
aten::ones (aten::empty)instead of just the low-level allocator callaten::empty. - Size (KB): The size of this specific memory allocation.
- Allocation Time / Release Time: The lifecycle of the tensor created by this allocation.
- Operator: The name of the operator that triggered the allocation. As the documentation noted, the profiler provides helpful context, showing
-
Memory Statistics Table (bottom): This table provides an aggregated view, summing up memory usage by operator type.
- Size Increase: The net memory change (allocations minus deallocations) for each operator type.
- Allocation Size: The gross memory allocated by each operator type.
By selecting the peak on the graph and then inspecting the Memory Events table, you can answer the question: "What single operation was responsible for the highest memory usage in my training step?"
From Analysis to Optimization: A Case Study
Identifying the peak is the first step. The next is using that information to make decisions. For example, if memory usage is very low, it might indicate an opportunity to increase your batch size to improve GPU utilization and throughput.
The following article demonstrates this exact thought process.
PyTorch Model Performance Analysis and Optimization
This article from Towards Data Science provides a practical walk-through of using the profiler to optimize a model.
Please read the section titled Optimization #3. The author analyzes the Memory View, observes that peak utilization is very low, and uses this insight to justify increasing the batch size, which significantly boosts performance.
Conversely, you might find that you cannot increase your batch size due to an OutOfMemoryError. Analyzing the memory timeline can reveal why. Sometimes, the issue isn't the steady-state memory usage but an anomalous spike caused by a single large tensor or an unexpected memory leak.
In this next clip, the presenter analyzes a memory plot to understand a more complex memory issue where peak usage is much higher than the average, preventing him from using a larger batch size.
Lecture 16: On Hands Profiling
This segment from "On Hands Profiling" shows a real-world debugging session where the memory timeline reveals a surprising memory consumption pattern.
Watch from I run out of memory. The speaker is confused why he's running out of memory when the steady-state activations seem small. By inspecting the memory profiler's output, he discovers an anomalous peak far exceeding the typical memory usage, pointing to a different kind of problem than just large activations. This highlights how the timeline can reveal unexpected behavior.
Conclusion
In this lesson, you've learned how to move from data generation to data analysis within TensorBoard's Memory View. You can now dissect the memory timeline of your model's execution and apply a methodical process to find the source of peak memory consumption.
The key takeaways are:
- The "Allocated" memory curve shows the dynamic memory usage of your model, typically rising during the forward pass and falling during the backward pass.
- You can interactively select a time range around the peak of the memory curve to filter the profiler data.
- The "Memory Events" table, when filtered to a peak, reveals the specific operator responsible for the largest memory allocation.
- Analyzing peak memory usage isn't just for debugging errors; it's crucial for performance optimization, informing decisions like adjusting the batch size.
So far, we have focused on a time-centric view to find a single point of failure or opportunity. In the next lesson, we will shift our perspective. We'll explore the profiler's Operator View, which provides an aggregated summary of memory and time consumption across all operators. This will help us identify which parts of our model are systematically the most expensive, rather than just focusing on a single moment in time.