-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpuProfiler.py
286 lines (249 loc) · 12.5 KB
/
gpuProfiler.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
from torch.optim.lr_scheduler import StepLR
from torch.autograd import Variable
import time
import collections
from typing import Type, Any, Callable, Union, List, Optional
from torch.nn.common_types import _size_1_t, _size_2_t, _size_3_t
import jsonpickle
import json
import torch.cuda.profiler as profiler
import torch.cuda.nvtx as nvtx
import pyprof
class Perf(object):
def __init__(self, eidToStr = {}):
super(Perf, self).__init__()
self.measurements = []
self.sum = []
self.count = []
self.eidToStr = eidToStr
def recordTime(self, eid, elapsedTime):
if eid >= len(self.measurements):
self.measurements += [[]] * (eid - len(self.measurements) + 1)
self.sum += [0.0] * (eid - len(self.sum) + 1)
self.count += [0] * (eid - len(self.count) + 1)
self.measurements[eid].append(elapsedTime)
self.sum[eid] += elapsedTime
self.count[eid] += 1
def printStats(self):
# maxEventStrLen = max([len(eventStr) for eventStr in self.eidToStr.values()])
for eid in range(len(self.measurements)):
if self.count[eid] == 0:
continue
median = sorted(self.measurements[eid])[int(len(self.measurements[eid]) / 2)]
if eid in self.eidToStr:
print("Event %15s ==> avg: %8.1f us, median: %8.1f us" % (self.eidToStr[eid], self.sum[eid] / self.count[eid], median))
else:
print("Event %5d ==> avg: %8.1f us, median: %8.1f us" % (eid, self.sum[eid] / self.count[eid], median))
def getStat(self, eid):
return sorted(self.measurements[eid])[int(len(self.measurements[eid]) / 2)]
# return self.sum[eid] / self.count[eid]
def printHeader(self):
print("#BatchSize", end = "")
print(" width", end = "")
print(" filters", end = "")
print(" mults", end = "")
print(" | AVG : ", end = "")
for eid in range(len(self.measurements)):
if eid in self.eidToStr:
print("%10s" % self.eidToStr[eid], end = "")
else:
print("Event %4d" % eid, end = "")
print(" |Median: ", end = "")
for eid in range(len(self.measurements)):
if eid in self.eidToStr:
print("%10s" % self.eidToStr[eid], end = "")
else:
print("Event %4d" % eid, end = "")
print(" | Accuracy", end = "")
print(" | Count(eid0)")
def printAll(self, batchSize, width, filterCount, accuracy):
# Avg.
print("%9d " % batchSize, end = "")
print("%9d " % width, end = "")
print("%9d " % filterCount, end = "")
print("%11d " % (batchSize * width * width * filterCount * 9 * 3), end = "")
print("%10s"%"", end = "")
for eid in range(len(self.measurements)):
if self.count[eid] == 0:
continue
print("%10.1f" % (self.sum[eid] / self.count[eid]), end = "")
print(" %9s"%"", end = "")
for eid in range(len(self.measurements)):
if self.count[eid] == 0:
continue
median = sorted(self.measurements[eid])[int(len(self.measurements[eid]) / 2)]
print("%10.1f" % median, end = "")
print(" %9.2f" % accuracy, end = "")
print(" %10d" % len(self.measurements[0]))
class GpuProfiler:
def __init__(self, device):
self.conv2dBenchCache = {}
self.benchCacheHit = 0
self.benchCacheMiss = 0
self.linearBenchCache = {}
self.device = device
def saveProfile(self, path = "gpuProfile.json"):
with open(path, "w") as outfile:
data = {"conv2dBenchCache": self.conv2dBenchCache, "linearBenchCache": self.linearBenchCache}
planInJson = jsonpickle.encode(data, unpicklable=False)
json.dump(json.loads(planInJson), outfile, indent=2, sort_keys=False)
print("[GpuProfiler] Saved %d entries." % (len(self.conv2dBenchCache) + len(self.linearBenchCache)))
if (self.benchCacheHit + self.benchCacheMiss) > 0:
print("[GpuProfiler] Cache hit %3.1f %%" % (100 * self.benchCacheHit / (self.benchCacheHit + self.benchCacheMiss)))
def loadProfile(self, path = "gpuProfile.json"):
try:
with open(path) as f:
data = json.load(f)
if "conv2dBenchCache" in data:
self.conv2dBenchCache = data["conv2dBenchCache"]
if "linearBenchCache" in data:
self.linearBenchCache = data["linearBenchCache"]
except IOError:
print("[GpuProfiler] No profile file exists at %s." % path)
def train(self, model, device, train_loader, criterion, optimizer, epoch, perf, profile=False):
model.train()
iter_to_capture_start = 50
iter_to_capture_end = 53
with torch.autograd.profiler.emit_nvtx():
iterationCount = 0
for batch_idx, (data, target) in enumerate(train_loader):
start_time = time.time()
ev_zero = torch.cuda.Event(enable_timing=True)
ev_fp = torch.cuda.Event(enable_timing=True)
ev_loss = torch.cuda.Event(enable_timing=True)
ev_bp = torch.cuda.Event(enable_timing=True)
ev_opt = torch.cuda.Event(enable_timing=True)
# if profile and iterationCount == iter_to_capture_start:
# print("profiler started.")
# profiler.start()
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
torch.cuda.synchronize()
ev_zero.record()
output = model(data)
ev_fp.record()
output = torch.flatten(output, 1)
output = F.log_softmax(output, dim=1)
loss = criterion(output, target)
# bp_start = time.time()
# torch.cuda.synchronize()
ev_loss.record()
# loss.backward()
output.backward(output)
ev_bp.record()
# torch.cuda.synchronize()
# bpTime = (time.time() - bp_start) * 1E6
optimizer.step()
ev_opt.record()
# if profile and iterationCount == iter_to_capture_end:
# print("profiler ended.")
# profiler.stop()
# ev_bp.synchronize()
ev_opt.synchronize()
stop_time = time.time()
# perf.recordTime(0, 1000 * ev_start.elapsed_time(ev_load))
# perf.recordTime(1, 1000 * ev_load.elapsed_time(ev_zero))
perf.recordTime(2, 1000 * ev_zero.elapsed_time(ev_fp))
# perf.recordTime(3, 1000 * ev_fp.elapsed_time(ev_loss))
perf.recordTime(4, 1000 * ev_loss.elapsed_time(ev_bp))
# perf.recordTime(4, bpTime)
# perf.recordTime(4, 1000 * ev_fp.elapsed_time(ev_bp))
# perf.recordTime(5, 1000 * ev_bp.elapsed_time(ev_opt))
perf.recordTime(6, 1000 * ev_zero.elapsed_time(ev_opt))
perf.recordTime(7, (stop_time - start_time) * 1000 * 1000)
iterationCount += 1
def benchModel(self, model, inputSize, batchSize, profile=False):
train_dataset = self.SyntheticDataset(inputSize, batchSize * 1000, 1000) # 30) #
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batchSize, shuffle=False, pin_memory=True, drop_last=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
criterion = nn.CrossEntropyLoss().cuda(self.device)
perfStat = Perf({0: 'load', 1: 'zero', 2: 'fp', 3: 'loss', 4: 'bp', 5: 'opt', 6: 'total/bat', 7: 'totalCPU'})
self.train(model.cuda(), self.device, train_loader, criterion, optimizer, 1, perfStat, profile)
# gpuTime = perfStat.getStat(2) + perfStat.getStat(4)
gpuTime = perfStat.getStat(6) # Includes loss and optimizer.
if profile:
print("%.f + %.f" % (perfStat.getStat(2), perfStat.getStat(4)))
return gpuTime, perfStat.getStat(2), perfStat.getStat(4)
def runConv2dBench(self, config, params, profile=False):
if str((config, params)) in self.conv2dBenchCache and profile == False:
self.benchCacheHit += 1
return self.conv2dBenchCache[str((config, params))]
self.benchCacheMiss += 1
batchSize = config[0]
width = config[1]
height = config[2]
inChannels = config[3]
filterCount = config[4]
train_dataset = self.SyntheticDataset((inChannels, width, height), batchSize * 100, 100) #
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batchSize, shuffle=False, pin_memory=True, drop_last=True)
newParams = params.copy()
newParams["in_channels"] = inChannels
newParams["out_channels"] = filterCount
# model = self.Conv2dOp(inChannels, filterCount).to(self.device)
model = nn.Conv2d(**newParams).to(self.device)
# optimizer = torch.optim.Adadelta(model.parameters())
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
criterion = nn.CrossEntropyLoss().cuda(self.device)
perfStat = Perf({0: 'load', 1: 'zero', 2: 'fp', 3: 'loss', 4: 'bp', 5: 'opt', 6: 'total/bat', 7: 'totalCPU'})
# scheduler = StepLR(optimizer, step_size=1)
self.train(model, self.device, train_loader, criterion, optimizer, 1, perfStat, profile)
# scheduler.step()
gpuTime = perfStat.getStat(2) + perfStat.getStat(4)
if profile:
print("%.f + %.f" % (perfStat.getStat(2), perfStat.getStat(4)))
self.conv2dBenchCache[str((config, params))] = gpuTime
return gpuTime
def runLinearBench(self, config, profile=False):
if str(config) in self.linearBenchCache and profile == False:
self.benchCacheHit += 1
return self.linearBenchCache[str(config)]
self.benchCacheMiss += 1
batchSize = config[0]
inFeatures = config[1]
outFeatures = config[2]
train_dataset = self.SyntheticDataset((inFeatures), batchSize * 100, num_classes=outFeatures)
# train_dataset = self.SyntheticDataset((inFeatures), batchSize * 30, num_classes=outFeatures)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batchSize, shuffle=False, pin_memory=True, drop_last=True)
model = self.LinearOp(inFeatures, outFeatures).to(self.device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
criterion = nn.CrossEntropyLoss().cuda(self.device)
perfStat = Perf({0: 'load', 1: 'zero', 2: 'fp', 3: 'loss', 4: 'bp', 5: 'opt', 6: 'total/bat', 7: 'totalCPU'})
scheduler = StepLR(optimizer, step_size=1)
self.train(model, self.device, train_loader, criterion, optimizer, 1, perfStat, profile)
# scheduler.step()
if profile:
print("%.f + %.f" % (perfStat.getStat(2), perfStat.getStat(4)))
gpuTime = perfStat.getStat(2) + perfStat.getStat(4)
self.linearBenchCache[str(config)] = gpuTime
return gpuTime
class SyntheticDataset(torch.utils.data.dataset.Dataset):
def __init__(self, input_size, length, num_classes=1000):
self.tensor = Variable(torch.rand(input_size)).type(torch.FloatTensor)
self.target = torch.Tensor(1).random_(0, num_classes)[0].type(torch.LongTensor)
self.length = length
def __getitem__(self, index):
return self.tensor, self.target
def __len__(self):
return self.length
# class Conv2dOp(nn.Module):
# def __init__(self, inChannels, filterCount, num_classes=1000):
# super(GpuProfiler.Conv2dOp, self).__init__()
# self.num_classes = num_classes
# self.conv1 = nn.Conv2d(inChannels, filterCount, (3, 3), (1, 1), (1, 1))
# def forward(self, x):
# x = self.conv1(x)
# return x
class LinearOp(nn.Module):
def __init__(self, inFeatures, outFeatures):
super(GpuProfiler.LinearOp, self).__init__()
self.linear1 = nn.Linear(inFeatures, outFeatures)
def forward(self, x):
x = self.linear1(x)
return x