-
Notifications
You must be signed in to change notification settings - Fork 4
/
conv_dense.py
55 lines (46 loc) · 1.44 KB
/
conv_dense.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
#!/usr/bin/env python
import torch
import torch.nn as nn
import torch.autograd.Variable as Variable
import sys
import time
# cudnn is tooooo fast!! it has too many in house optimization. we use cublas as baseline
torch.backends.cudnn.enabled = False
# run for 100 times
def profile(bs, iw, ih, ic, oc, kw, kh, times=100):
conv = nn.Conv2d(ic, oc, kernel_size=(kw, kh), stride=(1, 1), padding=(kw/2, kh/2)).cuda()
x = Variable(torch.randn(bs, ic, iw, ih)).cuda()
# run once to prevent initialization overhead
y = conv(x)
#profile
torch.cuda.synchronize()
begin = time.time()
for i in range(times):
y = conv(x)
torch.cuda.synchronize()
end = time.time()
return (end - begin) / times
vgg16_config = {
"conv1.1": [224, 224, 3, 64, 3, 3],
"conv1.2": [224, 224, 64, 64, 3, 3],
"conv2.1": [112, 112, 64, 128, 3, 3],
"conv2.2": [112, 112, 128, 128, 3, 3],
"conv3.1": [56, 56, 128, 256, 3, 3],
"conv3.2": [56, 56, 256, 256, 3, 3],
"conv3.3": [56, 56, 256, 256, 3, 3],
"conv4.1": [28, 28, 256, 512, 3, 3],
"conv4.2": [28, 28, 512, 512, 3, 3],
"conv4.3": [28, 28, 512, 512, 3, 3],
"conv5.1": [14, 14, 512, 512, 3, 3],
"conv5.2": [14, 14, 512, 512, 3, 3],
"conv5.3": [14, 14, 512, 512, 3, 3]
}
def main():
# batch size
bs = 64
with open("conv_dense.csv", "w") as f:
f.write("layer, time\n")
for k, (iw, ih, ic, oc, kw, kh) in vgg16_config:
f.write("%s, %f\n" % (k, profile(bs, iw, ih, ic, oc, kw, kh)))
if __name__ == "__main__":
main()