-
Notifications
You must be signed in to change notification settings - Fork 13
/
MultiCudaTensor.lua
306 lines (261 loc) · 8.73 KB
/
MultiCudaTensor.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
------------------------------------------------------------------------
--[[ MultiCudaTensor ]]--
-- This experimental tensor is used by the NCEModule in dpnn to
-- distribute weight/gradWeight over 2 gpus.
-- The MCT only implements the small fraction of use-cases that the
-- NCEModule requires.
------------------------------------------------------------------------
local MCT = torch.class("torch.MultiCudaTensor")
MCT.__noGPU__ = true -- will prevent nn.GPU from switching devices
-- each buffer is indexed by device
local buffers1, buffers2 = {}, {}
function MCT:__init(catdim, tensors)
self.catdim = catdim or -1
self.tensors = tensors or {}
end
function MCT:size(dim)
if not self._size then
if #self.tensors == 0 then
self._size = {}
end
self._size = self.tensors[1]:size():totable()
for i=2,#self.tensors do
self._size[self.catdim] = self._size[self.catdim] + self.tensors[i]:size(self.catdim)
end
end
if dim then
return self._size[dim]
end
return torch.LongStorage(self._size)
end
function MCT:dim()
return self:size():size()
end
function MCT:zero()
for i,tensor in ipairs(self.tensors) do
cutorch.withDevice(tensor:getDevice(), function()
tensor:zero()
end)
end
return self
end
function MCT:t()
assert(self:size():size() == 2)
return self:transpose(1,2)
end
function MCT:transpose(dim1, dim2)
local dim = self.catdim
if dim1 == self.catdim then
dim = dim2
elseif dim2 == self.catdim then
dim = dim1
end
local tensors = {}
for i,tensor in ipairs(self.tensors) do
cutorch.withDevice(tensor:getDevice(), function()
tensors[i] = tensor:transpose(dim1, dim2)
end)
end
local result = self.new(dim, tensors)
return result
end
-- self.weight.index(self._weight, self.weight, 1, self.sampleidx:view(-1))
function MCT.index(res, src, dim, indices)
-- we only support a specific use-case
assert(torch.type(res) == 'torch.CudaTensor')
assert(torch.type(src) == 'torch.MultiCudaTensor')
assert(torch.type(dim) == 'number')
assert(dim == 1)
assert(torch.type(indices) == 'torch.CudaTensor' or torch.type(indices) == 'torch.LongTensor')
assert(indices:dim() == 1)
assert(src.catdim ~= dim)
local size = src:size()
size[dim] = indices:size(1)
res:resize(size)
local start = 1
for i,srctensor in ipairs(src.tensors) do
local device = srctensor:getDevice()
local res_ = res:narrow(src.catdim, start, srctensor:size(src.catdim))
local res__ = res_
cutorch.withDevice(device, function()
if device ~= res_:getDevice() then
buffers2[device] = buffers2[device] or res_.new()
buffers2[device]:resizeAs(res_):copy(res_)
res__ = buffers2[device]
end
if torch.type(indices) == 'torch.CudaTensor' and indices:getDevice() ~= device then
buffers1[device] = buffers1[device] or indices.new()
buffers1[device]:resizeAs(indices):copy(indices)
res__:index(srctensor, dim, buffers1[device])
else
res__:index(srctensor, dim, indices)
end
end)
if device ~= res:getDevice() then
res_:copy(res__)
end
start = start + res_:size(src.catdim)
end
return res
end
-- self.gradWeight:indexAdd(1, sampleidx, _gradWeight)
function MCT:indexAdd(dim, indices, src)
assert(torch.type(src) == 'torch.CudaTensor')
assert(torch.type(dim) == 'number')
assert(dim == 1)
assert(self.catdim ~= dim)
assert(torch.type(indices) == 'torch.CudaTensor' or torch.type(indices) == 'torch.LongTensor')
local start = 1
for i,tensor in ipairs(self.tensors) do
local device = tensor:getDevice()
local src_ = src:narrow(self.catdim, start, tensor:size(self.catdim))
local src__ = src_
cutorch.withDevice(device, function()
if device ~= src:getDevice() then
buffers2[device] = buffers2[device] or src.new()
buffers2[device]:resizeAs(src_):copy(src_)
src__ = buffers2[device]
end
if torch.type(indices) == 'torch.CudaTensor' and indices:getDevice() ~= device then
buffers1[device] = buffers1[device] or indices.new()
buffers1[device]:resizeAs(indices):copy(indices)
tensor:indexAdd(dim, buffers1[device], src__)
else
tensor:indexAdd(dim, indices, src__)
end
end)
start = start + src_:size(self.catdim)
end
return self
end
function MCT:add(value, src)
if not src then
src = value
value = 1
end
assert(torch.type(src) == 'torch.MultiCudaTensor')
assert(torch.type(value) == 'number')
for i,srctensor in ipairs(src.tensors) do
local dstdevice = self.tensors[i]:getDevice()
local srcdevice = srctensor:getDevice()
assert(dstdevice == srcdevice)
cutorch.withDevice(srcdevice, function()
self.tensors[i]:add(value, srctensor)
end)
end
return self
end
-- momGradParams[i]:mul(momFactor)
function MCT:mul(value)
for i,tensor in ipairs(self.tensors) do
cutorch.withDevice(tensor:getDevice(), function() tensor:mul(value) end)
end
return self
end
-- self.weight.addmm(self.linout, 0, self.linout, 1, input, self.weight:t())
-- res = (v1 * M) + (v2 * mat1 * mat2)
function MCT.addmm(res, v1, M, v2, mat1, mat2)
-- we only support a specific use-case
assert(mat2.catdim == 1)
assert(torch.type(mat2) == 'torch.MultiCudaTensor')
assert(torch.type(mat1) == 'torch.CudaTensor')
assert(torch.type(M) == 'torch.CudaTensor' and torch.pointer(M) == torch.pointer(res))
assert(torch.type(res) == 'torch.CudaTensor')
res:mul(v1)
local start = 1
local lastres = res
for i,mat2_ in ipairs(mat2.tensors) do
local mat1_ = mat1:narrow(2, start, mat2_:size(1))
local device = mat2_:getDevice()
cutorch.withDevice(device, function()
if device ~= mat1_:getDevice() then
buffers2[device] = buffers2[device] or mat1_.new()
buffers2[device]:resizeAs(mat1_):copy(mat1_)
mat1_ = buffers2[device]
end
buffers1[device] = buffers1[device] or lastres.new()
buffers1[device]:resizeAs(res)
buffers1[device]:mm(mat1_, mat2_)
end)
local resdevice = res:getDevice()
if device == resdevice then
res:add(v2, buffers1[device])
else
cutorch.withDevice(resdevice, function()
buffers1[resdevice] = buffers1[resdevice] or res.new()
buffers1[resdevice]:resizeAs(res):copy(buffers1[device])
end)
res:add(v2, buffers1[resdevice])
end
start = start + mat2_:size(1)
end
assert(start-1 == mat2:size(1))
return res
end
-- gradParam.new():resizeAs(gradParam):copy(gradParam)
function MCT:resizeAs(src)
self.catdim = src.catdim
for i,tensor in ipairs(src.tensors) do
self.tensors[i] = self.tensors[i] or tensor.new()
cutorch.withDevice(tensor:getDevice(), function() self.tensors[i]:resizeAs(tensor) end)
end
return self
end
function MCT:copy(src)
for i,tensor in ipairs(src.tensors) do
self.tensors[i]:copy(tensor)
end
return self
end
function MCT:write(file)
-- Write all values in the object as a table.
local object = {}
local tensors = self.tensors
self.tensors = nil
for k, v in pairs(self) do
object[k] = v
end
file:writeObject(object)
file:writeObject(#tensors)
for i,tensor in ipairs(tensors) do
file:writeObject(tensor:getDevice())
file:writeObject(tensor)
end
self.tensors = tensors
end
function MCT:read(file)
local object = file:readObject()
for k, v in pairs(object) do
self[k] = v
end
self.tensors = {}
local N = file:readObject()
for i=1,N do
local device = file:readObject()
self.tensors[i] = cutorch.withDevice(device, function() return file:readObject() end)
end
end
function MCT:clone()
local f = torch.MemoryFile("rw"):binary()
f:writeObject(self)
f:seek(1)
local clone = f:readObject()
f:close()
return clone
end
function MCT:uniform(lower, upper)
for i,tensor in ipairs(self.tensors) do
cutorch.withDevice(tensor:getDevice(), function() tensor:uniform(lower, upper) end)
end
return self
end
-- math.pow(gradParam:norm(),2)
function MCT:norm(...)
assert(#{...} == 0)
local norm = 0
for i,tensor in ipairs(self.tensors) do
norm = norm + cutorch.withDevice(tensor:getDevice(), function() return math.pow(tensor:norm(),2) end)
end
return math.sqrt(norm)
end
assert(not MCT.storage, "If you ever define storage, you will need to modify Module.sharedClone in dpnn.Module")