-
Notifications
You must be signed in to change notification settings - Fork 5
/
cudnnConvolution.go
1447 lines (1306 loc) · 41.5 KB
/
cudnnConvolution.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
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 gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
"github.com/dereklstinson/cutil"
)
/*
Descriptors
*/
//ConvolutionD sets all the convolution info
type ConvolutionD struct {
descriptor C.cudnnConvolutionDescriptor_t
dims C.int
gogc bool
}
const convolutionnd2dtestflag = true
func (c *ConvolutionD) String() string {
cmode, dtype, pad, stride, dilation, err := c.Get()
if err != nil {
return fmt.Sprintf("ConvolutionD{\nError\n}\n")
}
return fmt.Sprintf(
"ConvolutionD{\n%s\n%s\nPad: %v\nStride: %v\nDilation: %v\nAddress: %p\n}\n", cmode.String(), dtype.String(), pad, stride, dilation, c)
}
//CreateConvolutionDescriptor creates a convolution descriptor
func CreateConvolutionDescriptor() (*ConvolutionD, error) {
d := new(ConvolutionD)
err := Status(C.cudnnCreateConvolutionDescriptor(&d.descriptor)).error("NewConvolution2dDescriptor-create")
if err != nil {
return nil, err
}
if setfinalizer {
runtime.SetFinalizer(d, destroyconvolutiondescriptor)
}
return d, nil
}
//Set sets the convolution descriptor
//Input.Type of the filter layout format. If this input is set to CUDNN_TENSOR_NCHW, which is one of the enumerated values allowed by cudnnTensorFormat_t descriptor, then the layout of the filter is as follows:
//
// For N=4, i.e., for a 4D filter descriptor, the filter layout is in the form of KCRS (K represents the number of output feature maps, C the number of input feature maps, R the number of rows per filter, and S the number of columns per filter.)
//
// For N=3, i.e., for a 3D filter descriptor, the number S (number of columns per filter) is omitted.
//
// For N=5 and greater, the layout of the higher dimensions immediately follow RS.
//
// On the other hand, if this input is set to CUDNN_TENSOR_NHWC, then the layout of the filter is as follows:
//
// for N=4, i.e., for a 4D filter descriptor, the filter layout is in the form of KRSC.
//
// For N=3, i.e., for a 3D filter descriptor, the number S (number of columns per filter) is omitted, and the layout of C immediately follows R.
//
// For N=5 and greater, the layout of the higher dimensions are inserted between S and C. See also the description for cudnnTensorFormat_t.
//
// Note:
//
// Length of stride, pad, and dilation need to be len(tensordims) -2.
func (c *ConvolutionD) Set(mode ConvolutionMode, data DataType, pad, stride, dilation []int32) error {
cdata := data.c()
cmode := mode.c()
cpad := int32Tocint(pad)
cstride := int32Tocint(stride)
cdilation := int32Tocint(dilation)
c.dims = C.int(len(pad))
return Status(C.cudnnSetConvolutionNdDescriptor(c.descriptor, c.dims, &cpad[0], &cstride[0], &cdilation[0], cmode, cdata)).error("NewConvolutionNdDescriptor-set")
}
//Get gets returns the values used to make the convolution descriptor
func (c *ConvolutionD) Get() (mode ConvolutionMode, data DataType, pad []int32, stride []int32, dilation []int32, err error) {
if c.dims == 0 {
c.dims = C.CUDNN_DIM_MAX
}
padding := make([]C.int, c.dims)
striding := make([]C.int, c.dims)
dilationing := make([]C.int, c.dims)
var actual C.int
var moded C.cudnnConvolutionMode_t
var dtype C.cudnnDataType_t
err = Status(C.cudnnGetConvolutionNdDescriptor(c.descriptor, c.dims, &actual, &padding[0], &striding[0], &dilationing[0], &moded, &dtype)).error("GetndDescriptor")
c.dims = actual
return ConvolutionMode(moded), DataType(dtype), cintToint32(padding[:actual]), cintToint32(striding[:actual]), cintToint32(dilationing[:actual]), err
}
//SetGroupCount sets the Group Count
func (c *ConvolutionD) SetGroupCount(groupCount int32) error {
err := Status(C.cudnnSetConvolutionGroupCount(c.descriptor, C.int(groupCount))).error("SetGroupCountandMathtype-Group")
return err
}
//SetReorderType sets the reorder type
func (c *ConvolutionD) SetReorderType(r Reorder) error {
return Status(C.cudnnSetConvolutionReorderType(c.descriptor, r.c())).error("SetReorderType")
}
//GetReorderType gets the reorder type
func (c *ConvolutionD) GetReorderType() (r Reorder, err error) {
err = Status(C.cudnnGetConvolutionReorderType(c.descriptor, r.cptr())).error("GetReorderType")
return r, err
}
//SetMathType sets the mathtype
func (c *ConvolutionD) SetMathType(mathtype MathType) error {
x := Status(C.cudnnSetConvolutionMathType(c.descriptor, C.cudnnMathType_t(mathtype)))
return x.error("SetGroupCountandMathtype-Math")
}
//GetOutputDims is a helper function to give the size of the output of of a COnvolutionNDForward
//Each dimension of the (nbDims-2)-D images of the output tensor is computed as followed:
//
// outputDim = 1 + ( inputDim + 2*pad - (((filterDim-1)*dilation)+1) )/convolutionStride;
//
// Note if input and filter are NHWC. cudnn would take the formats as NCHW and output an NCHW
// gocudnn will take that NCHW and format it to an actual NHWC.
func (c *ConvolutionD) GetOutputDims(input *TensorD, filter *FilterD) ([]int32, error) {
cdims := make([]C.int, int32(input.dims))
err := Status(C.cudnnGetConvolutionNdForwardOutputDim(c.descriptor, input.descriptor, filter.descriptor, input.dims, &cdims[0])).error("GetConvolutionNdForwardOutputDim")
if err != nil {
return nil, err
}
fflg := input.frmt
dims := cintToint32(cdims)
switch input.frmt {
case fflg.NHWC():
dims = compatabilityNHWCdimsCudnntoGocudnn(dims)
}
return dims, err
}
//Destroy destroys the ConvolutionDescriptor. If GC is set then it only returns nil.
//Currently GC is set with no option to turn off
func (c *ConvolutionD) Destroy() error {
if setfinalizer || c.gogc {
return nil
}
return destroyconvolutiondescriptor(c)
}
func destroyconvolutiondescriptor(c *ConvolutionD) error {
return Status(C.cudnnDestroyConvolutionDescriptor(c.descriptor)).error("DestroyConvolutionDescriptor")
}
//GetBackwardDataWorkspaceSize is a helper function that will return the minimum Size of the workspace to be passed by the convolution given an algo.
func (c *ConvolutionD) GetBackwardDataWorkspaceSize(
handle *Handle,
wD *FilterD,
dyD *TensorD,
dxD *TensorD,
algo ConvBwdDataAlgo) (uint, error) {
var sizebytes C.size_t
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetConvolutionBackwardDataWorkspaceSize(
handle.x,
wD.descriptor,
dyD.descriptor,
c.descriptor,
dxD.descriptor,
algo.c(),
&sizebytes)).error("(c *ConvolutionD) GetBackwardDataWorkspaceSize")
})
} else {
err = Status(C.cudnnGetConvolutionBackwardDataWorkspaceSize(
handle.x,
wD.descriptor,
dyD.descriptor,
c.descriptor,
dxD.descriptor,
algo.c(),
&sizebytes)).error("(c *ConvolutionD) GetBackwardDataWorkspaceSize")
}
return uint(sizebytes), err
}
//BackwardData does the backwards convolution on data
//
//This function computes the convolution data gradient of the tensor dy,
//where y is the output of the forward convolution in (*ConvolutionD)Forward().
//It uses the specified algo, and returns the results in the output tensor dx.
//Scaling factors alpha and beta can be used to scale the computed result or accumulate with the current dx.
//
//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.
// ----
// ---
// wD(input):
//
// For previously set input tensor descriptor.
// ---
// ----
// w(input):
//
// Data pointer to GPU memory associated with the tensor descriptor xD.
//
// ----
// ---
// dyD(input):
//
// For previously set input tensor descriptor of dy.
// ---
// ----
// dy(input):
//
// Data pointer to GPU memory associated with the input tensor desctiptor.(Holds back propigation errors)
// ----
// ---
// algo(input):
//
// Enumerant that specifies which backward data convolution algorithm shoud be used to compute the results.
// ---
// ----
// wspace, wspaceSIB(inputs):
//
// Data pointer and size in bytes of workspace needed for algo passed. If no wspace is need nil can be passed.
// ----
// ---
// dxD(input):
// For previously set output tensor descriptor of dx.
// ---
// ----
// dx(input/output):
// Data pointer to GPU memory associated with the output tensor desctiptor.(Holds back propigation errors for layer it received its forward inputs.)
// ----
//
//Supported Configurations
// ----
// Config: "TRUE_HALF_CONFIG (only compute capability 5.3 and later)."
// TensorD (wD,dyD,dxD): (*DataType)Half()
// ConvolutionD: (*DataType)Half()
// ----
// ---
// Config: "PSEUDO_HALF_CONFIG"
// TensorD (wD,dyD,dxD): (*DataType)Half()
// ConvolutionD: (*DataType)Float()
// ---
// ----
// Config: "FLOAT_CONFIG"
// TensorD (wD,dyD,dxD): (*DataType)Float()
// ConvolutionD: (*DataType)Float()
// ----
// ---
// Config: "DOUBLE_CONFIG"
// TensorD (wD,dyD,dxD): (*DataType)Double()
// ConvolutionD: (*DataType)Double()
// ---
//
//Note:
//Specifying a separate algorithm can cause changes in performance, support and computation determinism.
//
//Table of algorithm with configs can be found at. (gocudnn flag names are similar to cudnn)
// https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html#cudnnConvolutionBackwardData
//
//Possible Error Returns:
// nil:
//
// The function launched successfully.
//
// CUDNN_STATUS_NOT_SUPPORTED:
//
// At least one of the following conditions are met:
// 1) dyD or dxD have negative tensor striding
// 2) dyD, wD or dxD has a number of dimensions that is not 4 or 5
// 3) The chosen algo does not support the parameters provided; see above for exhaustive list of parameter support for each algo
// 4) dyD or wD indicate an output channel count that isn't a multiple of group count (if group count has been set in ConvolutionD).
//
// CUDNN_STATUS_BAD_PARAM:
//
// At least one of the following conditions are met:
// 1) At least one of the following is NULL: handle, dyD, wD, ConvolutionD, dxD, dy, w, dx, alpha, beta
// 2) wD and dyD have a non-matching number of dimensions
// 3) wD and dxD have a non-matching number of dimensions
// 4) wD has fewer than three number of dimensions
// 5) wD, dxD and dyD have a non-matching data type.
// 6) wD and dxD have a non-matching number of input feature maps per image (or group in case of Grouped Convolutions).
// 7) dyD's spatial sizes do not match with the expected size as determined by (*ConvolutionD)GetOutputDims().
//
// CUDNN_STATUS_MAPPING_ERROR:
//
// An error occurs during the texture binding of the filter data or the input differential tensor data
//
// CUDNN_STATUS_EXECUTION_FAILED:
//
// The function failed to launch on the GPU.
//
func (c *ConvolutionD) BackwardData(
handle *Handle,
alpha float64,
wD *FilterD, w cutil.Mem,
dyD *TensorD, dy cutil.Mem,
algo ConvBwdDataAlgo,
wspace cutil.Mem, wspaceSIB uint,
beta float64,
dxD *TensorD, dx cutil.Mem,
) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
if wspace == nil {
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor,
w.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
nil,
(C.size_t)(wspaceSIB),
b.CPtr(),
dxD.descriptor,
dx.Ptr(),
)).error("(c *ConvolutionD) BackwardData")
}
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor,
w.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
(C.size_t)(wspaceSIB),
b.CPtr(),
dxD.descriptor,
dx.Ptr(),
)).error("(c *ConvolutionD) BackwardData")
})
}
if wspace == nil {
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor,
w.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
nil,
(C.size_t)(wspaceSIB),
b.CPtr(),
dxD.descriptor,
dx.Ptr(),
)).error("(c *ConvolutionD) BackwardData")
}
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor,
w.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
(C.size_t)(wspaceSIB),
b.CPtr(),
dxD.descriptor,
dx.Ptr(),
)).error("(c *ConvolutionD) BackwardData")
}
//BackwardDataUS is like BackwardData but uses unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) BackwardDataUS(
handle *Handle,
alpha float64,
wD *FilterD, w unsafe.Pointer,
dyD *TensorD, dy unsafe.Pointer,
algo ConvBwdDataAlgo,
wspace unsafe.Pointer, wspacesize uint,
beta float64,
dxD *TensorD, dx unsafe.Pointer,
) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor, w,
dyD.descriptor, dy,
c.descriptor,
algo.c(),
wspace, (C.size_t)(wspacesize),
b.CPtr(),
dxD.descriptor, dx,
)).error("(c *ConvolutionD) BackwardDataUS")
})
}
return Status(C.cudnnConvolutionBackwardData(
handle.x,
a.CPtr(),
wD.descriptor, w,
dyD.descriptor, dy,
c.descriptor,
algo.c(),
wspace, (C.size_t)(wspacesize),
b.CPtr(),
dxD.descriptor, dx,
)).error("(c *ConvolutionD) BackwardDataUS")
}
//Im2Col transformes the multiDim tensors into 2d tensors for speed up in calculation at the cost of memory.
func (c *ConvolutionD) Im2Col(
handle *Handle,
xD *TensorD,
x cutil.Mem,
wD *FilterD,
buffer cutil.Mem,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnIm2Col(
handle.x,
xD.descriptor,
x.Ptr(),
wD.descriptor,
c.descriptor,
buffer.Ptr(),
)).error("(c *ConvolutionD) Im2Col")
})
}
return Status(C.cudnnIm2Col(
handle.x,
xD.descriptor,
x.Ptr(),
wD.descriptor,
c.descriptor,
buffer.Ptr(),
)).error("(c *ConvolutionD) Im2Col")
}
//Im2ColUS is like IN2Col but using unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) Im2ColUS(
handle *Handle,
xD *TensorD, x unsafe.Pointer,
wD *FilterD,
buffer unsafe.Pointer,
) error {
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnIm2Col(
handle.x,
xD.descriptor, x,
wD.descriptor,
c.descriptor,
buffer,
)).error("(c *ConvolutionD) Im2ColUS")
})
}
return Status(C.cudnnIm2Col(
handle.x,
xD.descriptor, x,
wD.descriptor,
c.descriptor,
buffer,
)).error("(c *ConvolutionD) Im2ColUS")
}
//BackwardBias is used to compute the bias gradient for batch convolution db is returned
func (c *ConvolutionD) BackwardBias(
handle *Handle,
alpha float64,
dyD *TensorD,
dy cutil.Mem,
beta float64,
dbD *TensorD,
db cutil.Mem) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnConvolutionBackwardBias(handle.x, a.CPtr(), dyD.descriptor, dy.Ptr(), b.CPtr(), dbD.descriptor, db.Ptr())).error("(c *ConvolutionD) BackwardBias")
})
}
return Status(C.cudnnConvolutionBackwardBias(handle.x, a.CPtr(), dyD.descriptor, dy.Ptr(), b.CPtr(), dbD.descriptor, db.Ptr())).error("(c *ConvolutionD) BackwardBias")
}
//BackwardBiasUS is like BackwardBias but using unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) BackwardBiasUS(
handle *Handle,
alpha float64,
dyD *TensorD, dy unsafe.Pointer,
beta float64,
dbD *TensorD, db unsafe.Pointer) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnConvolutionBackwardBias(handle.x, a.CPtr(), dyD.descriptor, dy, b.CPtr(), dbD.descriptor, db)).error(" (c *ConvolutionD) BackwardBiasUS")
})
}
return Status(C.cudnnConvolutionBackwardBias(handle.x, a.CPtr(), dyD.descriptor, dy, b.CPtr(), dbD.descriptor, db)).error(" (c *ConvolutionD) BackwardBiasUS")
}
//GetBackwardFilterWorkspaceSize is a helper function that will return the minimum Size of the workspace to be passed by the convolution given an algo.
func (c *ConvolutionD) GetBackwardFilterWorkspaceSize(
handle *Handle,
xD *TensorD,
dyD *TensorD,
dwD *FilterD,
algo ConvBwdFiltAlgo) (uint, error) {
var err error
var sizebytes C.size_t
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetConvolutionBackwardFilterWorkspaceSize(
handle.x,
xD.descriptor,
dyD.descriptor,
c.descriptor,
dwD.descriptor,
algo.c(),
&sizebytes)).error("(c *ConvolutionD) GetBackwardFilterWorkspaceSize")
})
} else {
err = Status(C.cudnnGetConvolutionBackwardFilterWorkspaceSize(
handle.x,
xD.descriptor,
dyD.descriptor,
c.descriptor,
dwD.descriptor,
algo.c(),
&sizebytes)).error("(c *ConvolutionD) GetBackwardFilterWorkspaceSize")
}
return uint(sizebytes), err
}
//BackwardFilter does the backwards convolution
func (c *ConvolutionD) BackwardFilter(
handle *Handle,
alpha float64,
xD *TensorD, x cutil.Mem,
dyD *TensorD, dy cutil.Mem,
algo ConvBwdFiltAlgo,
wspace cutil.Mem, wspacesize uint,
beta float64,
dwD *FilterD, dw cutil.Mem,
) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
if wspace == nil {
if cudnndebugmode {
fmt.Println("wspace is nil")
}
return Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor,
x.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
nil,
C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor,
dw.Ptr(),
)).error("(c *ConvolutionD) BackwardFilter")
}
if cudnndebugmode {
fmt.Println("is not nil")
}
return Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor,
x.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor,
dw.Ptr(),
)).error("(c *ConvolutionD) BackwardFilter")
})
} else {
if wspace == nil {
if cudnndebugmode {
fmt.Println("wspace is nil")
}
err = Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor,
x.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
nil,
C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor,
dw.Ptr(),
)).error("(c *ConvolutionD) BackwardFilter")
} else {
if cudnndebugmode {
fmt.Println("is not nil")
}
err = Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor,
x.Ptr(),
dyD.descriptor,
dy.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor,
dw.Ptr(),
)).error("(c *ConvolutionD) BackwardFilter")
}
}
if cudnndebugmode {
if err != nil {
fmt.Println("checking the addresses")
fmt.Println("handle.x", handle.x)
fmt.Println("a.CPtr()", a.CPtr())
fmt.Println("xD.descriptor", xD.descriptor)
fmt.Println("x.Ptr()", x.Ptr())
fmt.Println("dyD.descriptor", dyD.descriptor)
fmt.Println("dy.Ptr()", dy.Ptr())
fmt.Println("c.descriptor", c.descriptor)
fmt.Println("algo.c()", algo.c())
if wspace == nil {
fmt.Println("wspace", nil)
} else {
fmt.Println("wspace.Ptr()", wspace.Ptr())
}
fmt.Println("wspacesize", wspacesize)
fmt.Println("b.Cptr()", b.CPtr())
fmt.Println("dwD.descriptor", dwD.descriptor)
fmt.Println("dw.Ptr()", dw.Ptr())
//going to check the output
fmt.Printf("\nAlgo: %v", algo)
fmt.Printf("\n%v,\nxD: %v,\ndyD: \n%v,\ndwD: %v", c, xD, dyD, dwD)
fmt.Println(c.GetOutputDims(xD, dwD))
}
}
return err
}
//BackwardFilterUS is like BackwardFilter but using unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) BackwardFilterUS(
handle *Handle,
alpha float64,
xD *TensorD, x unsafe.Pointer,
dyD *TensorD, dy unsafe.Pointer,
algo ConvBwdFiltAlgo,
wspace unsafe.Pointer, wspacesize uint,
beta float64,
dwD *FilterD, dw unsafe.Pointer,
) error {
a := cscalarbydatatype(dyD.dtype, alpha)
b := cscalarbydatatype(dyD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor, x,
dyD.descriptor, dy,
c.descriptor,
algo.c(),
wspace, C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor, dw,
)).error("(c *ConvolutionD) BackwardFilterUS")
})
}
return Status(C.cudnnConvolutionBackwardFilter(
handle.x,
a.CPtr(),
xD.descriptor, x,
dyD.descriptor, dy,
c.descriptor,
algo.c(),
wspace, C.size_t(wspacesize),
b.CPtr(),
dwD.descriptor, dw,
)).error("(c *ConvolutionD) BackwardFilterUS")
}
//GetForwardWorkspaceSize is a helper function that will return the minimum Size of the workspace to be passed by the convolution given an algo.
func (c *ConvolutionD) GetForwardWorkspaceSize(
handle *Handle,
xD *TensorD,
wD *FilterD,
yD *TensorD,
algo ConvFwdAlgo) (uint, error) {
var sizebytes C.size_t
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
return Status(C.cudnnGetConvolutionForwardWorkspaceSize(handle.x, xD.descriptor, wD.descriptor, c.descriptor, yD.descriptor, algo.c(), &sizebytes)).error("(c *ConvolutionD) GetForwardWorkspaceSize")
})
} else {
err = Status(C.cudnnGetConvolutionForwardWorkspaceSize(handle.x, xD.descriptor, wD.descriptor, c.descriptor, yD.descriptor, algo.c(), &sizebytes)).error("(c *ConvolutionD) GetForwardWorkspaceSize")
}
return uint(sizebytes), err
}
/* Convolution functions: All of the form "output = alpha * Op(inputs) + beta * output" */
//Forward Function to perform the forward pass for batch convolution
func (c *ConvolutionD) Forward(
handle *Handle,
alpha float64,
xD *TensorD, x cutil.Mem,
wD *FilterD, w cutil.Mem,
algo ConvFwdAlgo,
wspace cutil.Mem, wspacesize uint,
beta float64,
yD *TensorD, y cutil.Mem) error {
a := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
var err error
if handle.w != nil {
err = handle.w.Work(func() error {
if wspace == nil {
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x.Ptr(), wD.descriptor, w.Ptr(),
c.descriptor, algo.c(), nil, C.size_t(wspacesize), b.CPtr(), yD.descriptor, y.Ptr())).error("(c *ConvolutionD) Forward")
}
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x.Ptr(), wD.descriptor, w.Ptr(),
c.descriptor, algo.c(), wspace.Ptr(), C.size_t(wspacesize), b.CPtr(), yD.descriptor, y.Ptr())).error("(c *ConvolutionD) Forward")
})
} else {
if wspace == nil {
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x.Ptr(), wD.descriptor, w.Ptr(),
c.descriptor, algo.c(), nil, C.size_t(wspacesize), b.CPtr(), yD.descriptor, y.Ptr())).error("(c *ConvolutionD) Forward")
}
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x.Ptr(), wD.descriptor, w.Ptr(),
c.descriptor, algo.c(), wspace.Ptr(), C.size_t(wspacesize), b.CPtr(), yD.descriptor, y.Ptr())).error("(c *ConvolutionD) Forward")
}
if cudnndebugmode {
if err != nil {
fmt.Println("\nError for ConvForward\n", "alpha: ", a, "\nbeta: ", b, "\nxD: ", xD, "\nx :", x, "\nwD :", wD, "\nw: ", w, "\nwspace: ", wspace, "\nwspacesize: ", wspacesize, "\nyD: ", yD, "\ny: ", y)
fmt.Printf("\n%v\n", wD)
fmt.Printf("\n%v\n", c)
fmt.Printf("\n%v", algo)
actualwspacesize, err := c.GetForwardWorkspaceSize(handle, xD, wD, yD, algo)
fmt.Println("Workspace Size Compare passed/wanted:", wspacesize, actualwspacesize, err)
panic(err)
}
}
return err
}
//ForwardUS is like Forward but using unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) ForwardUS(
handle *Handle,
alpha float64,
xD *TensorD, x unsafe.Pointer,
wD *FilterD, w unsafe.Pointer,
algo ConvFwdAlgo,
wspace unsafe.Pointer, wspacesize uint,
beta float64,
yD *TensorD, y unsafe.Pointer) error {
a := cscalarbydatatype(yD.dtype, alpha)
b := cscalarbydatatype(yD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x, wD.descriptor, w,
c.descriptor, algo.c(), wspace, C.size_t(wspacesize), b.CPtr(), yD.descriptor, y)).error("(c *ConvolutionD) ForwardUS")
})
}
return Status(C.cudnnConvolutionForward(handle.x, a.CPtr(), xD.descriptor, x, wD.descriptor, w,
c.descriptor, algo.c(), wspace, C.size_t(wspacesize), b.CPtr(), yD.descriptor, y)).error("(c *ConvolutionD) ForwardUS")
}
//BiasActivationForward info can be found at:
//
//https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html#cudnnConvolutionBiasActivationForward
//
//Fused conv/bias/activation operation : y = Act( alpha1 * conv(x) + alpha2 * z + bias )
func (c *ConvolutionD) BiasActivationForward(
handle *Handle,
alpha1 float64,
xD *TensorD, x cutil.Mem,
wD *FilterD, w cutil.Mem,
algo ConvFwdAlgo,
wspace cutil.Mem,
wspacesize uint,
alpha2 float64,
zD *TensorD, z cutil.Mem,
biasD *TensorD, bias cutil.Mem,
aD *ActivationD,
yD *TensorD, y cutil.Mem,
) error {
a1 := cscalarbydatatype(yD.dtype, alpha1)
a2 := cscalarbydatatype(yD.dtype, alpha2)
if handle.w != nil {
return handle.w.Work(func() error {
if wspace == nil {
return Status(
C.cudnnConvolutionBiasActivationForward(
handle.x,
a1.CPtr(),
xD.descriptor,
x.Ptr(),
wD.descriptor,
w.Ptr(),
c.descriptor,
algo.c(),
nil,
C.size_t(wspacesize),
a2.CPtr(),
zD.descriptor,
z.Ptr(),
biasD.descriptor,
bias.Ptr(),
aD.descriptor,
yD.descriptor,
y.Ptr(),
)).error("(c *ConvolutionD) BiasActivationForward")
}
return Status(
C.cudnnConvolutionBiasActivationForward(
handle.x,
a1.CPtr(),
xD.descriptor,
x.Ptr(),
wD.descriptor,
w.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
C.size_t(wspacesize),
a2.CPtr(),
zD.descriptor,
z.Ptr(),
biasD.descriptor,
bias.Ptr(),
aD.descriptor,
yD.descriptor,
y.Ptr(),
)).error("(c *ConvolutionD) BiasActivationForward")
})
}
if wspace == nil {
return Status(
C.cudnnConvolutionBiasActivationForward(
handle.x,
a1.CPtr(),
xD.descriptor,
x.Ptr(),
wD.descriptor,
w.Ptr(),
c.descriptor,
algo.c(),
nil,
C.size_t(wspacesize),
a2.CPtr(),
zD.descriptor,
z.Ptr(),
biasD.descriptor,
bias.Ptr(),
aD.descriptor,
yD.descriptor,
y.Ptr(),
)).error("(c *ConvolutionD) BiasActivationForward")
}
return Status(
C.cudnnConvolutionBiasActivationForward(
handle.x,
a1.CPtr(),
xD.descriptor,
x.Ptr(),
wD.descriptor,
w.Ptr(),
c.descriptor,
algo.c(),
wspace.Ptr(),
C.size_t(wspacesize),
a2.CPtr(),
zD.descriptor,
z.Ptr(),
biasD.descriptor,
bias.Ptr(),
aD.descriptor,
yD.descriptor,
y.Ptr(),
)).error("(c *ConvolutionD) BiasActivationForward")
}
//BiasActivationForwardUS is like BiasActivationForward but using unsafe.Pointer instead of cutil.Mem
func (c *ConvolutionD) BiasActivationForwardUS(
handle *Handle,
alpha1 float64,
xD *TensorD, x unsafe.Pointer,
wD *FilterD, w unsafe.Pointer,
algo ConvFwdAlgo,
wspace unsafe.Pointer, wspacesize uint,
alpha2 float64,
zD *TensorD, z unsafe.Pointer,
biasD *TensorD, bias unsafe.Pointer,
aD *ActivationD,
yD *TensorD, y unsafe.Pointer,
) error {
a1 := cscalarbydatatype(yD.dtype, alpha1)
a2 := cscalarbydatatype(yD.dtype, alpha2)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(
C.cudnnConvolutionBiasActivationForward(
handle.x,
a1.CPtr(),
xD.descriptor, x,
wD.descriptor, w,
c.descriptor,
algo.c(),
wspace, C.size_t(wspacesize),
a2.CPtr(),
zD.descriptor, z,
biasD.descriptor, bias,
aD.descriptor,
yD.descriptor, y,
)).error("(c *ConvolutionD) BiasActivationForwardUS")
})
}
return Status(
C.cudnnConvolutionBiasActivationForward(