-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: Add torch profiler example (#1026)
* add profiler example Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * lint Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix markdown lint Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
- Loading branch information
Showing
7 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ignorePatterns": [ | ||
{ | ||
"pattern": "^http://localhost.*" | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# PyTorch profiler example | ||
|
||
This example is adopted from torch's [official tutorial](https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html). It shows how to use PyTorch profiler to analyze performance bottlenecks in a model. | ||
|
||
## Usage | ||
|
||
Run `envd up` at current folder. And execute `python main.py` in the envd container. Then you can see the profiling result through TensorBoard at http://localhost:8888 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
envdlib = include("https://github.com/tensorchord/envdlib") | ||
|
||
|
||
def build(): | ||
base(os="ubuntu20.04", language="python3") | ||
shell("zsh") | ||
install.cuda(version="11.2.0", cudnn="8") | ||
install.python_packages( | ||
[ | ||
"torch", | ||
"torchvision", | ||
"torch_tb_profiler", | ||
"--extra-index-url https://download.pytorch.org/whl/cu113", | ||
] | ||
) | ||
envdlib.tensorboard(envd_port=8888, envd_dir="/home/envd/log", host_port=8888) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Adopt from https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html | ||
import torch | ||
import torch.nn | ||
import torch.optim | ||
import torch.profiler | ||
import torch.utils.data | ||
import torchvision.datasets | ||
import torchvision.models | ||
import torchvision.transforms as T | ||
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights | ||
|
||
transform = T.Compose( | ||
[T.Resize(224), T.ToTensor(), T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] | ||
) | ||
train_set = torchvision.datasets.CIFAR10( | ||
root="./data", train=True, download=True, transform=transform | ||
) | ||
train_loader = torch.utils.data.DataLoader(train_set, batch_size=2, shuffle=True) | ||
|
||
device = torch.device("cuda:0") | ||
model = efficientnet_b0(weights=EfficientNet_B0_Weights.DEFAULT).to(device) | ||
criterion = torch.nn.CrossEntropyLoss().cuda(device) | ||
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9) | ||
model.train() | ||
|
||
|
||
def train(data): | ||
inputs, labels = data[0].to(device=device), data[1].to(device=device) | ||
outputs = model(inputs) | ||
loss = criterion(outputs, labels) | ||
optimizer.zero_grad() | ||
loss.backward() | ||
optimizer.step() | ||
|
||
|
||
with torch.profiler.profile( | ||
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2), | ||
on_trace_ready=torch.profiler.tensorboard_trace_handler( | ||
"/home/envd/log/efficientnet" | ||
), | ||
record_shapes=True, | ||
profile_memory=True, | ||
with_stack=True, | ||
) as prof: | ||
for step, batch_data in enumerate(train_loader): | ||
if step >= (1 + 1 + 3) * 2: | ||
break | ||
train(batch_data) | ||
prof.step() # Need to call this at the end of each step to notify profiler of steps' boundary. | ||
|
||
|
||
prof = torch.profiler.profile( | ||
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2), | ||
on_trace_ready=torch.profiler.tensorboard_trace_handler( | ||
"/home/envd/log/efficientnet" | ||
), | ||
record_shapes=True, | ||
with_stack=True, | ||
) | ||
prof.start() | ||
for step, batch_data in enumerate(train_loader): | ||
if step >= (1 + 1 + 3) * 2: | ||
break | ||
train(batch_data) | ||
prof.step() | ||
prof.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
envdlib = include("https://github.com/tensorchord/envdlib") | ||
|
||
|
||
def build(): | ||
base(os="ubuntu20.04", language="python3") | ||
shell("zsh") | ||
install.cuda(version="11.2.0", cudnn="8") | ||
install.python_packages( | ||
[ | ||
"torch", | ||
"torchvision", | ||
"torch_tb_profiler", | ||
"--extra-index-url https://download.pytorch.org/whl/cu113", | ||
] | ||
) | ||
envdlib.tensorboard(8888, envd_dir="/home/envd/log/efficientnet") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Adopt from https://pytorch.org/tutorials/intermediate/tensorboard_profiler_tutorial.html | ||
import torch | ||
import torch.nn | ||
import torch.optim | ||
import torch.profiler | ||
import torch.utils.data | ||
import torchvision.datasets | ||
import torchvision.models | ||
import torchvision.transforms as T | ||
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights | ||
|
||
transform = T.Compose( | ||
[T.Resize(224), T.ToTensor(), T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] | ||
) | ||
train_set = torchvision.datasets.CIFAR10( | ||
root="./data", train=True, download=True, transform=transform | ||
) | ||
train_loader = torch.utils.data.DataLoader(train_set, batch_size=2, shuffle=True) | ||
|
||
device = torch.device("cuda:0") | ||
model = efficientnet_b0(weights=EfficientNet_B0_Weights.DEFAULT).to(device) | ||
criterion = torch.nn.CrossEntropyLoss().cuda(device) | ||
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9) | ||
model.train() | ||
|
||
|
||
def train(data): | ||
inputs, labels = data[0].to(device=device), data[1].to(device=device) | ||
outputs = model(inputs) | ||
loss = criterion(outputs, labels) | ||
optimizer.zero_grad() | ||
loss.backward() | ||
optimizer.step() | ||
|
||
|
||
with torch.profiler.profile( | ||
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2), | ||
on_trace_ready=torch.profiler.tensorboard_trace_handler( | ||
"/home/envd/log/efficientnet" | ||
), | ||
record_shapes=True, | ||
profile_memory=True, | ||
with_stack=True, | ||
) as prof: | ||
for step, batch_data in enumerate(train_loader): | ||
if step >= (1 + 1 + 3) * 2: | ||
break | ||
train(batch_data) | ||
prof.step() # Need to call this at the end of each step to notify profiler of steps' boundary. | ||
|
||
|
||
prof = torch.profiler.profile( | ||
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2), | ||
on_trace_ready=torch.profiler.tensorboard_trace_handler( | ||
"/home/envd/log/efficientnet" | ||
), | ||
record_shapes=True, | ||
with_stack=True, | ||
) | ||
prof.start() | ||
for step, batch_data in enumerate(train_loader): | ||
if step >= (1 + 1 + 3) * 2: | ||
break | ||
train(batch_data) | ||
prof.step() | ||
prof.stop() |