-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcudnnActivation.go
384 lines (363 loc) · 12.1 KB
/
cudnnActivation.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
378
379
380
381
382
383
384
package gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
"github.com/dereklstinson/cutil"
)
//ActivationD is an opaque struct that holds the description of an activation operation.
type ActivationD struct {
descriptor C.cudnnActivationDescriptor_t
gogc bool
}
func (a *ActivationD) String() string {
mode, nan, coef, err := a.Get()
if err != nil {
return fmt.Sprintf("Activation Descriptor{\nError: %s\n}", err)
}
return fmt.Sprintf("Activation Descriptor{\n%v,\n%v,\ncoef: %v,\n}", mode, nan, coef)
}
//CreateActivationDescriptor creates an activation descriptor
func CreateActivationDescriptor() (*ActivationD, error) {
desc := new(ActivationD)
err := Status(C.cudnnCreateActivationDescriptor(&desc.descriptor)).error("NewActivationDescriptor-create")
if setfinalizer {
desc.gogc = true
runtime.SetFinalizer(desc, destroyactivationdescriptor)
}
return desc, err
}
//Set sets the activation operation according to the settings passed
func (a *ActivationD) Set(mode ActivationMode, nan NANProp, coef float64) error {
return Status(C.cudnnSetActivationDescriptor(a.descriptor, mode.c(), nan.c(), C.double(coef))).error("NewActivationDescriptor-set")
}
//Get gets the descriptor descriptors values
func (a *ActivationD) Get() (mode ActivationMode, nan NANProp, coef float64, err error) {
var coefd C.double
err = Status(C.cudnnGetActivationDescriptor(a.descriptor, mode.cptr(), nan.cptr(), &coefd)).error("GetDescriptor")
coef = (float64)(coefd)
return (mode), (nan), (coef), err
}
//Destroy destroys the activation descriptor if GC is not set. if not set method will only return nil
//Currently GC is always set with no way of turning it off
func (a *ActivationD) Destroy() error {
if setfinalizer || a.gogc {
return nil
}
return destroyactivationdescriptor(a)
}
func destroyactivationdescriptor(a *ActivationD) error {
return Status(C.cudnnDestroyActivationDescriptor(a.descriptor)).error("DestroyDescriptor")
}
//Forward does the forward activation function
//
//From deep learning sdk documentation (slightly modified for gocudnn):
//
//This routine applies a specified neuron activation function element-wise over each input value.
//
//Note: In-place operation is allowed for this routine; i.e., x and y cutil.Mem may be equal.
//However, this requires xD and yD descriptors to be identical
//(particularly, the strides of the input and output must match for in-place operation to be allowed).
//
//Note: All tensor formats are supported for 4 and 5 dimensions, however best performance is obtained
//when the strides of xD and yD are equal and HW-packed. For more than 5 dimensions
//the tensors must have their spatial dimensions packed.
//
//Parameters:
//
// ---
// handle(input):
//
// previously created Handle
// ---
// ----
// alpha, beta(input):
//
// Pointers to scaling factors (in host memory) used to blend the computation result with prior
// value in the output layer as follows: dstValue = alpha[0]*result + beta[0]*priorDstValue.
// ----
// ---
// xD(input):
//
// Handle to the previously initialized input tensor descriptor.
// ---
// ----
// x(input):
//
// Data pointer to GPU memory associated with the tensor descriptor xD.
//
// ----
// ---
// yD(input):
//
// Handle to the previously initialized output tensor descriptor.
// ---
// ----
// y(output):
//
// Data pointer to GPU memory associated with the output tensor descriptor yDesc.
// ----
//
//Possible Error Returns
//
// nil:
//
// The function launched successfully.
//
// CUDNN_STATUS_NOT_SUPPORTED:
//
// The function does not support the provided configuration.
//
// CUDNN_STATUS_BAD_PARAM:
//
// At least one of the following conditions are met:
//
// 1)The parameter mode has an invalid enumerant value.
// 2)The dimensions n,c,h,w of the input tensor and output tensors differ.
// 3)The datatype of the input tensor and output tensors differs.
// 4)The strides nStride,cStride,hStride,wStride of the input tensor and output tensors differ and in-place operation is used (i.e., x and y pointers are equal).
//
// CUDNN_STATUS_EXECUTION_FAILED:
//
// The function failed to launch on the GPU.
//
func (a *ActivationD) Forward(
handle *Handle,
alpha float64,
xD *TensorD, x cutil.Mem,
beta float64,
yD *TensorD, y cutil.Mem) error {
a1 := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnActivationForward(handle.x, a.descriptor, a1.CPtr(), xD.descriptor, x.Ptr(), b.CPtr(), yD.descriptor, y.Ptr())).error(" (a *ActivationD) Forward")
})
} else {
err = Status(C.cudnnActivationForward(handle.x, a.descriptor, a1.CPtr(), xD.descriptor, x.Ptr(), b.CPtr(), yD.descriptor, y.Ptr())).error(" (a *ActivationD) Forward")
}
return err
}
//ForwardUS is just like Forward but it takes unsafe.Pointers instead of cutil.Mem
func (a *ActivationD) ForwardUS(
handle *Handle,
alpha float64,
xD *TensorD, x unsafe.Pointer,
beta float64,
yD *TensorD, y unsafe.Pointer) error {
a1 := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnActivationForward(handle.x, a.descriptor, a1.CPtr(), xD.descriptor, x, b.CPtr(), yD.descriptor, y)).error("(a *ActivationD) ForwardUS")
})
} else {
err = Status(C.cudnnActivationForward(handle.x, a.descriptor, a1.CPtr(), xD.descriptor, x, b.CPtr(), yD.descriptor, y)).error("(a *ActivationD) ForwardUS")
}
return err
}
//Backward does the activation backward method
//
//From deep learning sdk documentation (slightly modified for gocudnn):
//
//This routine computes the gradient of a neuron activation function.
//
//Note: In-place operation is allowed for this routine; i.e., dx and dy cutil.Mem may be equal.
//However, this requires dxD and dyD descriptors to be identical
//(particularly, the strides of the input and output must match for in-place operation to be allowed).
//
//Note: All tensor formats are supported for 4 and 5 dimensions, however best performance is obtained
//when the strides of dxD and dyD are equal and HW-packed. For more than 5 dimensions
//the tensors must have their spatial dimensions packed.
//
//Parameters:
//
// ---
// handle(input):
//
// previously created Handle
// ---
// ----
// alpha, beta(input):
//
// Pointers to scaling factors (in host memory) used to blend the computation result with prior
// value in the output layer as follows: dstValue = alpha[0]*result + beta[0]*priorDstValue.
// ----
// ---
// xD(input):
//
// Handle to the previously initialized input tensor descriptor.
// ---
// ----
// x(input):
//
// Data pointer to GPU memory associated with the tensor descriptor xD.
// ----
// ---
// dxD(input):
//
// Handle to the previously initialized input tensor descriptor.
// ---
// ----
// dx(output):
//
// Data pointer to GPU memory associated with the tensor descriptor dxD.
// ----
// ---
// yD(input):
//
// Handle to the previously initialized output tensor descriptor.
// ---
// ----
// y(input):
//
// Data pointer to GPU memory associated with the output tensor descriptor yD.
// ----
// ---
// dyD(input):
//
// Handle to the previously initialized output tensor descriptor.
// ---
// ----
// dy(input):
//
// Data pointer to GPU memory associated with the output tensor descriptor dyD.
// ----
//
//Possible Error Returns
//
// nil:
//
// The function launched successfully.
//
// CUDNN_STATUS_NOT_SUPPORTED:
//
// 1) The dimensions n,c,h,w of the input tensor and output tensors differ.
// 2) The datatype of the input tensor and output tensors differs.
// 3) The strides nStride, cStride, hStride, wStride of the input tensor and the input differential tensor differ.
// 4) The strides nStride, cStride, hStride, wStride of the output tensor and the output differential tensor differ.
//
// CUDNN_STATUS_BAD_PARAM:
//
// At least one of the following conditions are met:
//
// The strides nStride, cStride, hStride, wStride of the input differential tensor and output
// differential tensors differ and in-place operation is used.
//
// CUDNN_STATUS_EXECUTION_FAILED:
//
// The function failed to launch on the GPU.
//
func (a *ActivationD) 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 {
a1 := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnActivationBackward(handle.x, a.descriptor, a1.CPtr(), yD.descriptor, y.Ptr(), dyD.descriptor, dy.Ptr(), xD.descriptor, x.Ptr(), b.CPtr(), dxD.descriptor, dx.Ptr())).error("(a *ActivationD) Backward")
})
} else {
err = Status(C.cudnnActivationBackward(handle.x, a.descriptor, a1.CPtr(), yD.descriptor, y.Ptr(), dyD.descriptor, dy.Ptr(), xD.descriptor, x.Ptr(), b.CPtr(), dxD.descriptor, dx.Ptr())).error("(a *ActivationD) Backward")
}
return err
}
//BackwardUS is just like Backward but it takes unsafe.Pointers instead of cutil.Mem
func (a *ActivationD) 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 {
a1 := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnActivationBackward(handle.x, a.descriptor, a1.CPtr(), yD.descriptor, y, dyD.descriptor, dy, xD.descriptor, x, b.CPtr(), dxD.descriptor, dx)).error("(a *ActivationD) BackwardUS")
})
} else {
err = Status(C.cudnnActivationBackward(handle.x, a.descriptor, a1.CPtr(), yD.descriptor, y, dyD.descriptor, dy, xD.descriptor, x, b.CPtr(), dxD.descriptor, dx)).error("(a *ActivationD) BackwardUS")
}
return err
}
//ActivationMode is used for activation discriptor flags flags are obtained through type's methods
type ActivationMode C.cudnnActivationMode_t
//Sigmoid sets a to ActivationMode(C.CUDNN_ACTIVATION_SIGMOID)and returns that value.
//
//Selects the sigmoid function.
func (a *ActivationMode) Sigmoid() ActivationMode {
*a = ActivationMode(C.CUDNN_ACTIVATION_SIGMOID)
return *a
}
//Relu sets a to ActivationMode(C.CUDNN_ACTIVATION_RELU)and returns that value.
//
//Selects the rectified linear function.
func (a *ActivationMode) Relu() ActivationMode {
*a = ActivationMode(C.CUDNN_ACTIVATION_RELU)
return *a
}
//Tanh sets a to ActivationMode(C.CUDNN_ACTIVATION_TANH)and returns that value.
//
//Selects the hyperbolic tangent function.
func (a *ActivationMode) Tanh() ActivationMode {
*a = ActivationMode(C.CUDNN_ACTIVATION_TANH)
return *a
}
//ClippedRelu sets a to ActivationMode(C.CUDNN_ACTIVATION_CLIPPED_RELU)and returns that value.
//
//Selects the clipped rectified linear function.
func (a *ActivationMode) ClippedRelu() ActivationMode {
*a = ActivationMode(C.CUDNN_ACTIVATION_CLIPPED_RELU)
return *a
}
//Elu sets a to ActivationMode(C.CUDNN_ACTIVATION_ELU) and returns that value.
//
//Selects the exponential linear function.
func (a *ActivationMode) Elu() ActivationMode { *a = ActivationMode(C.CUDNN_ACTIVATION_ELU); return *a }
//Identity returns ActivationMode(C.CUDNN_ACTIVATION_IDENTITY) (new for 7.1)
//
//Selects the identity function, intended for bypassing the activation step in (*Convolution)BiasActivationForward().
//(The Identity flag must use CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM, and only for (*Convolution)BiasActivationForward())
//Does not work with cudnnActivationForward() or cudnnActivationBackward().
func (a *ActivationMode) Identity() ActivationMode {
*a = ActivationMode(C.CUDNN_ACTIVATION_SIGMOID)
return *a
}
func (a ActivationMode) c() C.cudnnActivationMode_t { return C.cudnnActivationMode_t(a) }
func (a *ActivationMode) cptr() *C.cudnnActivationMode_t { return (*C.cudnnActivationMode_t)(a) }
func (a ActivationMode) String() string {
f := a
var x string
switch a {
case f.Identity():
x = "Identity"
case f.Elu():
x = "Elu"
case f.ClippedRelu():
x = "ClippedRelu"
case f.Tanh():
x = "Tanh"
case f.Relu():
x = "Relu"
case f.Sigmoid():
x = "Sigmoid"
default:
x = "Unsupported Format"
}
return "Activation Mode: " + x
}