forked from davidcpage/cifar10-fast
-
Notifications
You must be signed in to change notification settings - Fork 4
/
torch_backend.py
149 lines (118 loc) · 4.6 KB
/
torch_backend.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np
import torch
from torch import nn
import torchvision
from core import build_graph, cat, to_numpy
torch.backends.cudnn.benchmark = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@cat.register(torch.Tensor)
def _(*xs):
return torch.cat(xs)
@to_numpy.register(torch.Tensor)
def _(x):
return x.detach().cpu().numpy()
def warmup_cudnn(model, batch_size):
#run forward and backward pass of the model on a batch of random inputs
#to allow benchmarking of cudnn kernels
batch = {
'input': torch.Tensor(np.random.rand(batch_size,3,32,32)).cuda().half(),
'target': torch.LongTensor(np.random.randint(0,10,batch_size)).cuda()
}
model.train(True)
o = model(batch)
o['loss'].sum().backward()
model.zero_grad()
torch.cuda.synchronize()
#####################
## dataset
#####################
def cifar10(root):
train_set = torchvision.datasets.CIFAR10(root=root, train=True, download=True)
test_set = torchvision.datasets.CIFAR10(root=root, train=False, download=True)
return {
'train': {'data': train_set.train_data, 'labels': train_set.train_labels},
'test': {'data': test_set.test_data, 'labels': test_set.test_labels}
}
#####################
## data loading
#####################
class Batches():
def __init__(self, dataset, batch_size, shuffle, set_random_choices=False, num_workers=0, drop_last=False):
self.dataset = dataset
self.batch_size = batch_size
self.set_random_choices = set_random_choices
self.dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, num_workers=num_workers, pin_memory=True, shuffle=shuffle, drop_last=drop_last
)
def __iter__(self):
if self.set_random_choices:
self.dataset.set_random_choices()
return ({'input': x.to(device).half(), 'target': y.to(device).long()} for (x,y) in self.dataloader)
def __len__(self):
return len(self.dataloader)
#####################
## torch stuff
#####################
class Identity(nn.Module):
def forward(self, x): return x
class Mul(nn.Module):
def __init__(self, weight):
super().__init__()
self.weight = weight
def __call__(self, x):
return x*self.weight
class Flatten(nn.Module):
def forward(self, x): return x.view(x.size(0), x.size(1))
class Add(nn.Module):
def forward(self, x, y): return x + y
class Concat(nn.Module):
def forward(self, *xs): return torch.cat(xs, 1)
class Correct(nn.Module):
def forward(self, classifier, target):
return classifier.max(dim = 1)[1] == target
def batch_norm(num_channels, bn_bias_init=None, bn_bias_freeze=False, bn_weight_init=None, bn_weight_freeze=False):
m = nn.BatchNorm2d(num_channels)
if bn_bias_init is not None:
m.bias.data.fill_(bn_bias_init)
if bn_bias_freeze:
m.bias.requires_grad = False
if bn_weight_init is not None:
m.weight.data.fill_(bn_weight_init)
if bn_weight_freeze:
m.weight.requires_grad = False
return m
class Network(nn.Module):
def __init__(self, net):
self.graph = build_graph(net)
super().__init__()
for n, (v, _) in self.graph.items():
setattr(self, n, v)
def forward(self, inputs):
self.cache = dict(inputs)
for n, (_, i) in self.graph.items():
self.cache[n] = getattr(self, n)(*[self.cache[x] for x in i])
return self.cache
def half(self):
for module in self.children():
if type(module) is not nn.BatchNorm2d:
module.half()
return self
trainable_params = lambda model:filter(lambda p: p.requires_grad, model.parameters())
class TorchOptimiser():
def __init__(self, weights, optimizer, step_number=0, **opt_params):
self.weights = weights
self.step_number = step_number
self.opt_params = opt_params
self._opt = optimizer(weights, **self.param_values())
def param_values(self):
return {k: v(self.step_number) if callable(v) else v for k,v in self.opt_params.items()}
def step(self):
self.step_number += 1
self._opt.param_groups[0].update(**self.param_values())
self._opt.step()
def __repr__(self):
return repr(self._opt)
def SGD(weights, lr=0, momentum=0, weight_decay=0, dampening=0, nesterov=False):
return TorchOptimiser(weights, torch.optim.SGD, lr=lr, momentum=momentum,
weight_decay=weight_decay, dampening=dampening,
nesterov=nesterov)