-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathjit_test.go
223 lines (178 loc) · 4.5 KB
/
jit_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
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
package torch
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
const sumScript = `
def sum(a, b):
return a + b
`
const tupleInput = `
def sum(tup : Tuple[Tensor, Tensor]):
t0, t1 = tup
return t0 + t1
`
const sumAndSubs = `
def sum_sub(a, b):
return (a + b, a - b)
`
func Test_CompileTorchScript(t *testing.T) {
module, err := CompileTorchScript(sumScript)
if err != nil {
t.Error(err)
}
if len(module.GetMethodNames()) != 1 || module.GetMethodNames()[0] != "sum" {
t.Error("invalid methods in module", module.GetMethodNames())
}
method, err := module.GetMethod("sum")
if err != nil {
t.Error(err)
}
if method == nil {
t.Error("method was nil")
}
args := method.Arguments()
if args[0].Name != "a" {
t.Error("wrong arg name returned", args[0].Name)
}
if args[1].Name != "b" {
t.Error("wrong arg name returned", args[1].Name)
}
if args[0].Type != "Tensor" {
t.Error("wrong input type returned")
}
returns := method.Returns()
if len(returns) != 1 {
t.Error("sum should only return one tensor")
}
a, _ := NewTensor([]float32{1, 2})
b, _ := NewTensor([]float32{1, 2})
res, err := method.Run(a, b)
if err != nil {
t.Fatal(err)
}
if res.(*Tensor).Value().([]float32)[0] != 2 {
t.Error("1 + 1 should equal 2 but got", res.(*Tensor).Value())
}
if res.(*Tensor).Value().([]float32)[1] != 4 {
t.Error("2 + 2 should equal 4 but got", res.(*Tensor).Value())
}
}
func Test_InvalidTorchScript(t *testing.T) {
_, err := CompileTorchScript("should_compile")
if err == nil {
t.Error("should have returned an error")
}
if !strings.HasPrefix(err.Error(), "expected def but found 'ident' here:") {
t.Error("wrong message returned", err)
}
}
func Test_GetMethodMissingMethod(t *testing.T) {
module, err := CompileTorchScript(sumScript)
if err != nil {
t.Error(err)
}
_, err = module.GetMethod("this_wont_exist")
if err == nil {
t.Error("should return an error")
}
if err.Error() != "Method 'this_wont_exist' is not defined" {
t.Error("wrong message returned", err)
}
}
func Test_RunWithWrongArguments(t *testing.T) {
module, err := CompileTorchScript(sumScript)
if err != nil {
t.Error(err)
}
method, err := module.GetMethod("sum")
if err != nil {
t.Error(err)
}
_, err = method.Run()
if err == nil {
t.Error("should return error")
}
if err.Error() != "sum() is missing value for argument 'a'. Declaration: sum(Tensor a, Tensor b) -> Tensor" {
t.Error("wrong message returned", err)
}
}
func Test_TupleInput(t *testing.T) {
module, err := CompileTorchScript(tupleInput)
if err != nil {
t.Error(err)
}
a, _ := NewTensor([]float32{1})
b, _ := NewTensor([]float32{1})
res, err := module.RunMethod("sum", Tuple{a, b})
if err != nil {
t.Fatal(err)
}
sum := res.(*Tensor)
if sum.Value().([]float32)[0] != 2 {
t.Error("1 + 1 should equal 2 but got", res.(*Tensor).Value())
}
method, _ := module.GetMethod("sum")
if method.Arguments()[0].Type != "(Tensor, Tensor)" {
t.Error("wrong return type for method", method.Arguments())
}
}
func Test_TupleReturn(t *testing.T) {
module, err := CompileTorchScript(sumAndSubs)
if err != nil {
t.Error(err)
}
a, _ := NewTensor([]float32{1})
b, _ := NewTensor([]float32{1})
res, err := module.RunMethod("sum_sub", a, b)
if err != nil {
t.Fatal(err)
}
sum := res.(Tuple).Get(0).(*Tensor)
if sum.Value().([]float32)[0] != 2 {
t.Error("1 + 1 should equal 2 but got", res.(*Tensor).Value())
}
sub := res.(Tuple).Get(1).(*Tensor)
if sub.Value().([]float32)[0] != 0 {
t.Error("1 - 1 should equal 0 but got", res.(*Tensor).Value())
}
// Check returns
method, _ := module.GetMethod("sum_sub")
if method.Returns()[0].Type != "(Tensor, Tensor)" {
t.Error("wrong return type for method", method.Returns())
}
}
func Test_SaveAndLoadJITModule(t *testing.T) {
dir, err := ioutil.TempDir("", "modules")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
module, err := CompileTorchScript(sumScript)
if err != nil {
t.Error(err)
}
err = module.Save(path.Join(dir, "sum.pt"))
if err != nil {
t.Error(err)
}
loaded, err := LoadJITModule(path.Join(dir, "sum.pt"))
if err != nil {
t.Error(err)
}
a, _ := NewTensor([]float32{1, 2})
b, _ := NewTensor([]float32{1, 2})
res, err := loaded.RunMethod("sum", a, b)
if err != nil {
t.Fatal(err)
}
if res.(*Tensor).Value().([]float32)[0] != 2 {
t.Error("1 + 1 should equal 2 but got", res.(*Tensor).Value())
}
if res.(*Tensor).Value().([]float32)[1] != 4 {
t.Error("2 + 2 should equal 4 but got", res.(*Tensor).Value())
}
}