-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.py
66 lines (51 loc) · 1.67 KB
/
test.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
from __future__ import print_function
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import time
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--epochs', type=int, default=1, metavar='N',
help='number of epochs to train (default: 1)')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--mixf', action='store_true', default=False,
help='enables using mixed float precision')
args = parser.parse_args()
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(2048, 2048, kernel_size=1)
def forward(self, x):
x = F.relu(self.conv1(x))
return x
model = Net()
if args.mixf:
model.cuda().half()
else:
model.cuda()
ITERS = 300
def train(epoch):
model.train()
# dummy dataset the same size as imagenet
data_ = torch.FloatTensor(np.random.randn(4096, 2048, 1, 1))
#lets get copy time out of conv time:
if args.mixf:
data = data_.cuda().half()
else:
data = data_.cuda()
#time the entire thing, with proper cuda synchronization
torch.cuda.synchronize()
start = time.time()
for batch_idx in range(ITERS):
output = model(Variable(data))
torch.cuda.synchronize()
print("Time / iteration: ", (time.time()-start)/ITERS)
for epoch in range(1, args.epochs + 1):
train(epoch)