forked from torch/cunn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataParallelTable.lua
516 lines (435 loc) · 16.6 KB
/
DataParallelTable.lua
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
local gpuLocalCopyBuffers = {}
local baseGpuIndex = 1 -- A constant
-- *****************************************************************************
-- Helper Functions
-- *****************************************************************************
-- queryGPUDeviceId - Function to query a tensor or table for the
-- GPUID. For tables we will search the table for CudaTensors, query their
-- device and make sure the deviceIds of ALL CudaTensors are on the same GPU.
local function queryGPUDeviceId(object)
if torch.type(object) == 'torch.CudaTensor' then
return object:getDevice()
end
local deviceId
-- Try finding a parameter
local stack = {} -- explicit stack to recurse on tables
for key, param in pairs(object) do
if key ~= 'modules' then
stack[#stack+1] = param -- Push onto the stack
end
end
while #stack > 0 do
local param = stack[#stack]; stack[#stack] = nil -- Pop the stack
if (torch.type(param) == 'table') then
for i = 1, #param do stack[#stack+1] = param[i] end -- Push onto stack
elseif (torch.type(param) == 'torch.CudaTensor') then
if (torch.numel(param) > 0) then
-- Empty tensors are always on GPU "0"
local curId = param:getDevice()
if deviceId == nil then
deviceId = curId
else
assert(deviceId == curId,
'Found CudaTensor instances from different devices')
end
end
end
end
return deviceId
end
-- Get an avaliable GPU buffer for asyncGPUCopy. It is used when the GPU tensor
-- is not contiguous.
local function getBuffer()
local device = cutorch.getDevice()
if not gpuLocalCopyBuffers[device] then
gpuLocalCopyBuffers[device] = torch.CudaTensor()
end
return gpuLocalCopyBuffers[device]
end
-- setDeviceSafe - Avoid redundant calls to setDevice
local function setDevice(gpuid)
if (cutorch.getDevice() ~= gpuid) then
cutorch.setDevice(gpuid)
end
end
-- Asynchronous copy from source to dest from GPU to GPU.
-- This is borrowed (with modifications) from fbcunn.
local function asyncGPUCopy(dest, source)
assert(torch.typename(dest) == 'torch.CudaTensor')
assert(torch.typename(source) == 'torch.CudaTensor')
local prevDevice = cutorch.getDevice()
local destGpuid = dest:getDevice()
local sourceGpuid = source:getDevice()
if sourceGpuid == destGpuid then
-- if both tensors are on the same gpu normal operation works
setDevice(destGpuid)
dest:copy(source)
setDevice(prevDevice)
return
end
if source:isContiguous() and dest:isContiguous() then
-- if both tensors are contiguous operation across gpus works
setDevice(destGpuid)
dest:copy(source)
setDevice(prevDevice)
return
end
-- Either the dest or the source are not contiguous. we will need to do
-- intermediate copies.
local tmpSource = source
if not source:isContiguous() then
setDevice(sourceGpuid)
tmpSource = getBuffer()
tmpSource:resizeAs(source)
tmpSource:copy(source) -- Make contiguous using a copy
end
setDevice(destGpuid)
local tmpDest = dest
if not dest:isContiguous() then
local tmpDest = getBuffer()
tmpDest:resizeAs(tmpSource)
tmpDest:copy(tmpSource)
end
dest:copy(tmpDest)
cutorch.synchronize() -- Ensures we keep the buffer for the copy duration
-- Put the device back to what it was.
setDevice(prevDevice)
end
local function equalSize(sizeTable1, sizeTable2)
if (#sizeTable1 ~= #sizeTable2) then
return false
end
for i = 1, #sizeTable1 do
if sizeTable1[i] ~= sizeTable2[i] then return false end
end
return true
end
-- deepTensorsCopy - perform an elementwise copy of the tensors in the nested
-- table. We assume that the tables are properly initialized (ie same size and
-- structure), although we will assert it.
local function deepTensorsCopy(dst, src)
if (torch.type(src) == 'table') then
assert(torch.type(dst) == 'table' and #src == #dst)
for i = 1, #src do deepTensorsCopy(dst[i], src[i]) end
elseif torch.type(src):find('torch%..+Tensor') then
assert(torch.type(dst):find('torch%..+Tensor'))
assert(dst:isSameSizeAs(src))
asyncGPUCopy(dst, src)
else
error('input must be a nested table of tensors!')
end
end
-- deepTensorsAdd - perform an elementwise add of the tensors in the nested
-- table. We assume that the tables are properly initialized (ie same size and
-- structure), although we will assert it.
--
-- Note: this is necessary because add() will malloc new memory on the cuda
-- driver side every time we want to get new memory! Therefore, we actually
-- need to copy src to the dst gpu
local function deepTensorsAdd(dst, src)
if (torch.type(src) == 'table') then
assert(torch.type(dst) == 'table' and #src == #dst)
for i = 1, #src do deepTensorsAdd(dst[i], src[i]) end
elseif torch.type(src):find('torch%..+Tensor') then
assert(torch.type(dst):find('torch%..+Tensor'))
assert(dst:isSameSizeAs(src))
local dstGpuid = dst:getDevice()
local srcGpuid = src:getDevice()
local curGpuid = cutorch:getDevice()
setDevice(dstGpuid)
-- Copy src over to a buffer on the dst GPU
local srcBufferOnDstGpu = src
if (dstGpuid ~= srcGpuid) then
srcBufferOnDstGpu = getBuffer()
srcBufferOnDstGpu:resizeAs(src)
assert(src:isContiguous())
srcBufferOnDstGpu:copy(src)
end
-- Perform the actual add
dst:add(srcBufferOnDstGpu)
if (dstGpuid ~= srcGpuid) then
-- Ensures we get to keep the buffer for the duration of the add
cutorch.synchronize()
end
setDevice(curGpuid) -- Put the GPU id back to what it was
else
error('input must be a nested table of tensors!')
end
end
-- *****************************************************************************
-- DataParallelTable
-- *****************************************************************************
local DataParallelTable, parent = torch.class('nn.DataParallelTable',
'nn.Container')
function DataParallelTable:__init(dimension)
parent.__init(self)
if not dimension then
error "must specify a dimension!"
end
self.dimension = dimension
self.modules = {}
self.gpuAssignments = {} -- Which gpuid each module sits on
self.inputGpu = {} -- inputs for each gpu
self.gradOutputGpu = {} -- gradOutputs for each gpu
self.outputGpu = {} -- outputs for each gpu
self.gradInputGpu = {} -- gradInput for each gpu
end
-- NOTE: The input should be on the FIRST added GPU device, and similarly the
-- output will be on the FIRST GPU device.
function DataParallelTable:add(module, gpuid)
assert(gpuid <= cutorch.getDeviceCount() and gpuid >= 1)
assert(#self.modules == #self.gpuAssignments)
self.modules[#self.modules + 1] = module
self.gpuAssignments[#self.gpuAssignments + 1] = gpuid
return self
end
function DataParallelTable:__tostring()
return 'DataParallelTable: ' .. #self.modules .. ' x ' .. tostring(self.modules[1])
end
function DataParallelTable:get(index)
return self.modules[index]
end
function DataParallelTable:updateOutput(input)
local baseGpuid = self.gpuAssignments[baseGpuIndex]
assert(queryGPUDeviceId(input) == baseGpuid, 'Input is not on gpu ' ..
baseGpuid)
local prevGpuid = cutorch.getDevice()
-- distribute the input to GPUs
for i = 1, #self.modules do
local gpuid = self.gpuAssignments[i]
-- Split the tensors in the input nested table to the GPU with gpuid
-- _distributeTensorRecursive(src,dst,srcGpuid,srcInd,dstGpuid,dstInd)
self.inputGpu[gpuid] = self:_distributeTensorRecursive(
input, self.inputGpu[gpuid],
baseGpuid, baseGpuIndex, gpuid, i,
#self.modules
)
end
cutorch.synchronize()
-- update output for each module asynchronously
for i, module in ipairs(self.modules) do
local gpuid = self.gpuAssignments[i]
setDevice(gpuid)
self.outputGpu[gpuid] = module:updateOutput(self.inputGpu[gpuid])
end
cutorch.synchronize()
-- concatenate the outputs to the base GPU
for i = 1, #self.modules do
local gpuid = self.gpuAssignments[i]
-- Merge the tensors in the input nested table to the GPU with gpuid
-- _concatTensorRecursive(src,dst,srcGpuid,srcInd,dstGpuid,dstInd)
self.output = self:_concatTensorRecursive(
self.outputGpu[gpuid], self.output,
gpuid, i, baseGpuid, baseGpuIndex,
#self.modules
)
end
setDevice(prevGpuid)
return self.output
end
function DataParallelTable:updateGradInput(input, gradOutput)
-- We assume that updateOutput has already been called (therefore inputGpu
-- has been populated)
local baseGpuid = self.gpuAssignments[baseGpuIndex]
assert(queryGPUDeviceId(gradOutput) == baseGpuid,
'gradOutput is not on gpu ' .. baseGpuid)
local prevGpuid = cutorch.getDevice()
-- distribute the gradOutput to GPUs
for i = 1, #self.modules do
local gpuid = self.gpuAssignments[i]
-- Split the tensors in the input nested table to the GPU with gpuid
-- _distributeTensorRecursive(src,dst,srcGpuid,srcInd,dstGpuid,dstInd)
self.gradOutputGpu[gpuid] = self:_distributeTensorRecursive(gradOutput,
self.gradOutputGpu[gpuid], baseGpuid, baseGpuIndex, gpuid, i, #self.modules)
end
cutorch.synchronize()
-- update gradInput for each module asynchronously
for i, module in ipairs(self.modules) do
local gpuid = self.gpuAssignments[i]
setDevice(gpuid)
self.gradInputGpu[gpuid] = module:updateGradInput(self.inputGpu[gpuid],
self.gradOutputGpu[gpuid])
end
cutorch.synchronize()
-- concatenate the outputs to the base GPU
for i = 1, #self.modules do
local gpuid = self.gpuAssignments[i]
-- Merge the tensors in the input nested table to the GPU with gpuid
-- _concatTensorRecursive(src,dst,srcGpuid,srcInd,dstGpuid,dstInd)
self.gradInput = self:_concatTensorRecursive(self.gradInputGpu[gpuid],
self.gradInput, gpuid, i, baseGpuid, baseGpuIndex, #self.modules)
end
cutorch.synchronize()
setDevice(prevGpuid)
return self.gradInput
end
function DataParallelTable:accGradParameters(input, gradOutput, scale)
-- We assume updateGradInput has already been called (so gradOutput has
-- already been populated)
local prevGpuid = cutorch.getDevice()
local baseGpuid = self.gpuAssignments[baseGpuIndex]
scale = scale or 1
-- Calculate the gradWeight + gradBias on each sub-module
for i, module in ipairs(self.modules) do
local gpuid = self.gpuAssignments[i]
setDevice(gpuid)
module:accGradParameters(self.inputGpu[gpuid], self.gradOutputGpu[gpuid],
scale)
end
cutorch.synchronize() -- We have to wait until accGradParameters has finished
-- Accumulate the gradients onto one GPU (the first one)
-- TODO: Parallelize this (ie a parallel merge)
local baseParams, baseGradParams = self.modules[baseGpuIndex]:parameters()
for i, module in ipairs(self.modules) do
if (i ~= baseGpuIndex) then
local params, gradParams = self.modules[i]:parameters()
deepTensorsAdd(baseGradParams, gradParams) -- dst, src
cutorch.synchronize()
end
end
setDevice(prevGpuid)
end
function DataParallelTable:syncParameters()
local prevGpuid = cutorch.getDevice()
local baseParams, baseGradParams = self.modules[baseGpuIndex]:parameters()
-- TODO: Parallelize this (ie a parallel copy)
for i, module in ipairs(self.modules) do
if (i ~= baseGpuIndex) then
local params, gradParams = self.modules[i]:parameters()
deepTensorsCopy(params, baseParams) -- dst, src
end
end
cutorch.synchronize()
setDevice(prevGpuid)
end
-- For compatability with nn.Optim from fbcunn
function DataParallelTable:MixGrads()
self:syncParameters()
end
function DataParallelTable:accUpdateGradParameters(input, gradOutput, lr)
error("accUpdateGradParameters not supported for DataParallelTable.")
end
function DataParallelTable:zeroGradParameters()
local prevGpuid = cutorch.getDevice()
for i, module in ipairs(self.modules) do
setDevice(self.gpuAssignments[i])
module:zeroGradParameters()
end
setDevice(prevGpuid)
end
function DataParallelTable:updateParameters(learningRate)
error("updateParameters not supported for DataParallelTable.")
end
function DataParallelTable:parameters()
local prevGpuid = cutorch.getDevice()
setDevice(self.gpuAssignments[1])
local ret = {self.modules[1]:parameters()}
setDevice(prevGpuid)
return unpack(ret)
end
function DataParallelTable:share(mlp,...)
error("Share not supported for DataParallelTable.")
end
function DataParallelTable:clone()
error("clone not supported for DataParallelTable.")
end
function DataParallelTable:reset(stdv)
local prevGpuid = cutorch.getDevice()
for i, module in ipairs(self.modules) do
setDevice(self.gpuAssignments[i])
module:reset(stdv)
end
setDevice(prevGpuid)
end
function DataParallelTable:name()
return 'DataParallelTable'
end
function DataParallelTable:type(typeStr)
error("type() not supported for DataParallelTable.")
end
function DataParallelTable:_calculateSliceRange(tensor, id, total)
local outerDim = tensor:size(self.dimension)
local eltsPerMod = torch.round( outerDim / #self.modules )
local rangeStart = (id - 1) * eltsPerMod + 1
local rangeEnd = rangeStart + eltsPerMod - 1
if id == total then
rangeEnd = outerDim
end
self.batchSize = outerDim -- TODO: this is a hack to propagate batchSize to line 494
-- but might not be generic enough
return {rangeStart, rangeEnd}
end
-- _distributeTensorRecursive - if the src is a tensor then the function slices
-- it long self.dimension and copies each portion into each child module.
-- Otherwise it does a recursive call on tables.
function DataParallelTable:_distributeTensorRecursive(src, dst,
srcGpuid, srcIndex, dstGpuid, dstIndex, nModules)
if (torch.type(src) == 'table') then
if torch.type(dst) ~= 'table' or #src ~= #dst then
dst = {}
end
-- Recurse on the table
for i = 1, #src do
dst[i] = self:_distributeTensorRecursive(src[i], dst[i], srcGpuid,
srcIndex, dstGpuid, dstIndex, nModules)
end
elseif torch.type(src):find('torch%..+Tensor') then
if (dst == nil or torch.type(dst) ~= 'torch.CudaTensor') then
-- Allocate only on startup or when input table structure changes.
-- Otherwise we will just resize the tensor below.
setDevice(dstGpuid)
dst = torch.CudaTensor()
end
-- Split the tensor
assert(torch.typename(src) == 'torch.CudaTensor')
local slice = src[{self:_calculateSliceRange(src, dstIndex, nModules)}]
if not dst:isSameSizeAs(slice) then
setDevice(dstGpuid)
dst:resizeAs(slice)
end
asyncGPUCopy(dst, slice) -- dst, src
else
error('input must be a nested table of tensors!')
end
return dst
end
-- _concatTensorRecursive - if the src is a tensor then the function copies it
-- into the dst slice along self.dimension.
-- Otherwise it does a recursive call on tables.
function DataParallelTable:_concatTensorRecursive(src, dst, srcGpuid,
srcIndex, dstGpuid, dstIndex, nModules)
if (torch.type(src) == 'table') then
if torch.type(dst) ~= 'table' or #src ~= #dst then
dst = {}
end
-- Recurse on the table
for i = 1, #src do
dst[i] = self:_concatTensorRecursive(src[i], dst[i], srcGpuid,
srcIndex, dstGpuid, dstIndex, nModules)
end
elseif torch.type(src):find('torch%..+Tensor') then
if (dst == nil or torch.type(dst) ~= 'torch.CudaTensor') then
-- Allocate only on startup or when input table structure changes.
-- Otherwise we will just resize the tensor below.
setDevice(dstGpuid)
dst = torch.CudaTensor()
end
if (torch.numel(src) > 0) then
-- Some modules return empty gradInputs if they don't actually return
-- anything.
local dstSize = src:size():totable()
dstSize[self.dimension] = self.batchSize
if not (equalSize(dst:size():totable(), dstSize)) then
setDevice(dstGpuid)
dst:resize(unpack(dstSize))
end
-- Split the tensor
assert(torch.typename(src) == 'torch.CudaTensor')
local slice = dst[{ self:_calculateSliceRange(dst, srcIndex, nModules) }]
asyncGPUCopy(slice, src) -- dst, src
end
else
error('input must be a nested table of tensors!')
end
return dst
end