forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnTensor_test.go
99 lines (93 loc) · 2.31 KB
/
cudnnTensor_test.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
package gocudnn
import (
"fmt"
"testing"
)
func TestCreateTensorDescriptor(t *testing.T) {
//checkarray func
checkarrays := func(a, b []int32) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
var (
frmt TensorFormat
dtype DataType
cmode ConvolutionMode
)
tensor, err := CreateTensorDescriptor()
if err != nil {
t.Error(err)
}
oshape := []int32{10, 3, 36, 36}
//NCHW N=Batches, C=Channels/Feature Maps, H=Height,W=Width for a tensor
err = tensor.Set(frmt.NCHW(), dtype.Float(), oshape, nil) //Since frmt is not set to strided then last option can be nil
if err != nil {
t.Error(err)
}
ostride := make([]int32, 4)
strider := int32(1)
for i := len(ostride) - 1; i >= 0; i-- {
ostride[i] = strider
strider *= oshape[i]
}
gfrmt, gtype, gshape, gstride, err := tensor.Get()
if !checkarrays(oshape, gshape) {
t.Error("Not Matching", oshape, gshape)
}
if !checkarrays(ostride, gstride) {
t.Error("Not Matching", ostride, gstride)
}
if gfrmt != frmt {
t.Error("Not Matching", gfrmt.String(), frmt.String())
}
if gtype != dtype {
t.Error("Not Matching", gtype.String(), dtype.String())
}
filter, err := CreateFilterDescriptor()
if err != nil {
t.Error(err)
}
//dtype,frmt don't need their methods recalled since their methods also change their value.
//N = Number of Output Feature Maps, C= Number of Input Feature Maps (C needs to be 3 because of the previous tensor), H=Height ,W=Width
ofshape := []int32{20, 3, 5, 5}
err = filter.Set(dtype, frmt, ofshape)
if err != nil {
t.Error(err)
}
fdtype, ffrmt, fgshape, err := filter.Get()
if err != nil {
t.Error(err)
}
if !checkarrays(ofshape, fgshape) {
if !checkarrays(oshape, gshape) {
t.Error("Not Matching", ofshape, fgshape)
}
}
if ffrmt != frmt {
t.Error("Not Matching", ffrmt.String(), frmt.String())
}
if fdtype != dtype {
t.Error("Not Matching", fdtype.String(), dtype.String())
}
convolution, err := CreateConvolutionDescriptor()
if err != nil {
t.Error(err)
}
err = convolution.Set(cmode.CrossCorrelation(), dtype, []int32{0, 0}, []int32{3, 3}, []int32{1, 1})
if err != nil {
t.Error(err)
}
//34-5=29 29/3
outdims, err := convolution.GetOutputDims(tensor, filter)
if err != nil {
t.Error(err)
}
fmt.Println(outdims)
}