-
Notifications
You must be signed in to change notification settings - Fork 35
/
tensor_test.go
166 lines (146 loc) · 4.52 KB
/
tensor_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
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
package gotorch_test
import (
"io/ioutil"
"os"
"runtime"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/assert"
torch "github.com/wangkuiyi/gotorch"
"github.com/wangkuiyi/gotorch/nn/initializer"
)
func TestTensorDetach(t *testing.T) {
x := torch.RandN([]int64{1}, true)
y := x.Detach()
assert.NotNil(t, y.T)
initializer.Zeros(&y)
assert.Equal(t, float32(0.0), x.Item())
}
func TestFromBlob(t *testing.T) {
data := [2][3]float32{{1.0, 1.1, 1.2}, {2, 3, 4}}
out := torch.FromBlob(unsafe.Pointer(&data), torch.Float, []int64{2, 3})
assert.Equal(t, []int64{2, 3}, out.Shape())
}
func TestTensorString(t *testing.T) {
data := [2][3]float32{{1.0, 1.1, 1.2}, {2, 3, 4}}
out := torch.FromBlob(unsafe.Pointer(&data), torch.Float, []int64{2, 3})
g := ` 1.0000 1.1000 1.2000
2.0000 3.0000 4.0000
[ CPUFloatType{2,3} ]`
assert.Equal(t, g, out.String())
}
func TestTensorGrad(t *testing.T) {
a := torch.RandN([]int64{10, 10}, true)
assert.NotNil(t, a.Grad().T)
// According to libtorch document https://bit.ly/2QnwHrI, either a
// tensor that requires grad or not, the grad() method returns a tensor.
//
/// This function returns an undefined tensor by default and returns a
/// defined tensor the first time a call to `backward()` computes
/// gradients for this Tensor. The attribute will then contain the
/// gradients computed and future calls to `backward()` will accumulate
/// (add) gradients into it.
b := torch.RandN([]int64{10, 10}, false)
assert.NotNil(t, b.Grad().T)
}
func TestCastTo(t *testing.T) {
a := torch.NewTensor([]int64{1, 2})
b := a.CastTo(torch.Float)
assert.Equal(t, torch.Float, b.Dtype())
b = a.To(torch.NewDevice("cpu"))
assert.Equal(t, torch.Long, b.Dtype())
b = a.To(torch.NewDevice("cpu"), torch.Float)
assert.Equal(t, torch.Float, b.Dtype())
}
func TestCUDA(t *testing.T) {
a := assert.New(t)
device := getDefaultDevice()
input := torch.NewTensor([][]float32{{1, 2}, {3, 4}})
if !torch.IsCUDAAvailable() {
// CUDA should panics on CPU device
a.Panics(func() {
input.CUDA(device, false)
})
a.Panics(func() {
input.CUDA(device, true)
})
return
}
b := input.CUDA(device, false)
a.Equal(" 1 2\n 3 4\n[ CUDAFloatType{2,2} ]", b.String())
c := input.CUDA(device, true)
torch.GetCurrentCUDAStream(device).Synchronize()
a.Equal(" 1 2\n 3 4\n[ CUDAFloatType{2,2} ]", c.String())
}
func TestCopyTo(t *testing.T) {
device := torch.NewDevice("cpu")
a := torch.NewTensor([]int64{1, 2})
b := a.CopyTo(device)
assert.True(t, torch.Equal(a, b))
}
func TestDim(t *testing.T) {
a := torch.RandN([]int64{2, 3}, false)
assert.Equal(t, int64(2), a.Dim())
}
func TestShape(t *testing.T) {
a := torch.RandN([]int64{2, 3}, false)
assert.Equal(t, int64(2), a.Shape()[0])
assert.Equal(t, int64(3), a.Shape()[1])
// a.Argmax returns a 0-dim tensor
b := a.Argmax()
assert.Equal(t, 0, len(b.Shape()))
}
func TestSave(t *testing.T) {
file, e := ioutil.TempFile("", "gotroch-test-save-*")
assert.NoError(t, e)
defer os.Remove(file.Name())
a := torch.RandN([]int64{2, 3}, false)
a.Save(file.Name())
b := torch.Load(file.Name())
assert.EqualValues(t, a.Shape(), b.Shape())
assert.Equal(t, a.Dtype(), b.Dtype())
assert.Equal(t, a.String(), b.String())
}
func TestSetData(t *testing.T) {
a := torch.Full([]int64{2, 3}, 0, false)
b := torch.Ones([]int64{2, 3}, false)
assert.False(t, torch.Equal(a, b))
b.SetData(a)
assert.True(t, torch.Equal(a, b))
}
func TestTensorIndex(t *testing.T) {
a := torch.NewTensor([][]float32{{1, 2}, {3, 4}})
assert.Equal(t, float32(1), a.Index(0, 0).Item().(float32))
assert.Equal(t, float32(2), a.Index(0, 1).Item().(float32))
assert.Equal(t, float32(3), a.Index(1, 0).Item().(float32))
assert.Equal(t, float32(4), a.Index(1, 1).Item().(float32))
assert.Panics(t, func() { a.Index(0).Item() })
assert.Panics(t, func() { a.Index(0, 0, 0).Item() })
}
func TestTensorPinMemory(t *testing.T) {
a := torch.NewTensor([][]float32{{1, 2}, {3, 4}})
b := a.PinMemory()
if torch.IsCUDAAvailable() {
assert.Equal(t, " 1 2\n 3 4\n[ CUDAFloatType{2,2} ]", b.String())
} else {
assert.Equal(t, " 1 2\n 3 4\n[ CPUFloatType{2,2} ]", b.String())
}
}
func TestTensorGC(t *testing.T) {
torch.GC()
defer torch.FinishGC()
runtime.LockOSThread()
c := make(chan torch.Tensor, 0)
{
torch.NewTensor([][]float32{{1, 2}, {3, 4}})
go func() {
a := torch.NewTensor([][]float32{{1, 2}, {3, 4}})
c <- a
time.Sleep(time.Second)
runtime.KeepAlive(&a)
}()
}
<-c
assert.Eventually(t, func() bool { torch.GC(); return true }, 10*time.Millisecond, 10*time.Microsecond)
}