forked from yqhu/profiler-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_ofi_prof.py
121 lines (99 loc) · 3.93 KB
/
mnist_ofi_prof.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Based on: https://github.com/pytorch/examples/blob/master/mnist/main.py
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
import time
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.bn1 = torch.nn.BatchNorm2d(32)
self.bn2 = torch.nn.BatchNorm2d(64)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
with torch.profiler.record_function("conv1"):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x)
with torch.profiler.record_function("conv2"):
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x)
with torch.profiler.record_function("head"):
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
def test(model, test_loader, prof=None):
with torch.inference_mode():
for data, target in test_loader:
output = model(data)
if prof:
prof.step()
def main():
test_kwargs = {'batch_size': 64, 'num_workers': 4}
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
test_dataset = datasets.MNIST('../data', train=False,
transform=transform)
test_loader = torch.utils.data.DataLoader(test_dataset, **test_kwargs)
# eager mode
model = Net().eval()
# warm up
test(model, test_loader)
start = time.perf_counter()
test(model, test_loader)
print(f'eager mode: {1000 * (time.perf_counter() - start):.1f} ms')
with torch.profiler.profile(activities=[torch.profiler.ProfilerActivity.CPU],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler('mnist-eager'),
profile_memory=True,
with_stack=True,
record_shapes=True) as prof:
test(model, test_loader, prof=prof)
# jit
model = Net().eval()
model = torch.jit.script(model)
# warm up
test(model, test_loader)
start = time.perf_counter()
test(model, test_loader)
print(f'jit: {1000 * (time.perf_counter() - start):.1f} ms')
with torch.profiler.profile(activities=[torch.profiler.ProfilerActivity.CPU],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler('mnist-jit'),
profile_memory=True,
with_stack=True,
record_shapes=True) as prof:
test(model, test_loader, prof=prof)
# ofi
model = Net().eval()
model = torch.jit.script(model)
model = torch.jit.optimize_for_inference(model.eval())
# warm up
test(model, test_loader)
start = time.perf_counter()
test(model, test_loader)
print(f'ofi: {1000 * (time.perf_counter() - start):.1f} ms')
with torch.profiler.profile(activities=[torch.profiler.ProfilerActivity.CPU],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler('mnist-ofi'),
profile_memory=True,
with_stack=True,
record_shapes=True) as prof:
test(model, test_loader, prof=prof)
if __name__ == '__main__':
main()