forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnDropOut.go
377 lines (350 loc) · 8.81 KB
/
cudnnDropOut.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"runtime"
"unsafe"
"github.com/dereklstinson/GoCudnn/gocu"
"github.com/dereklstinson/cutil"
)
//DropOutD holds the dropout descriptor
type DropOutD struct {
descriptor C.cudnnDropoutDescriptor_t
gogc bool
}
//CreateDropOutDescriptor creates a drop out descriptor to be set
func CreateDropOutDescriptor() (*DropOutD, error) {
dod := new(DropOutD)
err := Status(C.cudnnCreateDropoutDescriptor(&dod.descriptor)).error("CreateDropOutDescriptor()")
if err != nil {
return nil, err
}
if setfinalizer {
dod.gogc = true
runtime.SetFinalizer(dod, destroydropoutdescriptor)
}
return nil, nil
}
//Set sets the drop out descriptor
func (d *DropOutD) Set(handle *Handle, dropout float32, states cutil.Mem, bytes uint, seed uint64) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) Set")
})
}
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) Set")
}
//SetUS is like Set but uses unsafe.Pointer instead of cutil.Mem
func (d *DropOutD) SetUS(handle *Handle, dropout float32, states unsafe.Pointer, bytes uint, seed uint64) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout), states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) SetUS")
})
}
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout), states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) SetUS")
}
//Destroy destroys the dropout descriptor unless the the finalizer flag was set.
func (d *DropOutD) Destroy() error {
if setfinalizer || d.gogc {
return nil
}
return destroydropoutdescriptor(d)
}
func destroydropoutdescriptor(d *DropOutD) error {
return Status(C.cudnnDestroyDropoutDescriptor(d.descriptor)).error("destroydropoutdescriptor(d *DropOutD)")
}
//Restore restores the descriptor to a previously saved-off state
func (d *DropOutD) Restore(
handle *Handle,
dropout float32, //probability that the input value is set to zero
states cutil.Mem,
bytes uint,
seed uint64,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) Restore")
})
}
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) Restore")
}
//RestoreUS is like Restore but uses unsafe.Pointer instead of cutil.Mem
func (d *DropOutD) RestoreUS(
handle *Handle,
dropout float32, //probability that the input value is set to zero
states unsafe.Pointer,
bytes uint,
seed uint64,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) RestoreUS")
})
}
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("(d *DropOutD) RestoreUS")
}
//Get gets the descriptor to a previously saved-off state
func (d *DropOutD) Get(
handle *Handle,
) (float32, cutil.Mem, uint64, error) {
var seed C.ulonglong
var dropout C.float
var x unsafe.Pointer
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("(d *DropOutD) Get")
})
} else {
err = Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("(d *DropOutD) Get")
}
return float32(dropout), gocu.WrapUnsafe(x), uint64(seed), err
}
//GetUS is like GetUS but uses unsafe.Pointer instead of cutil.Mem
func (d *DropOutD) GetUS(handle *Handle) (float32, unsafe.Pointer, uint64, error) {
var seed C.ulonglong
var dropout C.float
var x unsafe.Pointer
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("(d *DropOutD) GetUS")
})
} else {
err = Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("(d *DropOutD) GetUS")
}
return float32(dropout), x, uint64(seed), err
}
//GetStateSize returns the state size in bytes
//Method calls a function that doesn't use DropOutD, but it is a dropout type function, and is
//used to get the size the cutil.Mem, or unsafe.Pointer needs to for state.
func (d *DropOutD) GetStateSize(handle *Handle) (uint, error) {
var size C.size_t
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnDropoutGetStatesSize(handle.x, &size)).error("(d *DropOutD) GetStateSize")
})
} else {
err = Status(C.cudnnDropoutGetStatesSize(handle.x, &size)).error("(d *DropOutD) GetStateSize")
}
return uint(size), err
}
//GetReserveSpaceSize returns the size of reserve space in bytes. Method calls a function that doesn't
//use the DropOutD, but function is releveant to the DropOut operation
func (d *DropOutD) GetReserveSpaceSize(t *TensorD) (uint, error) {
var size C.size_t
err := Status(C.cudnnDropoutGetReserveSpaceSize(t.descriptor, &size)).error("(d *DropOutD) GetReserveSpaceSize")
return uint(size), err
}
//Forward performs the dropoutForward
//
// Input/Output: y,reserveSpace
func (d *DropOutD) Forward(
handle *Handle,
xD *TensorD, //input
x cutil.Mem, //input
yD *TensorD, //input
y cutil.Mem, //input/output
reserveSpace cutil.Mem, //input/output
reservesize uint,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("(d *DropOutD) Forward")
})
}
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("(d *DropOutD) Forward")
}
//ForwardUS is like Forward but uses unsafe.Pointer instead of cutil.Mem
func (d *DropOutD) ForwardUS(
handle *Handle,
xD *TensorD, x unsafe.Pointer, //input
yD *TensorD, y unsafe.Pointer, //input/output
reserveSpace unsafe.Pointer, reservesize uint,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor, x,
yD.descriptor, y,
reserveSpace,
C.size_t(reservesize),
)).error(" (d *DropOutD) ForwardUS")
})
}
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor, x,
yD.descriptor, y,
reserveSpace,
C.size_t(reservesize),
)).error(" (d *DropOutD) ForwardUS")
}
//Backward performs the dropoutForward
//
// Input/Output: dx,reserveSpace
func (d *DropOutD) Backward(
handle *Handle,
dyD *TensorD, //input
dy cutil.Mem, //input
dxD *TensorD, //input
dx cutil.Mem, //input/output
reserveSpace cutil.Mem, //input/output
reservesize uint,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor,
dy.Ptr(),
dxD.descriptor,
dx.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("(d *DropOutD) Backward")
})
}
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor,
dy.Ptr(),
dxD.descriptor,
dx.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("(d *DropOutD) Backward")
}
//BackwardUS is like Backward but uses unsafe.Pointer instead of cutil.Mem
func (d *DropOutD) BackwardUS(
handle *Handle,
dyD *TensorD, //input
dy unsafe.Pointer, //input
dxD *TensorD, //input
dx unsafe.Pointer, //input/output
reserveSpace unsafe.Pointer, //input/output
reservesize uint,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor, dy,
dxD.descriptor, dx,
reserveSpace, C.size_t(reservesize),
)).error("(d *DropOutD) BackwardUS")
})
}
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor, dy,
dxD.descriptor, dx,
reserveSpace, C.size_t(reservesize),
)).error("(d *DropOutD) BackwardUS")
}