Skip to content

Commit 7127ea9

Browse files
Olivia-liufacebook-github-bot
authored andcommitted
New doc for the memory planning inspection util function (#5430)
Summary: Pull Request resolved: #5430 ## Why The memory planning inspection tool `generate_memory_trace()` has proved to be useful by users, and we will be presenting it in the executorch debugging and profiling lightning talk at PTC, so it's important to have documentation for it which users can refer to. ## What A new page in the official doc that talks about how to use the memory planning inspection tool `generate_memory_trace()`. Reviewed By: dbort Differential Revision: D62796663 fbshipit-source-id: 6c868590d805e9f21bccc7023131eb814b96e5b1
1 parent d4afbe7 commit 7127ea9

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed
612 KB
Loading

docs/source/compiler-memory-planning.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ program = edge_program.to_executorch(
8383
```
8484

8585
Users attempting to write a custom memory planning algorithm should start by looking at [the greedy algorithm's implementation](https://github.com/pytorch/executorch/blob/d62c41ca86435e5316e7ed292b6d68aff27a2fb7/exir/memory_planning.py#L459C1-L459C12).
86+
87+
## Debugging Tool
88+
89+
Please refer to [Memory Planning Inspection](./memory-planning-inspection.md) for a tool to inspect the result of memory planning.

docs/source/devtools-overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The ExecuTorch Developer Tools support the following features:
1515
- **Delegate Integration** - Surfacing performance details from delegate backends
1616
- Link back delegate operator execution to the nodes they represent in the edge dialect graph (and subsequently linking back to source code and module hierarchy)
1717
- **Debugging** - Intermediate outputs and output quality analysis
18+
- **Memory Allocation Insights** - Visualize how memory is planned, where all the live tensors are at any point in time
1819
- **Visualization** - Coming soon
1920

2021
## Fundamental components of the Developer Tools

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Topics in this section will help you get started with ExecuTorch.
207207
sdk-profiling
208208
sdk-debugging
209209
sdk-inspector
210+
memory-planning-inspection
210211
sdk-delegate-integration
211212
devtools-tutorial
212213

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Memory Planning Inspection in ExecuTorch
2+
3+
After the [Memory Planning](https://pytorch.org/executorch/main/concepts.html#memory-planning) pass of ExecuTorch, memory allocation information is stored on the nodes of the [`ExportedProgram`](https://pytorch.org/executorch/main/concepts.html#exportedprogram). Here, we present a tool designed to inspect memory allocation and visualize all active tensor objects.
4+
5+
## Usage
6+
User should add this code after they call [to_executorch()](https://pytorch.org/executorch/main/export-to-executorch-api-reference.html#executorch.exir.EdgeProgramManager.to_executorch), and it will write memory allocation information stored on the nodes to the file path "memory_profile.json". The file is compatible with the Chrome trace viewer; see below for more information about interpreting the results.
7+
8+
```python
9+
from executorch.util.activation_memory_profiler import generate_memory_trace
10+
generate_memory_trace(
11+
executorch_program_manager=prog,
12+
chrome_trace_filename="memory_profile.json",
13+
enable_memory_offsets=True,
14+
)
15+
```
16+
* `prog` is an instance of [`ExecuTorchProgramManager`](https://pytorch.org/executorch/main/export-to-executorch-api-reference.html#executorch.exir.ExecutorchProgramManager), returned by [to_executorch()](https://pytorch.org/executorch/main/export-to-executorch-api-reference.html#executorch.exir.EdgeProgramManager.to_executorch).
17+
* Set `enable_memory_offsets` to `True` to show the location of each tensor on the memory space.
18+
19+
## Chrome Trace
20+
Open a Chrome browser tab and navigate to <chrome://tracing/>. Upload the generated `.json` to view.
21+
Example of a [MobileNet V2](https://pytorch.org/vision/main/models/mobilenetv2.html) model:
22+
23+
![Memory planning Chrome trace visualization](/_static/img/memory_planning_inspection.png)
24+
25+
Note that, since we are repurposing the Chrome trace tool, the axes in this context may have different meanings compared to other Chrome trace graphs you may have encountered previously:
26+
* The horizontal axis, despite being labeled in seconds (s), actually represents megabytes (MBs).
27+
* The vertical axis has a 2-level hierarchy. The first level, "pid", represents memory space. For CPU, everything is allocated on one "space"; other backends may have multiple. In the second level, each row represents one time step. Since nodes will be executed sequentially, each node represents one time step, thus you will have as many nodes as there are rows.
28+
29+
## Further Reading
30+
* [Memory Planning](https://pytorch.org/executorch/main/compiler-memory-planning.html)

0 commit comments

Comments
 (0)