forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudnnPooling.go
358 lines (323 loc) · 10.1 KB
/
cudnnPooling.go
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
package gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"runtime"
"unsafe"
"github.com/dereklstinson/cutil"
)
//PoolingD handles the pooling descriptor
type PoolingD struct {
descriptor C.cudnnPoolingDescriptor_t
dims C.int
gogc bool
}
//CreatePoolingDescriptor creates a pooling descriptor.
func CreatePoolingDescriptor() (*PoolingD, error) {
p := new(PoolingD)
err := Status(C.cudnnCreatePoolingDescriptor(&p.descriptor)).error("NewPooling2dDescriptor-create")
if setfinalizer || p.gogc {
runtime.SetFinalizer(p, destroypoolingdescriptor)
}
return p, err
}
//Set sets pooling descriptor to values passed
func (p *PoolingD) Set(mode PoolingMode, nan NANProp, window, padding, stride []int32) error {
cwindow := int32Tocint(window)
cpadding := int32Tocint(padding)
cstride := int32Tocint(stride)
p.dims = (C.int)(len(window))
return Status(C.cudnnSetPoolingNdDescriptor(
p.descriptor,
mode.c(),
nan.c(),
p.dims,
&cwindow[0],
&cpadding[0],
&cstride[0],
)).error("(*PoolingD)SetND")
}
//Get gets the descriptor values for pooling
func (p *PoolingD) Get() (mode PoolingMode, nan NANProp, window, padding, stride []int32, err error) {
windowc := make([]C.int, p.dims)
paddingc := make([]C.int, p.dims)
stridec := make([]C.int, p.dims)
var actual C.int
err = Status(C.cudnnGetPoolingNdDescriptor(
p.descriptor,
p.dims,
mode.cptr(),
nan.cptr(),
&actual,
&windowc[0],
&paddingc[0],
&stridec[0],
)).error("GetPoolingDescriptor-nd")
window, padding, stride = cintToint32(windowc), cintToint32(paddingc), cintToint32(stridec)
return mode, nan, window, padding, stride, err
}
//GetOutputDims will return the forward output dims from the pooling desc, and the tensor passed
func (p *PoolingD) GetOutputDims(
input *TensorD,
) ([]int32, error) {
outputdims := make([]C.int, input.dims)
err := Status(C.cudnnGetPoolingNdForwardOutputDim(
p.descriptor,
input.descriptor,
input.dims,
&outputdims[0],
)).error("GetPoolingForwardOutputDim-nd")
return cintToint32(outputdims), err
}
//Destroy destroys the pooling descriptor.
//
//Right now gocudnn is handle by the go GC exclusivly, but sometime in the future
//user of package will be be able to toggle it.
func (p *PoolingD) Destroy() error {
if setfinalizer || p.gogc {
return nil
}
return destroypoolingdescriptor(p)
}
func destroypoolingdescriptor(p *PoolingD) error {
return Status(C.cudnnDestroyPoolingDescriptor(p.descriptor)).error("DestroyDescriptor")
}
/* Pooling functions: All of the form "output = alpha * Op(inputs) + beta * output" */
//Forward does the poolingForward operation
func (p *PoolingD) Forward(
handle *Handle,
alpha float64,
xD *TensorD, x cutil.Mem,
beta float64,
yD *TensorD, y cutil.Mem,
) error {
a := cscalarbydatatype(xD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
return Status(C.cudnnPoolingForward(
handle.x,
p.descriptor,
a.CPtr(),
xD.descriptor,
x.Ptr(),
b.CPtr(),
yD.descriptor,
y.Ptr(),
)).error("PoolingForward")
}
//ForwardUS is like Forward but uses unsafe.Pointer instead of cutil.Mem
func (p *PoolingD) ForwardUS(
handle *Handle,
alpha float64,
xD *TensorD, x unsafe.Pointer,
beta float64,
yD *TensorD, y unsafe.Pointer,
) error {
a := cscalarbydatatype(xD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
return Status(C.cudnnPoolingForward(
handle.x,
p.descriptor,
a.CPtr(),
xD.descriptor, x,
b.CPtr(),
yD.descriptor, y,
)).error("PoolingForward")
}
//Backward does the backward pooling operation
func (p *PoolingD) Backward(
handle *Handle,
alpha float64,
yD *TensorD, y cutil.Mem,
dyD *TensorD, dy cutil.Mem,
xD *TensorD, x cutil.Mem,
beta float64,
dxD *TensorD, dx cutil.Mem,
) error {
a := cscalarbydatatype(xD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
return Status(C.cudnnPoolingBackward(handle.x, p.descriptor, a.CPtr(), yD.descriptor, y.Ptr(), dyD.descriptor, dy.Ptr(), xD.descriptor, x.Ptr(), b.CPtr(), dxD.descriptor, dx.Ptr())).error("PoolingBackward")
}
//BackwardUS is like Backward but uses unsafe.Pointer instead of cutil.Mem
func (p *PoolingD) BackwardUS(
handle *Handle,
alpha float64,
yD *TensorD, y unsafe.Pointer,
dyD *TensorD, dy unsafe.Pointer,
xD *TensorD, x unsafe.Pointer,
beta float64,
dxD *TensorD, dx unsafe.Pointer,
) error {
a := cscalarbydatatype(xD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
return Status(C.cudnnPoolingBackward(handle.x,
p.descriptor,
a.CPtr(),
yD.descriptor, y,
dyD.descriptor, dy,
xD.descriptor, x,
b.CPtr(),
dxD.descriptor, dx)).error("PoolingBackward")
}
/*
* pooling mode
*/
//PoolingMode is used for flags in pooling
type PoolingMode C.cudnnPoolingMode_t
//Max returns PoolingMode(C.CUDNN_POOLING_MAX) flag
//
//The maximum value inside the pooling window is used.
func (p *PoolingMode) Max() PoolingMode { *p = PoolingMode(C.CUDNN_POOLING_MAX); return *p }
//AverageCountIncludePadding returns PoolingMode(C.CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING) flag
//
//Values inside the pooling window are averaged.
//The number of elements used to calculate the average
//includes spatial locations falling in the padding region.
func (p *PoolingMode) AverageCountIncludePadding() PoolingMode {
*p = PoolingMode(C.CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING)
return *p
}
//AverageCountExcludePadding returns PoolingMode(C.CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING) flag
//
//Values inside the pooling window are averaged.
//The number of elements used to calculate the average
//excludes spatial locations falling in the padding region.
func (p *PoolingMode) AverageCountExcludePadding() PoolingMode {
*p = PoolingMode(C.CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING)
return *p
}
//MaxDeterministic returns PoolingMode(C.CUDNN_POOLING_MAX_DETERMINISTIC) flag
//
//The maximum value inside the pooling window is used.
//The algorithm used is deterministic.
func (p *PoolingMode) MaxDeterministic() PoolingMode {
*p = PoolingMode(C.CUDNN_POOLING_MAX_DETERMINISTIC)
return *p
}
func (p PoolingMode) c() C.cudnnPoolingMode_t { return C.cudnnPoolingMode_t(p) }
func (p *PoolingMode) cptr() *C.cudnnPoolingMode_t { return (*C.cudnnPoolingMode_t)(p) }
/*
//Set2D sets pooling descriptor to 2d
func (p *PoolingD) Set2D(mode PoolingMode, nan NANProp, Hwindow, Wwindow, Vpadding, Hpadding, Vstride, Hstride int32) error {
return Status(C.cudnnSetPooling2dDescriptor(
p.descriptor,
mode.c(),
nan.c(),
C.int(Hwindow),
C.int(Wwindow),
C.int(Vpadding),
C.int(Hpadding),
C.int(Vstride),
C.int(Hstride),
)).error("(*PoolingD)Set2Dex")
}
//Set2DEx sets up a 2D pooling descriptor but with the window padding and stride are in slices.
func (p *PoolingD) Set2DEx(mode PoolingMode, nan NANProp, window, padding, stride []int32) error {
if len(window) != len(padding) || len(window) != len(stride) || len(window) != 2 {
return errors.New("Window Padding and Stride array lengths need to be 2")
}
p.dims = (C.int)(len(window))
return Status(C.cudnnSetPooling2dDescriptor(
p.descriptor,
mode.c(),
nan.c(),
C.int(window[0]),
C.int(window[1]),
C.int(padding[0]),
C.int(padding[1]),
C.int(stride[0]),
C.int(stride[1]),
)).error("(*PoolingD)Set2Dex")
}
//SetND sets pooling descriptor to values passed
func (p *PoolingD) SetND(mode PoolingMode, nan NANProp, window, padding, stride []int32) error {
cwindow := int32Tocint(window)
cpadding := int32Tocint(padding)
cstride := int32Tocint(stride)
p.dims = (C.int)(len(window))
return Status(C.cudnnSetPoolingNdDescriptor(
p.descriptor,
mode.c(),
nan.c(),
p.dims,
&cwindow[0],
&cpadding[0],
&cstride[0],
)).error("(*PoolingD)SetND")
}
//Get2D get 2d gets the 2D descriptor values
func (p *PoolingD) Get2D() (mode PoolingMode, nan NANProp, Hwindow, Wwindow, Vpadding, Hpadding, Vstride, Hstride int32, err error) {
var hw, ww, vp, hp, vs, hs C.int
err = Status(C.cudnnGetPooling2dDescriptor(
p.descriptor,
mode.cptr(), nan.cptr(),
&hw, &ww, &vp, &hp, &vs, &hs,
)).error("GetPooling2dDescriptor-2d")
Hwindow, Wwindow, Vpadding, Hpadding, Vstride, Hstride = (int32)(hw), (int32)(ww), (int32)(vp), (int32)(hp), (int32)(vs), (int32)(hs)
return mode, nan, Hwindow, Wwindow, Vpadding, Hpadding, Vstride, Hstride, err
}
//Get2DEx gets the 2d descriptor values in ND mode.
func (p *PoolingD) Get2DEx() (mode PoolingMode, nan NANProp, window, padding, stride []int32, err error) {
window = make([]int32, 2)
padding = make([]int32, 2)
stride = make([]int32, 2)
hw, ww, vp, hp, vs, hs := (*C.int)(&window[0]), (*C.int)(&window[1]), (*C.int)(&padding[0]), (*C.int)(&padding[1]), (*C.int)(&stride[0]), (*C.int)(&stride[1])
err = Status(C.cudnnGetPooling2dDescriptor(
p.descriptor,
mode.cptr(), nan.cptr(),
hw, ww, vp, hp, vs, hs,
)).error("GetPooling2dDescriptor-2d")
return mode, nan, window, padding, stride, err
}
//GetND gets the nd descriptor for pooling
func (p *PoolingD) GetND() (mode PoolingMode, nan NANProp, window, padding, stride []int32, err error) {
windowc := make([]C.int, p.dims)
paddingc := make([]C.int, p.dims)
stridec := make([]C.int, p.dims)
var actual C.int
err = Status(C.cudnnGetPoolingNdDescriptor(
p.descriptor,
p.dims,
mode.cptr(),
nan.cptr(),
&actual,
&windowc[0],
&paddingc[0],
&stridec[0],
)).error("GetPoolingDescriptor-nd")
window, padding, stride = cintToint32(windowc), cintToint32(paddingc), cintToint32(stridec)
return mode, nan, window, padding, stride, err
}
//GetOutputDim will return the forward output dims from the pooling desc, and the tensor passed
func (p *PoolingD) GetOutputDim(
input *TensorD,
) ([]int32, error) {
if p.dims > 2 {
outputdims := make([]C.int, p.dims)
err := Status(C.cudnnGetPoolingNdForwardOutputDim(
p.descriptor,
input.descriptor,
p.dims,
&outputdims[0],
)).error("GetPoolingForwardOutputDim-nd")
if setkeepalive {
keepsalivebuffer(p, input)
}
return cintToint32(outputdims), err
}
outputdims := make([]C.int, 4)
err := Status(C.cudnnGetPooling2dForwardOutputDim(
p.descriptor,
input.descriptor,
&outputdims[0],
&outputdims[1],
&outputdims[2],
&outputdims[3],
)).error("GetPoolingForwardOutputDim-2d")
if setkeepalive {
keepsalivebuffer(p, input)
}
return cintToint32(outputdims), err
}
*/