Repository navigation
Expand file tree
/
Copy pathprofiling.py
More file actions
169 lines (147 loc) · 5.93 KB
/
Copy pathprofiling.py
File metadata and controls
169 lines (147 loc) · 5.93 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'''
*
* SIDCo - Efficient Statistical-based Compression Technique for Distributed ML.
*
* Author: Ahmed Mohamed Abdelmoniem Sayed, <ahmedcs982@gmail.com, github:ahmedcs>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of CRAPL LICENCE avaliable at
* http://matt.might.net/articles/crapl/
* http://matt.might.net/articles/crapl/CRAPL-LICENSE.txt
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the CRAPL LICENSE for more details.
*
* Please READ carefully the attached README and LICENCE file with this software
*
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
import time
import numpy as np
import torch.nn as nn
from torch.autograd import Variable
import models.lstm as lstmpy
class Profiling(object):
def __init__(self, model):
if isinstance(model, torch.nn.Module) is False:
raise ValueError("Not a valid model, please provide a 'nn.Module' instance.")
self.model = model
self._parameter_names = {v: k for k, v
in model.named_parameters()}
self._seq_keys = [k for k, v in model.named_parameters()]
self._backward_seq_keys = []
self._backward_key_sizes = []
self._grad_accs = []
self._handles = {}
self.hook_done = False
self._start = time.time()
self._register_hooks()
self._is_profiling = False
def _register_hooks(self):
for name, p in self.model.named_parameters():
p.register_hook(self._make_hook(name, p))
def _make_hook(self, name, p):
def hook(*ignore):
if not self._is_profiling:
return
name = self._parameter_names.get(p)
if len(self._backward_seq_keys) != len(self._seq_keys):
self._backward_seq_keys.append(name)
self._backward_key_sizes.append(p.numel())
if name not in self._handles:
self._handles[name] = []
torch.cuda.synchronize()
ct = self._timestamp(name)
self._handles[name].append(ct - self._start)
return hook
def reset_start(self):
self._start = time.time()
def reset(self):
self._start = time.time()
self._handles.clear()
def stop(self):
self._is_profiling = False
def start(self):
self._is_profiling = True
self._start = time.time()
def get_backward_seq_keys(self):
return self._backward_seq_keys
def get_backward_key_sizes(self):
return self._backward_key_sizes
def get_layerwise_times(self):
num_trials = len(self._handles[self._seq_keys[0]])
layerwise_times_multipletest = []
totals = []
for j in range(num_trials):
s = 0
total = 0.0
layerwise_times = [] # from the last layer to the first layer
#for i, k in enumerate(self._seq_keys[::-1]):
for i, k in enumerate(self._backward_seq_keys):
t = self._handles[k][j]
#print('name: ', k, ' diff: ', t-s)
layerwise_times.append(t-s)
total += (t-s)
s = total
layerwise_times_multipletest.append(layerwise_times)
totals.append(total)
array = np.array(layerwise_times_multipletest)
layerwise_times = np.mean(array, axis=0)
return layerwise_times, np.mean(totals)
def _timestamp(self, name):
return time.time()
def benchmark(trainer):
# Benchmark to achieve the backward time per layer
p = Profiling(trainer.net)
# Warmup
input_shape, output_shape = trainer.get_data_shape()
warmup = 5 # warmup should be 0 on some GPUs (e.g., P102-100)
iteration = 50
for i in range(iteration+warmup):
data = trainer.data_iter()
if trainer.dataset == 'an4':
inputs, labels_cpu, input_percentages, target_sizes = data
input_sizes = input_percentages.mul_(int(inputs.size(3))).int()
else:
inputs, labels_cpu = data
if trainer.is_cuda:
if trainer.dnn == 'lstm' :
inputs = Variable(inputs.transpose(0, 1).contiguous()).cuda()
labels = Variable(labels_cpu.transpose(0, 1).contiguous()).cuda()
else:
inputs, labels = inputs.cuda(non_blocking=True), labels_cpu.cuda(non_blocking=True)
else:
labels = labels_cpu
if trainer.dnn == 'lstman4':
out, output_sizes = trainer.net(inputs, input_sizes)
out = out.transpose(0, 1) # TxNxH
loss = trainer.criterion(out, labels_cpu, output_sizes, target_sizes)
torch.cuda.synchronize()
loss = loss / inputs.size(0) # average the loss by minibatch
elif trainer.dnn == 'lstm' :
hidden = trainer.net.init_hidden()
hidden = lstmpy.repackage_hidden(hidden)
#print(inputs.size(), hidden[0].size(), hidden[1].size())
outputs, hidden = trainer.net(inputs, hidden)
tt = torch.squeeze(labels.view(-1, trainer.net.batch_size * trainer.net.num_steps))
loss = trainer.criterion(outputs.view(-1, trainer.net.vocab_size), tt)
torch.cuda.synchronize()
else:
# forward + backward + optimize
outputs = trainer.net(inputs)
loss = trainer.criterion(outputs, labels)
torch.cuda.synchronize()
if i >= warmup:
p.start()
loss.backward()
if trainer.is_cuda:
torch.cuda.synchronize()
layerwise_times, sum_total = p.get_layerwise_times()
seq_keys = p.get_backward_seq_keys()
p.stop()
return seq_keys[::-1], layerwise_times[::-1], p.get_backward_key_sizes()[::-1]