forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnDropOut.go
246 lines (219 loc) · 5.96 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
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 {
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("SetDropoutDescriptor")
}
//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 {
return Status(C.cudnnSetDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout), states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("SetDropoutDescriptor")
}
//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("DestroyDescriptor")
}
//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 {
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states.Ptr(),
C.size_t(bytes),
C.ulonglong(seed),
)).error("RestoreDropoutDescriptor")
}
//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 {
return Status(C.cudnnRestoreDropoutDescriptor(
d.descriptor,
handle.x,
C.float(dropout),
states,
C.size_t(bytes),
C.ulonglong(seed),
)).error("RestoreDropoutDescriptor")
}
//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
err := Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("GetDropoutDescriptor")
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
err := Status(C.cudnnGetDropoutDescriptor(
d.descriptor,
handle.x,
&dropout,
&x,
&seed,
)).error("GetDropoutDescriptor")
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
err := Status(C.cudnnDropoutGetStatesSize(handle.x, &size)).error("DropoutGetStateSize")
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("DropoutGetReserveSpaceSize")
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 {
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("DropoutForward")
}
//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 {
return Status(C.cudnnDropoutForward(
handle.x,
d.descriptor,
xD.descriptor, x,
yD.descriptor, y,
reserveSpace,
C.size_t(reservesize),
)).error("DropoutForward")
}
//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 {
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor,
dy.Ptr(),
dxD.descriptor,
dx.Ptr(),
reserveSpace.Ptr(),
C.size_t(reservesize),
)).error("DropoutBackward")
}
//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 {
return Status(C.cudnnDropoutBackward(
handle.x,
d.descriptor,
dyD.descriptor, dy,
dxD.descriptor, dx,
reserveSpace, C.size_t(reservesize),
)).error("DropoutBackward")
}