File: pytorch_test.go

package info (click to toggle)
golang-github-nlpodyssey-gopickle 0.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: python: 32; makefile: 5
file content (326 lines) | stat: -rw-r--r-- 9,432 bytes parent folder | download
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
// Copyright 2020 NLP Odyssey Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pytorch

import (
	"fmt"
	"path"
	"testing"
)

func TestFloat16Tensors(t *testing.T) { // Half
	for _, filename := range makeFilenames("tensor_float16") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*HalfStorage)
			if !fsOk {
				t.Fatalf("expected *HalfStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertFloat32SliceEqual(t, fs.Data, []float32{1.2, -3.4, 5.6, -7.8}, 0.002)
		})
	}
}

func TestFloat32Tensors(t *testing.T) { // Float
	for _, filename := range makeFilenames("tensor_float32") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*FloatStorage)
			if !fsOk {
				t.Fatalf("expected *FloatStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertFloat32SliceEqual(t, fs.Data, []float32{1.2, -3.4, 5.6, -7.8}, 0.0)
		})
	}
}

func TestFloat64Tensors(t *testing.T) { // Double
	for _, filename := range makeFilenames("tensor_float64") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*DoubleStorage)
			if !fsOk {
				t.Fatalf("expected *DoubleStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertFloat64SliceEqual(t, fs.Data, []float64{1.2, -3.4, 5.6, -7.8}, 0.0)
		})
	}
}

func TestInt8Tensors(t *testing.T) { // Char
	for _, filename := range makeFilenames("tensor_int8") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*CharStorage)
			if !fsOk {
				t.Fatalf("expected *CharStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertInt8SliceEqual(t, fs.Data, []int8{1, -2, 3, -4})
		})
	}
}

func TestInt16Tensors(t *testing.T) { // Short
	for _, filename := range makeFilenames("tensor_int16") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*ShortStorage)
			if !fsOk {
				t.Fatalf("expected *ShortStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertInt16SliceEqual(t, fs.Data, []int16{1, -2, 3, -4})
		})
	}
}

func TestInt32Tensors(t *testing.T) { // Int
	for _, filename := range makeFilenames("tensor_int32") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*IntStorage)
			if !fsOk {
				t.Fatalf("expected *IntStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertInt32SliceEqual(t, fs.Data, []int32{1, -2, 3, -4})
		})
	}
}

func TestInt64Tensors(t *testing.T) { // Long
	for _, filename := range makeFilenames("tensor_int64") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*LongStorage)
			if !fsOk {
				t.Fatalf("expected *LongStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertInt64SliceEqual(t, fs.Data, []int64{1, -2, 3, -4})
		})
	}
}

func TestUInt8Tensors(t *testing.T) { // Byte
	for _, filename := range makeFilenames("tensor_uint8") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*ByteStorage)
			if !fsOk {
				t.Fatalf("expected *ByteStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertUInt8SliceEqual(t, fs.Data, []uint8{1, 10, 100, 255})
		})
	}
}

func TestBoolTensors(t *testing.T) {
	for _, filename := range makeFilenames("tensor_bool") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*BoolStorage)
			if !fsOk {
				t.Fatalf("expected *ByteStorage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertBoolSliceEqual(t, fs.Data, []bool{true, false, true, false})
		})
	}
}

func TestBFloat16Tensors(t *testing.T) { // Float
	for _, filename := range makeFilenames("tensor_bfloat16") {
		t.Run(filename, func(t *testing.T) {
			tensor := loadTensorFromFile(t, filename)
			assertCommonTensorFields(t, tensor)
			fs, fsOk := tensor.Source.(*BFloat16Storage)
			if !fsOk {
				t.Fatalf("expected *BFloat16Storage, got %#v", tensor.Source)
			}
			assertBaseStorageFields(t, fs.BaseStorage, 4, "cpu")
			assertFloat32SliceEqual(t, fs.Data, []float32{1.2, -3.4, 5.6, -7.8}, 0.013)
		})
	}
}

func loadTensorFromFile(t *testing.T, filename string) *Tensor {
	result, err := Load(path.Join("testdata", filename))
	if err != nil {
		t.Fatal(err)
	}
	tensor, tensorOk := result.(*Tensor)
	if !tensorOk {
		t.Fatalf("expected *Tensor, got %#v", result)
	}
	return tensor
}

func makeFilenames(prefix string) []string {
	filenames := make([]string, 0, 10)
	for _, proto := range [...]int{1, 2, 3, 4, 5} {
		for _, useZip := range [...]bool{false, true} {
			suffix := fmt.Sprintf("_proto%d", proto)
			if useZip {
				suffix += "_zip"
			}
			filenames = append(filenames, prefix+suffix+".pt")
		}
	}
	return filenames
}

func assertIntSliceEqual(t *testing.T, actual, expected []int) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertFloat32SliceEqual(t *testing.T, actual, expected []float32, eps float32) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		expectedVal := expected[index]
		if actualVal < expectedVal-eps || actualVal > expectedVal+eps {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertFloat64SliceEqual(t *testing.T, actual, expected []float64, eps float64) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		expectedVal := expected[index]
		if actualVal < expectedVal-eps || actualVal > expectedVal+eps {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertInt8SliceEqual(t *testing.T, actual, expected []int8) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertInt16SliceEqual(t *testing.T, actual, expected []int16) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertInt32SliceEqual(t *testing.T, actual, expected []int32) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertInt64SliceEqual(t *testing.T, actual, expected []int64) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertUInt8SliceEqual(t *testing.T, actual, expected []uint8) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertBoolSliceEqual(t *testing.T, actual, expected []bool) {
	if len(actual) != len(expected) {
		t.Errorf("expected %v, actual %v", expected, actual)
		return
	}
	for index, actualVal := range actual {
		if actualVal != expected[index] {
			t.Errorf("expected %v, actual %v", expected, actual)
			return
		}
	}
}

func assertCommonTensorFields(t *testing.T, tensor *Tensor) {
	assertIntSliceEqual(t, tensor.Size, []int{4})
	assertIntSliceEqual(t, tensor.Stride, []int{1})
	if tensor.StorageOffset != 0 {
		t.Errorf("expected StorageOffset 0, got %d", tensor.StorageOffset)
	}
	if tensor.RequiresGrad {
		t.Errorf("expected RequiresGrad false, got True")
	}
}

func assertBaseStorageFields(t *testing.T, bs BaseStorage, size int, location string) {
	if bs.Size != size {
		t.Errorf("expected storage Size %d, got %d", size, bs.Size)
	}
	if bs.Location != location {
		t.Errorf("expected storage Location %#v, got %#v", location, bs.Location)
	}
}