-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiopenRNN.go
More file actions
1065 lines (1007 loc) · 44.9 KB
/
Copy pathmiopenRNN.go
File metadata and controls
1065 lines (1007 loc) · 44.9 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package miopen
/*
#include <miopen/miopen.h>
*/
import "C"
import (
"errors"
"runtime"
"github.com/dereklstinson/cutil"
)
//RNNMode is used for flags for the RNNMode. Flags are set through its methods
//
//RNN mode selection for rnn layer preference
type RNNMode C.miopenRNNMode_t
func (r RNNMode) c() C.miopenRNNMode_t { return (C.miopenRNNMode_t)(r) }
func (r *RNNMode) cptr() *C.miopenRNNMode_t { return (*C.miopenRNNMode_t)(r) }
//RELU - RNN with ReLU activation
func (r *RNNMode) RELU() RNNMode { *r = (RNNMode)(C.miopenRNNRELU); return *r }
//Tanh - RNN with tanh activation
func (r *RNNMode) Tanh() RNNMode { *r = (RNNMode)(C.miopenRNNTANH); return *r }
//LSTM - set to LSTM
func (r *RNNMode) LSTM() RNNMode { *r = (RNNMode)(C.miopenLSTM); return *r }
//GRU - Set to GRU
func (r *RNNMode) GRU() RNNMode { *r = (RNNMode)(C.miopenGRU); return *r }
//RNNInputMode is used for flags for the RNNInputMode. Flags are set through its methods
//
//Recurrent Neural Network layer initial input mode
type RNNInputMode C.miopenRNNInputMode_t
func (r RNNInputMode) c() C.miopenRNNInputMode_t { return (C.miopenRNNInputMode_t)(r) }
func (r *RNNInputMode) cptr() *C.miopenRNNInputMode_t { return (*C.miopenRNNInputMode_t)(r) }
//Linear - Matrix multiplication at the input of the first layer
func (r *RNNInputMode) Linear() RNNInputMode { *r = (RNNInputMode)(C.miopenRNNlinear); return *r }
//Skip - No operation is performed at the input of the first layer.
func (r *RNNInputMode) Skip() RNNInputMode { *r = (RNNInputMode)(C.miopenRNNskip); return *r }
//RNNAlgo is used for flags for the RNNAlgo. Flags are set through its methods
//
// Recurrent Neural Network algorithm mode
type RNNAlgo C.miopenRNNAlgo_t
func (r RNNAlgo) c() C.miopenRNNAlgo_t { return (C.miopenRNNAlgo_t)(r) }
func (r *RNNAlgo) cptr() *C.miopenRNNAlgo_t { return (*C.miopenRNNAlgo_t)(r) }
//Default is default.
func (r *RNNAlgo) Default() RNNAlgo { *r = (RNNAlgo)(C.miopenRNNdefault); return *r }
//RNNDirectionMode - Recurrent Neural Network bi-directional behavior
type RNNDirectionMode C.miopenRNNDirectionMode_t
func (r RNNDirectionMode) c() C.miopenRNNDirectionMode_t { return (C.miopenRNNDirectionMode_t)(r) }
func (r *RNNDirectionMode) cptr() *C.miopenRNNDirectionMode_t { return (*C.miopenRNNDirectionMode_t)(r) }
//UNI - Forward in time only
func (r *RNNDirectionMode) UNI() RNNDirectionMode {
*r = (RNNDirectionMode)(C.miopenRNNunidirection)
return *r
}
//BI - Forward and backwards in time
func (r *RNNDirectionMode) BI() RNNDirectionMode {
*r = (RNNDirectionMode)(C.miopenRNNbidirection)
return *r
}
//RNNBiasMode - Recurrent Neural Network add on bias
type RNNBiasMode C.miopenRNNBiasMode_t
func (r RNNBiasMode) c() C.miopenRNNBiasMode_t { return (C.miopenRNNBiasMode_t)(r) }
func (r *RNNBiasMode) cptr() *C.miopenRNNBiasMode_t { return (*C.miopenRNNBiasMode_t)(r) }
//NoBias - No Biases will be applied to GEMM operations
func (r *RNNBiasMode) NoBias() RNNBiasMode { *r = (RNNBiasMode)(C.miopenRNNNoBias); return *r }
//WithBias - Biases will be applied to GEMM operations
func (r *RNNBiasMode) WithBias() RNNBiasMode { *r = (RNNBiasMode)(C.miopenRNNwithBias); return *r }
//RNNGEMMalgoMode - RNN algo mode
type RNNGEMMalgoMode C.miopenRNNGEMMalgoMode_t
func (r RNNGEMMalgoMode) c() C.miopenRNNGEMMalgoMode_t { return (C.miopenRNNGEMMalgoMode_t)(r) }
func (r *RNNGEMMalgoMode) cptr() *C.miopenRNNGEMMalgoMode_t { return (*C.miopenRNNGEMMalgoMode_t)(r) }
//AlgoGEMM selects algo to GEMM
func (r *RNNGEMMalgoMode) AlgoGEMM() RNNGEMMalgoMode {
*r = (RNNGEMMalgoMode)(C.miopenRNNAlgoGEMM)
return *r
}
//CreateRNNDescriptor - Create a RNN layer Descriptor
func CreateRNNDescriptor() (rnnD *RNND, err error) {
rnnD = new(RNND)
err = Status(C.miopenCreateRNNDescriptor(&rnnD.d)).error("CreateRNNDescriptor")
runtime.SetFinalizer(rnnD, miopenDestroyRNNDescriptor)
return rnnD, err
}
func miopenDestroyRNNDescriptor(r *RNND) error {
return Status(C.miopenDestroyRNNDescriptor(r.d)).error("miopenDestroyRNNDescriptor")
}
//Set - Set the details of the RNN descriptor
//
//Interface for setting the values of the RNN descriptor object. This function requires specific
//algorithm selection.
//
//hsize Hidden layer size (input)
//nlayers Number of layers (input)
//inMode RNN first layer input mode (input)
//direction RNN direction (input)
//mode RNN model type (input)
//biasmode RNN bias included (input)
//algo RNN algorithm selected (input)
//dtype Only fp32 currently supported for RNNs (input)
func (r *RNND) Set(hsize, nlayers int32,
inMode RNNInputMode,
direction RNNDirectionMode,
mode RNNMode,
biasmode RNNBiasMode,
algo RNNAlgo,
dtype DataType) error {
return Status(C.miopenSetRNNDescriptor(r.d, (C.int)(hsize), (C.int)(nlayers), inMode.c(), direction.c(), mode.c(), biasmode.c(), algo.c(), dtype.c())).error("(r *RNND) Set()")
}
//Get - Retrieves a RNN layer descriptor's details
//
func (r *RNND) Get() (hsize, nlayers int32,
inMode RNNInputMode,
direction RNNDirectionMode,
mode RNNMode,
biasmode RNNBiasMode,
algo RNNAlgo,
err error) {
err = Status(C.miopenGetRNNDescriptor(r.d,
mode.cptr(),
algo.cptr(),
inMode.cptr(),
direction.cptr(),
biasmode.cptr(),
(*C.int)(&hsize),
(*C.int)(&nlayers))).error("(r *RNND) Get()")
return hsize, nlayers, inMode, direction, mode, biasmode, algo, err
}
//GetWorkspaceSize - Query the amount of memory required to execute the RNN layer
//
//This function calculates the amount of memory required to run the RNN layer given an RNN
//descriptor and a tensor descriptor.
//
// h MIOpen handle (input)
//
// xD An array of tensor descriptors. These are the
// input descriptors to each time step. The first dimension of each descriptor is the
// size and may decrease from element n to element n+1 and not increase in size.
// second dimension is the same for all descriptors in the array and is the input
// length. (input)
//
// wspacesib Number of bytes required for RNN layer execution (output)
func (r *RNND) GetWorkspaceSize(h *Handle, xD []*TensorD) (wspacesib uint, err error) {
var sizet C.size_t
xDs, seqLen := tensorDarraytomiopenTensorDescriptorArray(xD)
err = Status(C.miopenGetRNNWorkspaceSize(h.x, r.d, seqLen, &xDs[0], &sizet)).error("GetWorkspaceSize")
wspacesib = (uint)(sizet)
return wspacesib, err
}
//GetTrainingReserveSize - Query the amount of memory required for RNN training
//
//This function calculates the amount of memory required to train the RNN layer given an
//RNN descriptor and a tensor descriptor.
//
// h MIOpen handle (input)
//
// xD An array of tensor descriptors. These are the
// input descriptors to each time step. The first dimension of each descriptor is the
// batch size and may decrease from element n to element n+1 and not increase in size.
// The second dimension is the same for all descriptors in the array and is the input
// vector length. (input)
//
// reservesib Number of bytes required for RNN layer execution (output)
func (r *RNND) GetTrainingReserveSize(h *Handle, xD []*TensorD) (reservesib uint, err error) {
var sizet C.size_t
xDs, seqLen := tensorDarraytomiopenTensorDescriptorArray(xD)
err = Status(C.miopenGetRNNTrainingReserveSize(h.x, r.d, (seqLen), &xDs[0], &sizet)).error("GetTrainingReserveSize")
reservesib = (uint)(sizet)
return reservesib, err
}
//GetParamSize - Query the amount of parameter memory required for RNN training
//
//This function calculates the amount of parameter memory required to train the RNN layer given an
//RNN descriptor and a tensor descriptor.
//
// h MIOpen handle (input)
// xD A tensor descriptor (input)
// dtype MIOpen data type enum (input)
func (r *RNND) GetParamSize(h *Handle, xD *TensorD, dtype DataType) (paramSIB uint, err error) {
var sizet C.size_t
err = Status(C.miopenGetRNNParamsSize(h.x, r.d, xD.d, &sizet, dtype.c())).error("GetTrainingReserveSize")
paramSIB = (uint)(sizet)
return paramSIB, err
}
//GetRNNDParamDescriptor - Obtain a weight tensor descriptor for RNNs
//
//This function populates a weight descriptor that describes the memory layout of the
//weight matrix.
//
// h MIOpen handle (input)
// xD A previously populated tensor descriptor (input)
// dtype MIOpen data type enum, currently only fp32 is supported (input)
func (r *RNND) GetRNNDParamDescriptor(h *Handle, xD *TensorD, dtype DataType) (wD *TensorD, err error) {
wD, err = CreateTensorDescriptor()
if err != nil {
return nil, err
}
err = Status(C.miopenGetRNNParamsDescriptor(h.x, r.d, xD.d, wD.d, dtype.c())).error("(r *RNND)GetRNNDParamDescriptor()")
return wD, err
}
//GetInputTensorSize - Obtain a the size in bytes of the RNN input tensor
//
//This function determines the size in bytes of the allocation needed for the input data
//tensor for an RNN layer. The number of bytes is derived from the array of
//tensor descriptors.
//
// h MIOpen handle (input)
//
// xD An array of tensor descriptors. These are the
// input descriptors to each time step. The first dimension of each descriptor is the
// batch size and may decrease from element n to element n+1 and not increase in size.
// The second dimension is the same for all descriptors in the array and is the input
// vector length. (input)
func (r *RNND) GetInputTensorSize(h *Handle, xD []*TensorD) (sib uint, err error) {
var sizet C.size_t
xDs, seqLen := tensorDarraytomiopenTensorDescriptorArray(xD)
err = Status(C.miopenGetRNNInputTensorSize(h.x, r.d, (seqLen), &xDs[0], &sizet)).error("(r *RNND)miopenGetRNNInputTensorSize()")
sib = (uint)(sizet)
return sib, err
}
//GetHiddenTensorSize - Obtain a the size in bytes of the RNN hidden tensor
//
//This function determines the size in bytes of the allocation needed for the
//hidden tensor over all layers
//
// handle MIOpen handle (input)
// xD An array of previously populated tensor descriptors (input)
func (r *RNND) GetHiddenTensorSize(h *Handle, xD []*TensorD) (sib uint, err error) {
var sizet C.size_t
xDs, seqLen := tensorDarraytomiopenTensorDescriptorArray(xD)
err = Status(C.miopenGetRNNHiddenTensorSize(h.x, r.d, (seqLen), &xDs[0], &sizet)).error("(r *RNND)GetHiddenTensorSize()")
sib = (uint)(sizet)
return sib, err
}
//GetLayerParamSize - Gets the number of bytes of a parameter matrix
//
//For RNN vanilla ELU and TANH, paramID == 0 retrieves the
//weight matrix associated with the in input GEMM, while paramID == 1 retrieves
//the weight matrix associated with the hidden state GEMM.
//
//For LSTM paramID 0 to 3 refer to the weight matrices associated
//with the input GEMM, 4-7 are associated with matrices associated with the
//hidden state GEMM.
// paramID 0 and 4 are for the input gate.
// paramID 1 and 5 are for the forget gate.
// paramID 2 and 6 are for the output gate.
// paramID 3 and 7 are for the new memory gate.
//
//For GRU paramID 0 to 2 refer to the weight matrix offset associated
//with the input GEMM, while 3 through 5 are associated with the hidden state
//GEMM.
// paramID 0 and 3 are for the update gate.
// paramID 1 and 4 are for the reset gate.
// paramID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
// h MIOpen handle (input)
// layer layer number in the RNN stack (input)
// xDesc tensor descriptor to input (input)
// paramID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerParamSize(h *Handle, layer int32, xD *TensorD, paramID int32) (sib uint, err error) {
var sizet C.size_t
err = Status(C.miopenGetRNNLayerParamSize(h.x, r.d, (C.int)(layer), xD.d, (C.int)(paramID), &sizet)).error("(r *RNND)GetLayerParamSize()")
sib = (uint)(sizet)
return sib, err
}
//GetLayerBiasSize - Gets the number of bytes of a bias
//
//For RNN vanilla ELU and TANH, biasID == 0 retrieves the
//weight matrix associated with the in input GEMM, while biasID == 1 retrieves
//the weight matrix associated with the hidden state GEMM.
//
//For LSTM biasID 0 to 3 refer to the weight matrices associated
//with the input GEMM, 4-7 are associated with matrices associated with the
//hidden state GEMM.
// biasID 0 and 4 are for the input gate.
// biasID 1 and 5 are for the forget gate.
// biasID 2 and 6 are for the output gate.
// biasID 3 and 7 are for the new memory gate.
//
//For GRU biasID 0 to 2 refer to the weight matrix offset associated
//with the input GEMM, while 3 through 5 are associated with the hidden state
//GEMM.
// biasID 0 and 3 are for the update gate.
// biasID 1 and 4 are for the reset gate.
// biasID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
// h MIOpen handle (input)
// layer layer number in the RNN stack (input)
// biasID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerBiasSize(h *Handle, layer, biasID int32) (sib uint, err error) {
var sizet C.size_t
err = Status(C.miopenGetRNNLayerBiasSize(h.x, r.d, (C.int)(layer), (C.int)(biasID), &sizet)).error("(r *RNND)GetLayerBiasSize()")
sib = (uint)(sizet)
return sib, err
}
//GetLayerParam - Gets a weight matrix for a specific layer in an RNN stack
//
//This function retrieves the weight matrix data for a specific layer and parameter ID
//and copies the data into previously allocated device memory.
//
//For RNN vanilla miopenRNNRELU and miopenRNNTANH, paramID == 0 retrieves the
//weight matrix associated with the in input GEMM, while paramID == 1 retrieves
//the weight matrix associated with the hidden state GEMM.
//
//For miopenLSTM paramID 0 to 3 refer to the weight matrices associated
//with the input GEMM, 4-7 are associated with matrices associated with the
//hidden state GEMM.
// paramID 0 and 4 are for the input gate.
// paramID 1 and 5 are for the forget gate.
// paramID 2 and 6 are for the output gate.
// paramID 3 and 7 are for the new memory gate.
//
//For miopenGRU paramID 0 to 2 refer to the weight matrix offset associated
//with the input GEMM, while 3 through 5 are associated with the hidden state
//GEMM.
// paramID 0 and 3 are for the update gate.
// paramID 1 and 4 are for the reset gate.
// paramID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The output argument paramDesc is a previously created tensor descriptor that is populated
//to describe the memory layout of the parameter matrix. It is full packed and is used when
//calling to (r *RNND) SetLayerParam()
//
//The argument layerParam should either be nullptr, or have device memory allocated
//to allow copying of the entire layer parameter matrix into it. If layerParam is
//nullptr then only the paramDesc is populated and returned. The size in bytes of the
//layer parameter matrix can be determined by using (r *RNND) GetLayerParamSize(().
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case (r *RNND) GetLayerParam() will return
//a error status miopenStatusBadParm for input paramID associated with the input GEMM.
//
// h MIOpen handle (input)
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// wD A tensor descriptor to the parameter tensor (input)
// w Pointer to memory containing parameter tensor (input)
// paramID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerParam(h *Handle, layer int32, xD, wD *TensorD, w cutil.Mem, paramID int32) (paramD *TensorD, param cutil.Mem, err error) {
paramD, err = CreateTensorDescriptor()
if err != nil {
return nil, nil, err
}
param = new(mem)
err = Status(C.miopenGetRNNLayerParam(h.x,
r.d,
(C.int)(layer),
xD.d,
wD.d,
w.Ptr(),
(C.int)(paramID),
paramD.d,
param.Ptr())).error("(r *RNND)GetLayerParam()")
return paramD, param, err
}
//GetLayerBias - Gets a bias for a specific layer in an RNN stack
//
//This function retrieves the bias data for a specific layer and bias ID and copies
//the data into previously allocated device memory.
//
//For RNN vanilla miopenRNNRELU and miopenRNNTANH, biasID == 0 retrieves the
//bias associated with the in input GEMM, while biasID == 1 retrieves
//the bias associated with the hidden state GEMM.
//
//For miopenLSTM biasID 0 to 3 refer to the biases associated
//with the input GEMM, 4-7 are associated with biases associated with the
//hidden state GEMM.
// biasID 0 and 4 are for the input gate.
// biasID 1 and 5 are for the forget gate.
// biasID 2 and 6 are for the output gate.
// biasID 3 and 7 are for the new memory gate.
//
//For miopenGRU biasID 0 to 2 refer to the biases associated with the input GEMM,
//while 3 through 5 are associated with the hidden state GEMM.
// biasID 0 and 3 are for the update gate.
// biasID 1 and 4 are for the reset gate.
// biasID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The output argument biasDesc is a previously created tensor descriptor that is populated
//to describe the memory layout of the bias. It is full packed and is used when
//calling to (r *RNND) SetLayerBias()
//
//The argument layerBias should either be nullptr, or have device memory allocated
//to allow copying of the entire layer bias into it. If layerBias is
//nullptr then only the biasDesc is populated and returned. The size in bytes of the
//layer bias can be determined by using (r *RNND) GetLayerBiasSize().
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case (r *RNND) GetLayerBias() will return
//a error status miopenStatusBadParm for input biasID associated with the input GEMM.
//
// h MIOpen handle (input)
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// wD A tensor descriptor to the parameter tensor (input)
// w Pointer to memory containing parameter tensor (input)
// biasID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerBias(h *Handle, layer int32, xD, wD *TensorD, w cutil.Mem, biasID int32) (biasD *TensorD, bias cutil.Mem, err error) {
biasD, err = CreateTensorDescriptor()
if err != nil {
return nil, nil, err
}
bias = new(mem)
err = Status(C.miopenGetRNNLayerBias(h.x,
r.d,
(C.int)(layer),
xD.d,
wD.d,
w.Ptr(),
(C.int)(biasID),
biasD.d,
bias.Ptr())).error("(r *RNND)GetLayerBias()")
return biasD, bias, err
}
//GetLayerParamOffset -Gets an index offset for a specific weight matrix for a layer in the RNN stack
//
//This function retrieves the index offset for a weight matrix in a layer.
//
//For RNN vanilla miopenRNNRELU and miopenRNNTANH, paramID == 0 retrieves the
//weight matrix offset associated with the in input GEMM, while paramID == 1
//retrieves the weight matrix offset associated with the hidden state GEMM.
//
//For miopenLSTM paramID 0 to 3 refer to the weight matrix offsets associated
//with the input GEMM, 4-7 are associated with matrix offset associated with the
//hidden state GEMM.
// paramID 0 and 4 are for the input gate.
// paramID 1 and 5 are for the forget gate.
// paramID 2 and 6 are for the output gate.
// paramID 3 and 7 are for the new memory gate.
//
//For miopenGRU paramID 0 to 2 refer to the weight matrix offset associated
//with the input GEMM, while 3 through 5 are associated with the hidden state
//GEMM.
// paramID 0 and 3 are for the update gate.
// paramID 1 and 4 are for the reset gate.
// paramID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The output argument paramDesc is a previously created tensor descriptor that is populated
//to describe the memory layout of the parameter matrix. It is full packed and is used when
//calling to (r *RNND) SetLayerParam().
//
//The argument layerParamOffset should either be nullptr, or an address to place the
//offset. If layerParamOffset is nullptr then only the paramDesc is populated and returned.
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case (r *RNND)GetLayerParamOffset() will return
//a error status miopenStatusBadParm for input paramID associated with the input GEMM.
//
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// paramID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerParamOffset(layer int32, xD *TensorD, paramID int32) (paramD *TensorD, offset uint, err error) {
paramD, err = CreateTensorDescriptor()
if err != nil {
return nil, 0, err
}
var coffset C.size_t
err = Status(C.miopenGetRNNLayerParamOffset(
r.d,
(C.int)(layer),
xD.d,
(C.int)(paramID),
paramD.d,
&coffset)).error("(r *RNND)GetLayerParamOffset()")
offset = (uint)(coffset)
return paramD, offset, err
}
//GetLayerBiasOffset - Gets a bias index offset for a specific layer in an RNN stack
//
//This function retrieves the bias index offset for a specific layer and bias ID.
//
//For RNN vanilla miopenRNNRELU and miopenRNNTANH, biasID == 0 retrieves the
//bias associated with the in input GEMM, while biasID == 1 retrieves
//the weight matrix associated with the hidden state GEMM.
//
//For miopenLSTM biasID 0 to 3 refer to the bias offset associated
//with the input GEMM, 4-7 are the bias offsets associated with the hidden state GEMM.
// biasID 0 and 4 are for the input gate.
// biasID 1 and 5 are for the forget gate.
// biasID 2 and 6 are for the output gate.
// biasID 3 and 7 are for the new memory gate.
//
//For miopenGRU biasID 0 to 2 refer to the biases associated with the input GEMM,
//while 3 through 5 are associated with the hidden state GEMM.
// biasID 0 and 3 are for the update gate.
// biasID 1 and 4 are for the reset gate.
// biasID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The output argument biasDesc is a previously created tensor descriptor that is populated
//to describe the memory layout of the bias. It is full packed and is used when
//calling to (r *RNND) SetLayerBias()
//
//The argument layerBiasOffset should either be nullptr, or point to an output address.
//If layerBias is nullptr then only the biasDesc is populated and returned.
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case miopenGetRNNLayerBiasOffset() will return
//a error status miopenStatusBadParm for input biasID associated with the input GEMM.
//
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// biasID ID of the internal parameter tensor (input)
func (r *RNND) GetLayerBiasOffset(layer int32, xD *TensorD, paramID int32) (biasD *TensorD, offset uint, err error) {
biasD, err = CreateTensorDescriptor()
if err != nil {
return nil, 0, err
}
var coffset C.size_t
err = Status(C.miopenGetRNNLayerBiasOffset(
r.d,
(C.int)(layer),
xD.d,
(C.int)(paramID),
biasD.d,
&coffset)).error("(r *RNND)GetLayerParamOffset()")
offset = (uint)(coffset)
return biasD, offset, err
}
//SetLayerParam - Sets a weight matrix for a specific layer in an RNN stack
//
// This function sets the weight matrix data for a specific layer and parameter ID.
//
// For RNN vanilla miopenRNNRELU and miopenRNNTANH, paramID == 0 sets the
// weight matrix associated with the in input GEMM, while paramID == 1 sets
// the weight matrix associated with the hidden state GEMM.
//
// For miopenLSTM paramID 0 to 3 refer to the weight matrices associated
// with the input GEMM, 4-7 are associated with matrices associated with the
// hidden state GEMM.
// paramID 0 and 4 are for the input gate.
// paramID 1 and 5 are for the forget gate.
// paramID 2 and 6 are for the output gate.
// paramID 3 and 7 are for the new memory gate.
//
// For miopenGRU paramID 0 to 2 refer to the weight matrix offset associated
// with the input GEMM, while 3 through 5 are associated with the hidden state
// GEMM.
// paramID 0 and 3 are for the update gate.
// paramID 1 and 4 are for the reset gate.
// paramID 2 and 5 are for the new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The input argument paramDesc is a previously populated tensor descriptor typically
//by first calling (r *RNND) GetLayerParam().
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case (r *RNND) SetLayerParam() will return
//a error status miopenStatusBadParm for input paramID associated with the input GEMM.
//
// h MIOpen handle (input)
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// wD A tensor descriptor to the parameter tensor (input)
// w Pointer to memory containing parameter tensor (input)
// paramID ID of the internal parameter tensor (input)
// paramD Descriptor of the parameter tensor (input)
// layerParam Pointer to the memory location of the parameter tensor (input)
func (r *RNND) SetLayerParam(h *Handle, layer int32, xD,
wD *TensorD, w cutil.Mem,
paramID int32, paramD *TensorD, layerParam cutil.Mem) error {
return Status(C.miopenSetRNNLayerParam(h.x, r.d, (C.int)(layer), xD.d, wD.d, w.Ptr(), (C.int)(paramID), paramD.d, layerParam.Ptr())).error("SetLayerParam")
}
//SetLayerBias - Sets a bias for a specific layer in an RNN stack
//
//This function sets the bias data for a specific layer and bias ID.
//
//For RNN vanilla miopenRNNRELU and miopenRNNTANH, biasID == 0 retrieves the
//weight matrix associated with the in input GEMM, while biasID == 1 retrieves
//the bias associated with the hidden state GEMM.
//
// For miopenLSTM biasID 0 to 3 refer to the biases associated
// with the input GEMM, 4-7 are associated with the biases associated with the
// hidden state GEMM.
// biasID 0 and 4 are for the input gate.
// biasID 1 and 5 are for the forget gate.
// biasID 2 and 6 are for the output gate.
// biasID 3 and 7 are for the new memory gate.
//
// For miopenGRU biasID 0 to 2 refer to the biases associated with the input GEMM,
// while 3 through 5 are associated with the hidden state GEMM.
// biasID 0 and 3 are for the update gate.
// biasID 1 and 4 are for the reset gate.
// biasID 2 and 5 are for the new new memory gate.
//
//For bi-directional RNNs the backwards in time direction is numbered as the layer
//directly after the forward in time direction.
//
//The input argument biasDesc is a previously populated tensor descriptor typically
//by first calling miopenGetRNNLayeBias().
//
//Note: When inputSkip mode is selected there is no input layer matrix operation,
//and therefore no associated memory. In this case (r *RNND) SetLayerBias will return
//a error status miopenStatusBadParm for input biasID associated with the input GEMM.
//
// h MIOpen handle (input)
// layer The layer number in the RNN stack (input)
// xD A tensor descriptor to input (input)
// wD A tensor descriptor to the bias tensor (input)
// w Pointer to memory containing bias tensor (input)
// biasID ID of the internal bias tensor (input)
// biasD Descriptor of the bias tensor (output)
// layerBias Pointer to the memory location of the bias tensor (output)
func (r *RNND) SetLayerBias(h *Handle, layer int32, xD,
wD *TensorD, w cutil.Mem,
biasID int32, biasD *TensorD, layerBias cutil.Mem) error {
return Status(C.miopenSetRNNLayerBias(h.x, r.d, (C.int)(layer), xD.d, wD.d, w.Ptr(), (C.int)(biasID), biasD.d, layerBias.Ptr())).error("SetLayerParam")
}
//ForwardTraining - Execute forward training for recurrent layer
//
//Interface for executing the forward training pass on a RNN.
//
// h MIOpen handle (input)
//
// xD An array of tensor descriptors. These are the
// descriptors to each time step. The first dimension of each descriptor is the
// batch size and may decrease from element n to element n+1 and not increase in size.
// The second dimension is the same for all descriptors in the array and is the input
// vector length. (input)
//
// x Pointer to input tensor (input)
//
// hxD A hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// hx Pointer to the hidden layer input tensor. If hx is NULL,
// then the initial hidden state will be zero initialized. (input)
//
// cxD A cell tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// cx Pointer to the cell layer input tensor. If cx is NULL,
// then the initial cell state will be zero initialized. (input)
//
// wD A weights tensor descriptor (input)
//
// w Pointer to input weights tensor (input)
//
// yD An array of fully packed tensor descriptors associated
// with the output from each time step. The first dimension of the tensor descriptors
// must equal the first dimension of the first descriptor (batch size) in the xDesc
// tensor array. The second dimension of the element of the descriptor array
// depends on the direction mode selected. If the direction mode is unidirectional,
// the second dimension is the hiddenSize. If direction mode is bidirectional
// the second dimension is twice the hiddenSize. (input)
//
// y Pointer to output tensor (output)
//
// hyD A hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// hy Pointer to the hidden layer output tensor. If hy is NULL,
// then the final hidden state will not be saved. (output)
//
// cyD A cell tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// cy Pointer to the cell layer output tensor. If hy is NULL,
// then the final cell state will not be saved. (output)
//
// wspace Pointer to memory allocated for forward training (input)
//
// wspaceSIB Number of allocated bytes in memory for the workspace (input)
//
// rspace Pointer to memory allocated for random states (input / output)
//
// rspaceSIB Number of allocated bytes in memory for use in the forward (input)
func (r *RNND) ForwardTraining(h *Handle,
xD []*TensorD, x cutil.Mem,
hxD *TensorD, hx cutil.Mem,
cxD *TensorD, cx cutil.Mem,
wD *TensorD, w cutil.Mem,
yD []*TensorD, y cutil.Mem,
hyD *TensorD, hy cutil.Mem,
cyD *TensorD, cy cutil.Mem,
wspace cutil.Mem, wspaceSIB uint,
rspace cutil.Mem, rspaceSIB uint) error {
xDc, seqenceLen1 := tensorDarraytomiopenTensorDescriptorArray(xD)
yDc, seqenceLen2 := tensorDarraytomiopenTensorDescriptorArray(yD)
if seqenceLen1 != seqenceLen2 {
return errors.New("(r *RNND)ForwardTraining(): len(xD)!=len(yD)")
}
return Status(C.miopenRNNForwardTraining(h.x, r.d,
seqenceLen1, &xDc[0], x.Ptr(),
hxD.d, hx.Ptr(),
cxD.d, cx.Ptr(),
wD.d, w.Ptr(),
&yDc[0], y.Ptr(),
hyD.d, hy.Ptr(),
cyD.d, cy.Ptr(),
wspace.Ptr(), (C.size_t)(wspaceSIB),
rspace.Ptr(), (C.size_t)(rspaceSIB))).error("(r *RNND)ForwardTraining()")
}
//BackwardData - Execute forward training for recurrent layer
//
//Interface for executing the forward training pass on a RNN.
//
//
// h MIOpen handle (input)
//
// rnnD descriptor type (input)
//
// yD An array of tensor descriptors (input)
//
// y Pointer to input tensor (input)
//
// dyD An array of fully packed tensor descriptors associated
// with the output from each time step. The first dimension of the tensor descriptors
// must equal the first dimension of the first descriptor (batch size) in the xDesc
// tensor array. The second dimension of the element of the descriptor array
// depends on the direction mode selected. If the direction mode is unidirectional,
// the second dimension is the hiddenSize. If direction mode is bidirectional
// the second dimension is twice the hiddenSize. (input)
//
// dy Pointer to the hidden layer input tensor (input)
//
// dhyD hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// dhy Pointer to the cell layer input tensor (input)
//
// dcyD A cell tensor descriptor that has as its first dimension
// the number of layers if the direction mode is unidirectional and twice the
// of layers if the direction mode is bidirectional. The second dimension of
// must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// dcy Pointer to the cell layer input tensor. If dcy is NULL,
// then the initial delta cell state will be zero initialized. (input)
//
// wD A weights tensor descriptor (input)
//
// w Pointer to input weights tensor (input)
//
// hxD An input hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// hx Pointer to the hidden layer input tensor. If hx is NULL,
// then the initial hidden state will be zero initialized. (input)
//
// cxD A input cell tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// cx Pointer to the hidden layer input tensor. If cx is NULL,
// then the initial cell state will be zero initialized. (input)
//
// dxD An array of tensor descriptors. These are the
// input descriptors to each time step. The first dimension of each descriptor is the
// batch size and may decrease from element n to element n+1 and not increase in size.
// The second dimension is the same for all descriptors in the array and is the input
// vector length. (input)
//
//
// dx Pointer to the cell layer output tensor (output)
//
// dhxD A hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// dhx Pointer to the delta hidden layer output tensor. If dhx is NULL
// the hidden gradient will not ouput. (output)
//
// dcxD A tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// dcx Pointer to the cell layer output tensor. If dcx is NULL
// the cell gradient will not ouput. (output)
//
// wspace Pointer to memory allocated for forward training (input)
//
// wspaceSIB Number of allocated bytes in memory for the workspace (input)
//
// rspace Pointer to memory allocated for random states (input / output)
//
// rspaceSIB Number of allocated bytes in memory for use in the forward (input)
func (r *RNND) BackwardData(h *Handle,
yD []*TensorD, y cutil.Mem,
dyD []*TensorD, dy cutil.Mem,
dhyD *TensorD, dhy cutil.Mem,
dcyD *TensorD, dcy cutil.Mem,
wD *TensorD, w cutil.Mem,
hxD *TensorD, hx cutil.Mem,
cxD *TensorD, cx cutil.Mem,
dxD []*TensorD, dx cutil.Mem,
dhxD *TensorD, dhx cutil.Mem,
dcxD *TensorD, dcx cutil.Mem,
wspace cutil.Mem, wspaceSIB uint,
rspace cutil.Mem, rspaceSIB uint) error {
dxDc, seqenceLen1 := tensorDarraytomiopenTensorDescriptorArray(dxD)
dyDc, seqenceLen2 := tensorDarraytomiopenTensorDescriptorArray(dyD)
yDc, seqenceLen3 := tensorDarraytomiopenTensorDescriptorArray(yD)
if seqenceLen1 != seqenceLen2 || seqenceLen1 != seqenceLen3 {
return errors.New("(r *RNND)BackwardData(): len(dxD)!=len(dyD) || len(dxD)!= len(yD)")
}
return Status(C.miopenRNNBackwardData(h.x, r.d,
seqenceLen1, &yDc[0], y.Ptr(),
&dyDc[0], dy.Ptr(),
dhyD.d, dhy.Ptr(),
dcyD.d, dcy.Ptr(),
wD.d, w.Ptr(),
hxD.d, hx.Ptr(),
cxD.d, cx.Ptr(),
&dxDc[0], dx.Ptr(),
dhxD.d, dhx.Ptr(),
dcxD.d, dcx.Ptr(),
wspace.Ptr(), (C.size_t)(wspaceSIB),
rspace.Ptr(), (C.size_t)(rspaceSIB))).error("(r *RNND)BackwardData()")
}
//BackwardWeights - Execute forward training for recurrent layer
//
//Interface for executing the forward training pass on a RNN.
//
//
// h MIOpen handle (input)
//
// rnnD RNN layer descriptor type (input)
//
// xD An array of tensor descriptors. These are the
// input descriptors to each time step. The first dimension of each descriptor is the
// size and may decrease from element n to element n+1 and not increase in size.
// second dimension is the same for all descriptors in the array and is the input
// length. (input)
//
// x to input tensor (input)
//
// hxD A hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// hx to the hidden layer input tensor. If hx is NULL,
// then the initial hidden state will be zero initialized. (input)
//
// yD An array of fully packed tensor descriptors associated
// with the output from each time step. The first dimension of the tensor descriptors
// must equal the first dimension of the first descriptor (batch size) in the xDesc
// tensor array. The second dimension of the element of the descriptor array
// depends on the direction mode selected. If the direction mode is unidirectional,
// the second dimension is the hiddenSize. If direction mode is bidirectional
// the second dimension is twice the hiddenSize. (input)
//
// y Pointer to the output tensor (input)
//
// dwD A weights tensor descriptor (input)
//
// dw Pointer to input weights tensor (input / output)
//
// wspace Pointer to memory allocated for forward training (input)
//
// wspaceSIB Number of allocated bytes in memory for the workspace (input)
//
// rspace Pointer to memory allocated for random states (input)
//
// rspaceSIB Number of allocated bytes in memory for use in the forward (input)
func (r *RNND) BackwardWeights(h *Handle,
xD []*TensorD, x cutil.Mem,
hxD *TensorD, hx cutil.Mem,
yD []*TensorD, y cutil.Mem,
dwD *TensorD, dw cutil.Mem,
wspace cutil.Mem, wspaceSIB uint,
rspace cutil.Mem, rspaceSIB uint) error {
xDc, seqenceLen1 := tensorDarraytomiopenTensorDescriptorArray(xD)
yDc, seqenceLen2 := tensorDarraytomiopenTensorDescriptorArray(yD)
if seqenceLen1 != seqenceLen2 {
return errors.New("(r *RNND)BackwardWeights(): len(xD)!=len(yD)")
}
return Status(C.miopenRNNBackwardWeights(h.x, r.d,
seqenceLen1, &xDc[0], x.Ptr(),
hxD.d, hx.Ptr(),
&yDc[0], y.Ptr(),
dwD.d, dw.Ptr(),
wspace.Ptr(), (C.size_t)(wspaceSIB),
rspace.Ptr(), (C.size_t)(rspaceSIB))).error("(r *RNND)BackwardWeights()")
}
//ForwardInference - Execute forward inference for RNN layer
//
//Interface for executing the forward inference pass on a RNN.
//
// h MIOpen handle (input)
//
// xD An array of tensor descriptors. These are the
// descriptors to each time step. The first dimension of each descriptor is the
// batch size and may decrease from element n to element n+1 and not increase in size.
// The second dimension is the same for all descriptors in the array and is the input
// vector length. (input)
//
// x Pointer to input tensor (input)
//
// hxD A hidden tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)
//
// hx Pointer to the hidden layer input tensor. If hx is NULL,
// then the initial hidden state will be zero initialized. (input)
//
// cxD A cell tensor descriptor that has as its first dimension
// of the number of layers if the direction mode is unidirectional and twice the
// number of layers if the direction mode is bidirectional. The second dimension of
// the descriptor must equal the largest first dimension of the xDesc tensor descriptor
// array. The third dimension equals the hiddenSize. (input)