Skip to content

Commit bf00250

Browse files
xunsonghtye1
andauthored
Add document for legacy profiler tool (#1991)
* Add document for legacy profiler tool Signed-off-by: Huang, Xunsong <[email protected]> Co-authored-by: Ye Ting <[email protected]>
1 parent 003ea5e commit bf00250

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
67.1 KB
Loading
118 KB
Loading
66.5 KB
Loading
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# Legacy Profiler Tool
3+
4+
## Introduction
5+
6+
The legacy profiler tool is an extension of PyTorch\* legacy profiler for profiling operators' overhead on XPU devices. With this tool, users can get the information in many fields of the run models or code scripts. User should build Intel® Extension for PyTorch\* with profiler support as default and enable this tool by a `with` statement before the code segment.
7+
8+
## Use Case
9+
10+
To use the legacy profiler tool, you need to build Intel® Extension for PyTorch\* from source or install it via prebuilt wheel. You also have various methods to disable this tool.
11+
12+
### Build Tool
13+
14+
The build option `BUILD_PROFILER` is switched on as default but you can switch it off via setting `BUILD_PROFILER=OFF` while building Intel® Extension for PyTorch\* from source. With `BUILD_PROFILER=OFF`, no profiler code will be compiled and all python scripts using profiler with XPU support will raise a runtime error to user.
15+
16+
```unix
17+
[BUILD_PROFILER=ON] python setup.py install # build from source with profiler tool
18+
BUILD_PROFILER=OFF python setup.py install # build from source without profiler tool
19+
```
20+
21+
### Use Tool
22+
23+
In your model script, write `with` statement to enable the legacy profiler tool ahead of your code snippets, as shown in the following example:
24+
25+
```python
26+
# import all necessary libraries
27+
import torch
28+
import intel_extension_for_pytorch
29+
30+
# these lines won't be profiled before enabling profiler tool
31+
input_tensor = torch.randn(1024, dtype=torch.float32, device='xpu:0')
32+
33+
# enable legacy profiler tool with a `with` statement
34+
with torch.autograd.profiler_legacy.profile(use_xpu=True) as prof:
35+
# do what you want to profile here after the `with` statement with proper indent
36+
output_tensor_1 = torch.nonzero(input_tensor)
37+
output_tensor_2 = torch.unique(input_tensor)
38+
39+
# print the result table formatted by the legacy profiler tool as your wish
40+
print(prof.key_averages().table())
41+
```
42+
43+
There are a number of useful parameters defined in `torch.autograd.profiler_legacy.profile()`. Many of them are aligned with usages defined in PyTorch\*'s official profiler, such as `record_shapes`, a very useful parameter to control whether to record the shape of input tensors for each operator. To enable legacy profiler on XPU devices, pass `use_xpu=True`. For the usage of more parameters, please refer to [PyTorch\*'s tutorial page](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html).
44+
45+
### Disable Tool in Model Script
46+
47+
To disable the legacy profiler tool temporarily in your model script, pass `enabled=False` to `torch.autograd.profiler_legacy.profile()`:
48+
49+
```python
50+
with torch.autograd.profiler_legacy.profile(enabled=False, use_xpu=True) as prof:
51+
# as `enabled` is set to false, the profiler won't work on these lines of code
52+
output_tensor_1 = torch.nonzero(input_tensor)
53+
output_tensor_2 = torch.unique(input_tensor)
54+
55+
# This print will raise an error to user as the profiler was disabled
56+
print(prof.key_averages().table())
57+
```
58+
59+
### Results
60+
61+
Using the script shown above in **Use Tool** part, you'll see the result table printed out to the console as below:
62+
63+
![Legacy_profiler_result_1](../../images/profiler_legacy/Legacy_profiler_result_1.png)
64+
65+
In this result, you can find several fields like:
66+
67+
- `Name`: the name of run operators
68+
- `Self CPU %`, `Self CPU`: the time consumed by the operator itself at host excluded its children operator call. The column marked with percentage sign shows the propotion of time to total self cpu time. While an operator calls more than once in a run, the self cpu time may increase in this field.
69+
- `CPU total %`, `CPU total`: the time consumed by the operator at host included its children operator call. The column marked with percentasge sign shows the propotion of time to total cpu time. While an operator calls more than once in a run, the cpu time may increase in this field.
70+
- `CPU time avg`: the average time consumed by each once call of the operator at host. This average is calculated on the cpu total time.
71+
- `Self XPU`, `Self XPU %`: similar to `Self CPU (%)` but shows the time consumption on XPU devices.
72+
- `XPU total`: similar to `CPU total` but shows the time consumption on XPU devices.
73+
- `XPU time avg`: similar to `CPU time avg` but shows average time sonsumption on XPU devices. This average is calculated on the XPU total time.
74+
- `# of Calls`: number of call for each operators in a run.
75+
76+
You can print result table in different styles, such as sort all called operators in reverse order via `print(prof.table(sort_by='id'))` like:
77+
78+
![Legacy_profiler_result_2](../../images/profiler_legacy/Legacy_profiler_result_2.png)
79+
80+
### Export to Chrome Trace
81+
82+
You can export the result to a json file and then load it in the Chrome trace viewer (`chrome://tracing`) by add this line in your model script:
83+
84+
```python
85+
prof.export_chrome_trace("trace_file.json")
86+
```
87+
88+
In Chrome trace viewer, you may find the result shows like:
89+
90+
![Legacy_profiler_result_3](../../images/profiler_legacy/Legacy_profiler_result_3.png)
91+
92+
For more example results, please refer to [PyTorch\*'s tutorial page](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html).
93+

0 commit comments

Comments
 (0)