forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnRNN_algofindfw.go
420 lines (393 loc) · 12.2 KB
/
cudnnRNN_algofindfw.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"unsafe"
"github.com/dereklstinson/cutil"
)
//FindRNNForwardInferenceAlgorithmEx finds the inference algorithmEx
func (r *RNND) FindRNNForwardInferenceAlgorithmEx(
handle *Handle,
xD []*TensorD, //Input. An array of fully packed tensor descriptors describing the input to each recurrent iteration (one descriptor per iteration).
x cutil.Mem, //input
hxD *TensorD, //Input. A fully packed tensor descriptor describing the initial hidden state of the RNN.
hx cutil.Mem, //input
cxD *TensorD, //Input. A fully packed tensor descriptor describing the initial cell state for LSTM networks.
cx cutil.Mem, //input
wD *FilterD, //Input. Handle to a previously initialized filter descriptor describing the weights for the RNN.
w cutil.Mem, //Input
yD []*TensorD, //input An array of fully packed tensor descriptors.
y cutil.Mem, //Output Data pointer to GPU memory associated with the output tensor descriptor yDesc
hyD *TensorD, //input A fully packed tensor descriptor describing the final hidden state of the RNN.
hy cutil.Mem, //Output. Data pointer to GPU memory associated with the tensor descriptor hyDesc. If
cyD *TensorD, //Input. A fully packed tensor descriptor describing the final cell state for LSTM networks.
cy cutil.Mem, //output
findIntensity float32,
wspace cutil.Mem, wspacesize uint,
) ([]AlgorithmPerformance, error) {
algocount, err := r.getRNNForwardInferenceAlgorithmMaxCount(handle)
if err != nil {
return nil, err
}
seqLength := (C.int)(len(xD))
tocxD := tensorDArrayToC(xD)
tocyD := tensorDArrayToC(yD)
var retactAlgoCount C.int
perfResults := make([]C.cudnnAlgorithmPerformance_t, algocount)
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnFindRNNForwardInferenceAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x.Ptr(),
hxD.descriptor, hx.Ptr(),
cxD.descriptor, cx.Ptr(),
wD.descriptor, w.Ptr(),
&tocyD[0], y.Ptr(),
hyD.descriptor, hy.Ptr(),
cyD.descriptor, cy.Ptr(),
C.float(findIntensity),
(C.int)(algocount),
&retactAlgoCount,
&perfResults[0],
wspace.Ptr(), C.size_t(wspacesize),
)).error("(r *RNND) FindRNNForwardInferenceAlgorithmEx")
})
} else {
err = Status(C.cudnnFindRNNForwardInferenceAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x.Ptr(),
hxD.descriptor, hx.Ptr(),
cxD.descriptor, cx.Ptr(),
wD.descriptor, w.Ptr(),
&tocyD[0], y.Ptr(),
hyD.descriptor, hy.Ptr(),
cyD.descriptor, cy.Ptr(),
C.float(findIntensity),
(C.int)(algocount),
&retactAlgoCount,
&perfResults[0],
wspace.Ptr(), C.size_t(wspacesize),
)).error("(r *RNND) FindRNNForwardInferenceAlgorithmEx")
}
return calgoperftogoarray(perfResults, setfinalizer), err
}
//FindRNNForwardInferenceAlgorithmExUS is like FindRNNForwardInferenceAlgorithmEx but uses unsafe.Pointer instead of cutil.Mem
func (r *RNND) FindRNNForwardInferenceAlgorithmExUS(
handle *Handle,
xD []*TensorD, //Input. An array of fully packed tensor descriptors describing the input to each recurrent iteration (one descriptor per iteration).
x unsafe.Pointer, //input
hxD *TensorD, //Input. A fully packed tensor descriptor describing the initial hidden state of the RNN.
hx unsafe.Pointer, //input
cxD *TensorD, //Input. A fully packed tensor descriptor describing the initial cell state for LSTM networks.
cx unsafe.Pointer, //input
wD *FilterD, //Input. Handle to a previously initialized filter descriptor describing the weights for the RNN.
w unsafe.Pointer, //Input
yD []*TensorD, //input An array of fully packed tensor descriptors.
y unsafe.Pointer, //Output Data pointer to GPU memory associated with the output tensor descriptor yDesc
hyD *TensorD, //input A fully packed tensor descriptor describing the final hidden state of the RNN.
hy unsafe.Pointer, //Output. Data pointer to GPU memory associated with the tensor descriptor hyDesc. If
cyD *TensorD, //Input. A fully packed tensor descriptor describing the final cell state for LSTM networks.
cy unsafe.Pointer, //output
findIntensity float32,
wspace unsafe.Pointer, wspacesize uint,
) ([]AlgorithmPerformance, error) {
reqcount, err := r.getRNNForwardInferenceAlgorithmMaxCount(handle)
if err != nil {
return nil, err
}
seqLength := (C.int)(len(xD))
tocxD := tensorDArrayToC(xD)
tocyD := tensorDArrayToC(yD)
var retactAlgoCount C.int
perfResults := make([]C.cudnnAlgorithmPerformance_t, reqcount)
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnFindRNNForwardInferenceAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x,
hxD.descriptor, hx,
cxD.descriptor, cx,
wD.descriptor, w,
&tocyD[0], y,
hyD.descriptor, hy,
cyD.descriptor, cy,
C.float(findIntensity),
(C.int)(reqcount),
&retactAlgoCount,
&perfResults[0],
wspace, C.size_t(wspacesize),
)).error(" (r *RNND) FindRNNForwardInferenceAlgorithmExUS")
})
} else {
err = Status(C.cudnnFindRNNForwardInferenceAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x,
hxD.descriptor, hx,
cxD.descriptor, cx,
wD.descriptor, w,
&tocyD[0], y,
hyD.descriptor, hy,
cyD.descriptor, cy,
C.float(findIntensity),
(C.int)(reqcount),
&retactAlgoCount,
&perfResults[0],
wspace, C.size_t(wspacesize),
)).error(" (r *RNND) FindRNNForwardInferenceAlgorithmExUS")
}
return calgoperftogoarray(perfResults, setfinalizer), err
}
//GetRNNForwardTrainingAlgorithmMaxCount gets the max number of algorithms for rnnforward training algo
func (r *RNND) getRNNForwardTrainingAlgorithmMaxCount(handle *Handle) (int32, error) {
var count C.int
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetRNNForwardTrainingAlgorithmMaxCount(
handle.x,
r.descriptor,
&count)).error("(r *RNND) GetRNNForwardTrainingAlgorithmMaxCount")
})
} else {
err = Status(C.cudnnGetRNNForwardTrainingAlgorithmMaxCount(
handle.x,
r.descriptor,
&count)).error("(r *RNND) GetRNNForwardTrainingAlgorithmMaxCount")
}
return int32(count), err
}
//FindRNNForwardTrainingAlgorithmEx finds and orders the performance of rnn Algorithm for training returns that list with an error
func (r *RNND) FindRNNForwardTrainingAlgorithmEx(
handle *Handle,
xD []*TensorD, //input
x cutil.Mem, //input
hxD *TensorD, //input: A fully packed tensor descriptor describing the initial hidden state of the RNN.
hx cutil.Mem, //input
cxD *TensorD, // :input A fully packed tensor descriptor describing the initial cell state for LSTM networks.
cx cutil.Mem, //input
wD *FilterD, //input
w cutil.Mem, //input
yD []*TensorD, //Input. An array of fully packed tensor descriptors describing the output from each recurrent iteration (one descriptor per iteration).
y cutil.Mem, //output
hyD *TensorD, //input
hy cutil.Mem, //output
cyD *TensorD,
cy cutil.Mem, //output
findIntensity float32, //input
reqAlgocount int32, //input
wspace cutil.Mem, ///input
wspacesize uint,
rspace cutil.Mem, //input/output
rspacesize uint,
) ([]AlgorithmPerformance, error) {
reqcount, err := r.getRNNForwardTrainingAlgorithmMaxCount(handle)
if err != nil {
return nil, err
}
seqLength := (C.int)(len(xD))
tocxD := tensorDArrayToC(xD)
tocyD := tensorDArrayToC(yD)
var actualcount C.int
perfresults := make([]C.cudnnAlgorithmPerformance_t, reqAlgocount)
if handle.w != nil {
err = handle.w.Work(func() error {
if wspace == nil {
return Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0],
x.Ptr(),
hxD.descriptor,
hx.Ptr(),
cxD.descriptor,
cx.Ptr(),
wD.descriptor,
w.Ptr(),
&tocyD[0],
y.Ptr(),
hyD.descriptor,
hy.Ptr(),
cyD.descriptor,
cy.Ptr(),
C.float(findIntensity),
C.int(reqcount),
&actualcount,
&perfresults[0],
nil,
C.size_t(0),
rspace.Ptr(),
C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmEx")
}
return Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x.Ptr(),
hxD.descriptor, hx.Ptr(),
cxD.descriptor, cx.Ptr(),
wD.descriptor, w.Ptr(),
&tocyD[0], y.Ptr(),
hyD.descriptor, hy.Ptr(),
cyD.descriptor, cy.Ptr(),
C.float(findIntensity),
C.int(reqcount),
&actualcount, &perfresults[0],
wspace.Ptr(), C.size_t(wspacesize),
rspace.Ptr(), C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmEx")
})
} else {
if wspace == nil {
err = Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0],
x.Ptr(),
hxD.descriptor,
hx.Ptr(),
cxD.descriptor,
cx.Ptr(),
wD.descriptor,
w.Ptr(),
&tocyD[0],
y.Ptr(),
hyD.descriptor,
hy.Ptr(),
cyD.descriptor,
cy.Ptr(),
C.float(findIntensity),
C.int(reqcount),
&actualcount,
&perfresults[0],
nil,
C.size_t(0),
rspace.Ptr(),
C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmEx")
} else {
err = Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x.Ptr(),
hxD.descriptor, hx.Ptr(),
cxD.descriptor, cx.Ptr(),
wD.descriptor, w.Ptr(),
&tocyD[0], y.Ptr(),
hyD.descriptor, hy.Ptr(),
cyD.descriptor, cy.Ptr(),
C.float(findIntensity),
C.int(reqcount),
&actualcount, &perfresults[0],
wspace.Ptr(), C.size_t(wspacesize),
rspace.Ptr(), C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmEx")
}
}
if err != nil {
return nil, err
}
return calgoperftogoarray(perfresults, handle.gogc), err
}
//FindRNNForwardTrainingAlgorithmExUS is like FindRNNForwardTrainingAlgorithmEx but uses unsafe.Pointer instead of cutil.Mem
func (r *RNND) FindRNNForwardTrainingAlgorithmExUS(
handle *Handle,
xD []*TensorD, x unsafe.Pointer,
hxD *TensorD, hx unsafe.Pointer,
cxD *TensorD, cx unsafe.Pointer,
wD *FilterD, w unsafe.Pointer,
yD []*TensorD, y unsafe.Pointer,
hyD *TensorD, hy unsafe.Pointer,
cyD *TensorD, cy unsafe.Pointer,
findIntensity float32, //input
wspace unsafe.Pointer, wspacesize uint,
rspace unsafe.Pointer, rspacesize uint,
) ([]AlgorithmPerformance, error) {
reqcount, err := r.getRNNForwardTrainingAlgorithmMaxCount(handle)
if err != nil {
return nil, err
}
seqLength := (C.int)(len(xD))
tocxD := tensorDArrayToC(xD)
tocyD := tensorDArrayToC(yD)
var actualcount C.int
perfresults := make([]C.cudnnAlgorithmPerformance_t, reqcount)
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x,
hxD.descriptor, hx,
cxD.descriptor, cx,
wD.descriptor, w,
&tocyD[0], y,
hyD.descriptor, hy,
cyD.descriptor, cy,
C.float(findIntensity),
C.int(reqcount),
&actualcount,
&perfresults[0],
wspace, C.size_t(0),
rspace, C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmExUS")
})
} else {
err = Status(C.cudnnFindRNNForwardTrainingAlgorithmEx(
handle.x,
r.descriptor,
seqLength,
&tocxD[0], x,
hxD.descriptor, hx,
cxD.descriptor, cx,
wD.descriptor, w,
&tocyD[0], y,
hyD.descriptor, hy,
cyD.descriptor, cy,
C.float(findIntensity),
C.int(reqcount),
&actualcount,
&perfresults[0],
wspace, C.size_t(0),
rspace, C.size_t(rspacesize),
)).error("(r *RNND) FindRNNForwardTrainingAlgorithmExUS")
}
return calgoperftogoarray(perfresults, handle.gogc), err
}
//GetRNNForwardInferenceAlgorithmMaxCount returns the maxcount and error
func (r *RNND) getRNNForwardInferenceAlgorithmMaxCount(
handle *Handle,
) (int32, error) {
var count C.int
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetRNNForwardInferenceAlgorithmMaxCount(
handle.x,
r.descriptor,
&count,
)).error("GetRNNForwardInferenceAlgorithmMaxCount")
})
} else {
err = Status(C.cudnnGetRNNForwardInferenceAlgorithmMaxCount(
handle.x,
r.descriptor,
&count,
)).error("GetRNNForwardInferenceAlgorithmMaxCount")
}
return int32(count), err
}