forked from soumith/cudnn.torch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.lua
382 lines (330 loc) · 15.2 KB
/
functional.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
-- this file attempts to provide a purely functional set of bindings
-- all functions in this file retain absolutely no state.
-- There shouldn't be any reference to "self" in this file.
local cudnn = require 'cudnn.env'
local ffi = require 'ffi'
local errcheck = cudnn.errcheck
local NULL
if not jit then
NULL = ffi.C.NULL
end
cudnn.functional = {}
local function Batch2D(t)
return t:view(1, t:size(1), t:size(2), t:size(3))
end
-- accumulates the bias into output.
-- output is assumed to be allocated and given.
cudnn.functional.bias2D_updateOutput = function(handle, bias, output)
output = output:dim() == 3 and Batch2D(output) or output
local biasDesc = cudnn.toDescriptor(bias:view(1, bias:nElement(),1,1))
local oDesc = cudnn.toDescriptor(output)
errcheck('cudnnAddTensor', handle,
cudnn.scalar(output, 1), biasDesc[0], bias:data(),
cudnn.scalar(output, 1), oDesc[0], output:data())
end
-- accumulates the gradients into gradBias.
-- gradBias is assumed to be allocated and given.
cudnn.functional.bias2D_accGradParameters = function(handle, gradOutput, gradBias, scale)
gradOutput = gradOutput:dim() == 3 and Batch2D(gradOutput) or gradOutput
scale = scale or 1.0
local scaleT = torch.FloatTensor({scale})
local oDesc = cudnn.toDescriptor(gradOutput)
local biasDesc = cudnn.toDescriptor(gradBias:view(1, gradBias:nElement(),1,1))
errcheck('cudnnConvolutionBackwardBias', handle,
scaleT:data(),
oDesc[0], gradOutput:data(),
cudnn.scalar(gradOutput, 1),
biasDesc[0], gradBias:data())
end
-- Does a 2D Convolution (updateOutput) on input, weight
-- output is assumed to be allocated and given.
cudnn.functional.Convolution2D_updateOutput = function(handle, input, weight, output,
strideH, strideW, padH, padW, workspace)
input = input:dim() == 3 and Batch2D(input) or input
output = output:dim() == 3 and Batch2D(output) or output
-- create a weight descriptor
local weightDesc = ffi.new('struct cudnnFilterStruct*[1]')
errcheck('cudnnCreateFilterDescriptor', weightDesc)
local nOutputPlane, nInputPlane, kH, kW
= weight:size(1), weight:size(2), weight:size(3), weight:size(4)
local desc = torch.IntTensor({nOutputPlane, nInputPlane, kH, kW})
errcheck('cudnnSetFilterNdDescriptor', weightDesc[0], cudnn.typemap[torch.type(input)], 'CUDNN_TENSOR_NCHW', 4,
desc:data());
local function destroyWDesc(d)
errcheck('cudnnDestroyFilterDescriptor', d[0]);
end
ffi.gc(weightDesc, destroyWDesc)
-- create a convolution descriptor
local convDesc = ffi.new('struct cudnnConvolutionStruct*[1]')
errcheck('cudnnCreateConvolutionDescriptor', convDesc)
local pad = torch.IntTensor({padH, padW})
local stride = torch.IntTensor({strideH, strideW})
local upscale = torch.IntTensor({1,1})
errcheck('cudnnSetConvolutionNdDescriptor', convDesc[0],
2, pad:data(),
stride:data(), upscale:data(), 'CUDNN_CROSS_CORRELATION',
cudnn.configmap(torch.type(weight)));
local function destroyConvDesc(d)
errcheck('cudnnDestroyConvolutionDescriptor', d[0]);
end
ffi.gc(convDesc, destroyConvDesc)
-- create input descriptor
local iDesc = cudnn.toDescriptor(input)
-- create output descriptor
local oSize = torch.IntTensor(4)
errcheck('cudnnGetConvolutionNdForwardOutputDim',
convDesc[0], iDesc[0],
weightDesc[0], 4, oSize:data())
oSize = oSize:long()
assert(output:dim() == 4 and
output:size(1) == oSize[1] and
output:size(2) == oSize[2] and
output:size(3) == oSize[3] and
output:size(4) == oSize[4],
'Output is of wrong size')
-- create descriptor for output
local oDesc = cudnn.toDescriptor(output)
-- create forwardAlgorithm descriptors for
local algType = ffi.new("cudnnConvolutionFwdAlgo_t[?]", 1)
local algSearchMode = 'CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT'
local algWorkspaceLimit = 0
if workspace then
algWorkspaceLimit = workspace:nElement() * 4 -- 4 = sizeof float
end
errcheck('cudnnGetConvolutionForwardAlgorithm',
handle,
iDesc[0], weightDesc[0],
convDesc[0], oDesc[0],
algSearchMode, algWorkspaceLimit, algType)
-- do convolution
errcheck('cudnnConvolutionForward', handle,
cudnn.scalar(input, 1),
iDesc[0], input:data(),
weightDesc[0], weight:data(),
convDesc[0], algType[0],
workspace and workspace:data() or nil, algWorkspaceLimit,
cudnn.scalar(input, 0),
oDesc[0], output:data());
end
-- Does a 2D Convolution (updateGradInput) on input, weight, output, gradOutput
-- gradInput is assumed to be allocated and given.
cudnn.functional.Convolution2D_updateGradInput = function(handle, input, weight, output, gradOutput, gradInput,
strideH, strideW, padH, padW)
input = input:dim() == 3 and Batch2D(input) or input
output = output:dim() == 3 and Batch2D(output) or output
gradOutput = gradOutput:dim() == 3 and Batch2D(gradOutput) or gradOutput
gradInput = gradInput:dim() == 3 and Batch2D(gradInput) or gradInput
-- create a weight descriptor
local weightDesc = ffi.new('struct cudnnFilterStruct*[1]')
errcheck('cudnnCreateFilterDescriptor', weightDesc)
local nOutputPlane, nInputPlane, kH, kW
= weight:size(1), weight:size(2), weight:size(3), weight:size(4)
local desc = torch.IntTensor({nOutputPlane, nInputPlane, kH, kW})
errcheck('cudnnSetFilterNdDescriptor', weightDesc[0], cudnn.typemap[torch.type(input)], 'CUDNN_TENSOR_NCHW', 4,
desc:data());
local function destroyWDesc(d)
errcheck('cudnnDestroyFilterDescriptor', d[0]);
end
ffi.gc(weightDesc, destroyWDesc)
-- create a convolution descriptor
local convDesc = ffi.new('struct cudnnConvolutionStruct*[1]')
errcheck('cudnnCreateConvolutionDescriptor', convDesc)
local pad = torch.IntTensor({padH, padW})
local stride = torch.IntTensor({strideH, strideW})
local upscale = torch.IntTensor({1,1})
errcheck('cudnnSetConvolutionNdDescriptor', convDesc[0],
2, pad:data(),
stride:data(), upscale:data(), 'CUDNN_CROSS_CORRELATION',
cudnn.configmap(torch.type(weight)));
local function destroyConvDesc(d)
errcheck('cudnnDestroyConvolutionDescriptor', d[0]);
end
ffi.gc(convDesc, destroyConvDesc)
-- create input, output descriptor
local iDesc = cudnn.toDescriptor(input)
local oDesc = cudnn.toDescriptor(output)
local algType = ffi.new("cudnnConvolutionBwdDataAlgo_t[?]", 1)
local algSearchMode = 'CUDNN_CONVOLUTION_BWD_DATA_NO_WORKSPACE'
errcheck('cudnnGetConvolutionBackwardDataAlgorithm',
cudnn.getHandle(),
weightDesc[0], oDesc[0],
convDesc[0], iDesc[0],
algSearchMode, 0, algType)
-- do convolution
errcheck('cudnnConvolutionBackwardData', handle,
cudnn.scalar(input, 1),
weightDesc[0], weight:data(),
oDesc[0], gradOutput:data(),
convDesc[0],
algType[0],
NULL, 0,
cudnn.scalar(input, 0),
iDesc[0], gradInput:data());
end
-- accumulates the gradients into gradWeight.
-- gradWeight is assumed to be allocated and given.
local scaleT = torch.FloatTensor(1):fill(1.0)
cudnn.functional.Convolution2D_accGradParameters = function(handle, input, gradWeight, gradOutput,
strideH, strideW, padH, padW, scale)
input = input:dim() == 3 and Batch2D(input) or input
gradOutput = gradOutput:dim() == 3 and Batch2D(gradOutput) or gradOutput
scale = scale or 1.0
scaleT[1] = scale
-- create a weight descriptor
local weightDesc = ffi.new('struct cudnnFilterStruct*[1]')
errcheck('cudnnCreateFilterDescriptor', weightDesc)
local nOutputPlane, nInputPlane, kH, kW
= gradWeight:size(1), gradWeight:size(2), gradWeight:size(3), gradWeight:size(4)
local desc = torch.IntTensor({nOutputPlane, nInputPlane, kH, kW})
errcheck('cudnnSetFilterNdDescriptor', weightDesc[0], cudnn.typemap[torch.type(input)], 'CUDNN_TENSOR_NCHW', 4,
desc:data());
local function destroyWDesc(d)
errcheck('cudnnDestroyFilterDescriptor', d[0]);
end
ffi.gc(weightDesc, destroyWDesc)
-- create a convolution descriptor
local convDesc = ffi.new('struct cudnnConvolutionStruct*[1]')
errcheck('cudnnCreateConvolutionDescriptor', convDesc)
local pad = torch.IntTensor({padH, padW})
local stride = torch.IntTensor({strideH, strideW})
local upscale = torch.IntTensor({1,1})
errcheck('cudnnSetConvolutionNdDescriptor', convDesc[0],
2, pad:data(),
stride:data(), upscale:data(), 'CUDNN_CROSS_CORRELATION',
cudnn.configmap(torch.type(gradWeight)));
local function destroyConvDesc(d)
errcheck('cudnnDestroyConvolutionDescriptor', d[0]);
end
ffi.gc(convDesc, destroyConvDesc)
-- create input, output descriptor
local iDesc = cudnn.toDescriptor(input)
local oDesc = cudnn.toDescriptor(gradOutput)
local algType = ffi.new("cudnnConvolutionBwdFilterAlgo_t[?]", 1)
local algSearchMode = 'CUDNN_CONVOLUTION_BWD_FILTER_NO_WORKSPACE'
local algWorkspaceLimit = 0
errcheck('cudnnGetConvolutionBackwardFilterAlgorithm',
cudnn.getHandle(),
iDesc[0], oDesc[0],
convDesc[0], weightDesc[0],
algSearchMode, algWorkspaceLimit, algType)
-- do convolution
errcheck('cudnnConvolutionBackwardFilter', handle,
scaleT:data(),
iDesc[0], input:data(),
oDesc[0], gradOutput:data(),
convDesc[0],
algType[0],
NULL, 0,
cudnn.scalar(input, 1),
weightDesc[0], gradWeight:data());
end
-- Does a 2D Pooling (updateOutput) on input, weight
-- output is assumed to be allocated and given.
cudnn.functional.Pooling_updateOutput = function(handle, mode, input, output,
kH, kW, dH, dW, padH, padW, ceil_mode)
input = input:dim() == 3 and Batch2D(input) or input
output = output:dim() == 3 and Batch2D(output) or output
padH = padH or 0
padW = padW or 0
ceil_mode = ceil_mode or false
local oW, oH
if ceil_mode then
oW = math.ceil((input:size(4)+padW*2 - kW)/dW + 1)
oH = math.ceil((input:size(3)+padH*2 - kH)/dH + 1)
else
oW = math.floor((input:size(4)+padW*2 - kW)/dW + 1)
oH = math.floor((input:size(3)+padH*2 - kH)/dH + 1)
end
assert(oH == output:size(3) and oW == output:size(4),
'size mismatch: ' .. oH .. 'x' .. oW .. ' vs ' ..
output:size(3) .. 'x' .. output:size(4))
-- create pooling descriptor
local poolDesc = ffi.new('struct cudnnPoolingStruct*[1]')
errcheck('cudnnCreatePoolingDescriptor', poolDesc)
local ker = torch.IntTensor({kH, kW})
local str = torch.IntTensor({dH, dW})
local pad = torch.IntTensor({padH, padW})
errcheck('cudnnSetPoolingNdDescriptor', poolDesc[0], mode, 'CUDNN_PROPAGATE_NAN', 2,
ker:data(), pad:data(), str:data());
local function destroyPoolDesc(d)
errcheck('cudnnDestroyPoolingDescriptor', d[0]);
end
ffi.gc(poolDesc, destroyPoolDesc)
-- create input, output descriptor
local iDesc = cudnn.toDescriptor(input)
local oDesc = cudnn.toDescriptor(output)
-- pool
errcheck('cudnnPoolingForward', handle,
poolDesc[0],
cudnn.scalar(input, 1),
iDesc[0], input:data(),
cudnn.scalar(input, 0),
oDesc[0], output:data());
end
cudnn.functional.MaxPooling2D_updateOutput = function(handle, input, output,
kH, kW, dH, dW, padH, padW, ceil_mode)
cudnn.functional.Pooling_updateOutput(handle, 'CUDNN_POOLING_MAX', input, output,
kH, kW, dH, dW, padH, padW, ceil_mode);
end
cudnn.functional.AveragePooling2D_updateOutput = function(handle, input, output,
kH, kW, dH, dW, padH, padW, ceil_mode)
cudnn.functional.Pooling_updateOutput(handle, 'CUDNN_POOLING_AVERAGE', input, output,
kH, kW, dH, dW, padH, padW, ceil_mode);
end
-- Does a 2D Pooling (updateGradInput) on input, weight
-- output is assumed to be allocated and given.
cudnn.functional.Pooling_updateGradInput = function(handle, mode, input, output, gradOutput, gradInput,
kH, kW, dH, dW, padH, padW, ceil_mode)
input = input:dim() == 3 and Batch2D(input) or input
output = output:dim() == 3 and Batch2D(output) or output
gradOutput = gradOutput:dim() == 3 and Batch2D(gradOutput) or gradOutput
gradInput = gradInput:dim() == 3 and Batch2D(gradInput) or gradInput
padH = padH or 0
padW = padW or 0
ceil_mode = ceil_mode or false
local oW, oH
if ceil_mode then
oW = math.ceil((input:size(4)+padW*2 - kW)/dW + 1)
oH = math.ceil((input:size(3)+padH*2 - kH)/dH + 1)
else
oW = math.floor((input:size(4)+padW*2 - kW)/dW + 1)
oH = math.floor((input:size(3)+padH*2 - kH)/dH + 1)
end
assert(oH == output:size(3) and oW == output:size(4),
'size mismatch: ' .. oH .. 'x' .. oW .. ' vs ' ..
output:size(3) .. 'x' .. output:size(4))
-- create pooling descriptor
local poolDesc = ffi.new('struct cudnnPoolingStruct*[1]')
errcheck('cudnnCreatePoolingDescriptor', poolDesc)
local ker = torch.IntTensor({kH, kW})
local str = torch.IntTensor({dH, dW})
local pad = torch.IntTensor({padH, padW})
errcheck('cudnnSetPoolingNdDescriptor', poolDesc[0], mode, 'CUDNN_PROPAGATE_NAN', 2,
ker:data(), pad:data(), str:data());
local function destroyPoolDesc(d)
errcheck('cudnnDestroyPoolingDescriptor', d[0]);
end
ffi.gc(poolDesc, destroyPoolDesc)
-- create input, output descriptor
local iDesc = cudnn.toDescriptor(input)
local oDesc = cudnn.toDescriptor(output)
-- pool
errcheck('cudnnPoolingBackward',
handle, poolDesc[0],
cudnn.scalar(input, 1),
oDesc[0], output:data(),
oDesc[0], gradOutput:data(),
iDesc[0], input:data(),
cudnn.scalar(input, 0),
iDesc[0], gradInput:data());
end
cudnn.functional.MaxPooling2D_updateGradInput = function(handle, input, output, gradOutput, gradInput,
kH, kW, dH, dW, padH, padW, ceil_mode)
cudnn.functional.Pooling_updateGradInput(handle, 'CUDNN_POOLING_MAX', input, output, gradOutput, gradInput,
kH, kW, dH, dW, padH, padW, ceil_mode);
end
cudnn.functional.AveragePooling2D_updateGradInput = function(handle, input, output, gradOutput, gradInput,
kH, kW, dH, dW, padH, padW, ceil_mode)
cudnn.functional.Pooling_updateGradInput(handle, 'CUDNN_POOLING_AVERAGE', input, output, gradOutput, gradInput,
kH, kW, dH, dW, padH, padW, ceil_mode);
end