forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestutils_test.go
567 lines (517 loc) · 13.2 KB
/
testutils_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
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
package tensor
import (
"bytes"
"errors"
"math"
"math/cmplx"
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
"unsafe"
"github.com/chewxy/math32"
"github.com/pdevine/tensor/internal/storage"
)
func randomBool() bool {
i := rand.Intn(11)
return i > 5
}
// from : https://stackoverflow.com/a/31832326/3426066
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
const quickchecks = 1000
func newRand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func randomString() string {
n := rand.Intn(10)
b := make([]byte, n)
src := newRand()
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// taken from the Go Stdlib package math
func tolerancef64(a, b, e float64) bool {
d := a - b
if d < 0 {
d = -d
}
// note: b is correct (expected) value, a is actual value.
// make error tolerance a fraction of b, not a.
if b != 0 {
e = e * b
if e < 0 {
e = -e
}
}
return d < e
}
func closeenoughf64(a, b float64) bool { return tolerancef64(a, b, 1e-8) }
func closef64(a, b float64) bool { return tolerancef64(a, b, 1e-14) }
func veryclosef64(a, b float64) bool { return tolerancef64(a, b, 4e-16) }
func soclosef64(a, b, e float64) bool { return tolerancef64(a, b, e) }
func alikef64(a, b float64) bool {
switch {
case math.IsNaN(a) && math.IsNaN(b):
return true
case a == b:
return math.Signbit(a) == math.Signbit(b)
}
return false
}
// taken from math32, which was taken from the Go std lib
func tolerancef32(a, b, e float32) bool {
d := a - b
if d < 0 {
d = -d
}
// note: b is correct (expected) value, a is actual value.
// make error tolerance a fraction of b, not a.
if b != 0 {
e = e * b
if e < 0 {
e = -e
}
}
return d < e
}
func closef32(a, b float32) bool { return tolerancef32(a, b, 1e-5) } // the number gotten from the cfloat standard. Haskell's Linear package uses 1e-6 for floats
func veryclosef32(a, b float32) bool { return tolerancef32(a, b, 1e-6) } // from wiki
func soclosef32(a, b, e float32) bool { return tolerancef32(a, b, e) }
func alikef32(a, b float32) bool {
switch {
case math32.IsNaN(a) && math32.IsNaN(b):
return true
case a == b:
return math32.Signbit(a) == math32.Signbit(b)
}
return false
}
// taken from math/cmplx test
func cTolerance(a, b complex128, e float64) bool {
d := cmplx.Abs(a - b)
if b != 0 {
e = e * cmplx.Abs(b)
if e < 0 {
e = -e
}
}
return d < e
}
func cClose(a, b complex128) bool { return cTolerance(a, b, 1e-14) }
func cSoclose(a, b complex128, e float64) bool { return cTolerance(a, b, e) }
func cVeryclose(a, b complex128) bool { return cTolerance(a, b, 4e-16) }
func cAlike(a, b complex128) bool {
switch {
case cmplx.IsNaN(a) && cmplx.IsNaN(b):
return true
case a == b:
return math.Signbit(real(a)) == math.Signbit(real(b)) && math.Signbit(imag(a)) == math.Signbit(imag(b))
}
return false
}
func allClose(a, b interface{}, approxFn ...interface{}) bool {
switch at := a.(type) {
case []float64:
closeness := closef64
var ok bool
if len(approxFn) > 0 {
if closeness, ok = approxFn[0].(func(a, b float64) bool); !ok {
closeness = closef64
}
}
bt := b.([]float64)
for i, v := range at {
if math.IsNaN(v) {
if !math.IsNaN(bt[i]) {
return false
}
continue
}
if math.IsInf(v, 0) {
if !math.IsInf(bt[i], 0) {
return false
}
continue
}
if !closeness(v, bt[i]) {
return false
}
}
return true
case []float32:
closeness := closef32
var ok bool
if len(approxFn) > 0 {
if closeness, ok = approxFn[0].(func(a, b float32) bool); !ok {
closeness = closef32
}
}
bt := b.([]float32)
for i, v := range at {
if math32.IsNaN(v) {
if !math32.IsNaN(bt[i]) {
return false
}
continue
}
if math32.IsInf(v, 0) {
if !math32.IsInf(bt[i], 0) {
return false
}
continue
}
if !closeness(v, bt[i]) {
return false
}
}
return true
case []complex64:
bt := b.([]complex64)
for i, v := range at {
if cmplx.IsNaN(complex128(v)) {
if !cmplx.IsNaN(complex128(bt[i])) {
return false
}
continue
}
if cmplx.IsInf(complex128(v)) {
if !cmplx.IsInf(complex128(bt[i])) {
return false
}
continue
}
if !cSoclose(complex128(v), complex128(bt[i]), 1e-5) {
return false
}
}
return true
case []complex128:
bt := b.([]complex128)
for i, v := range at {
if cmplx.IsNaN(v) {
if !cmplx.IsNaN(bt[i]) {
return false
}
continue
}
if cmplx.IsInf(v) {
if !cmplx.IsInf(bt[i]) {
return false
}
continue
}
if !cClose(v, bt[i]) {
return false
}
}
return true
default:
return reflect.DeepEqual(a, b)
}
}
func checkErr(t *testing.T, expected bool, err error, name string, id interface{}) (cont bool) {
switch {
case expected:
if err == nil {
t.Errorf("Expected error in test %v (%v)", name, id)
}
return true
case !expected && err != nil:
t.Errorf("Test %v (%v) errored: %+v", name, id, err)
return true
}
return false
}
func sliceApproxf64(a, b []float64, fn func(a, b float64) bool) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if math.IsNaN(v) {
if !alikef64(v, b[i]) {
return false
}
}
if !fn(v, b[i]) {
return false
}
}
return true
}
func RandomFloat64(size int) []float64 {
r := make([]float64, size)
for i := range r {
r[i] = rand.NormFloat64()
}
return r
}
func factorize(a int) []int {
if a <= 0 {
return nil
}
// all numbers are divisible by at least 1
retVal := make([]int, 1)
retVal[0] = 1
fill := func(a int, e int) {
n := len(retVal)
for i, p := 0, a; i < e; i, p = i+1, p*a {
for j := 0; j < n; j++ {
retVal = append(retVal, retVal[j]*p)
}
}
}
// find factors of 2
// rightshift by 1 = division by 2
var e int
for ; a&1 == 0; e++ {
a >>= 1
}
fill(2, e)
// find factors of 3 and up
for next := 3; a > 1; next += 2 {
if next*next > a {
next = a
}
for e = 0; a%next == 0; e++ {
a /= next
}
if e > 0 {
fill(next, e)
}
}
return retVal
}
func shuffleInts(a []int, r *rand.Rand) {
for i := range a {
j := r.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
type TensorGenerator struct {
ShapeConstraint Shape
DtypeConstraint Dtype
}
func (g TensorGenerator) Generate(r *rand.Rand, size int) reflect.Value {
var retVal Tensor
// generate type of tensor
return reflect.ValueOf(retVal)
}
func (t *Dense) Generate(r *rand.Rand, size int) reflect.Value {
// generate type
ri := r.Intn(len(specializedTypes.set))
of := specializedTypes.set[ri]
datatyp := reflect.SliceOf(of.Type)
gendat, _ := quick.Value(datatyp, r)
// generate dims
var scalar bool
var s Shape
dims := r.Intn(5) // dims4 is the max we'll generate even though we can handle much more
l := gendat.Len()
// generate shape based on inputs
switch {
case dims == 0 || l == 0:
scalar = true
gendat, _ = quick.Value(of.Type, r)
case dims == 1:
s = Shape{gendat.Len()}
default:
factors := factorize(l)
s = Shape(BorrowInts(dims))
// fill with 1s so that we can get a non-zero TotalSize
for i := 0; i < len(s); i++ {
s[i] = 1
}
for i := 0; i < dims; i++ {
j := rand.Intn(len(factors))
s[i] = factors[j]
size := s.TotalSize()
if q, r := divmod(l, size); r != 0 {
factors = factorize(r)
} else if size != l {
if i < dims-2 {
factors = factorize(q)
} else if i == dims-2 {
s[i+1] = q
break
}
} else {
break
}
}
shuffleInts(s, r)
}
// generate flags
flag := MemoryFlag(r.Intn(4))
// generate order
order := DataOrder(r.Intn(4))
var v *Dense
if scalar {
v = New(FromScalar(gendat.Interface()))
} else {
v = New(Of(of), WithShape(s...), WithBacking(gendat.Interface()))
}
v.flag = flag
v.AP.o = order
// generate engine
oeint := r.Intn(2)
eint := r.Intn(4)
switch eint {
case 0:
v.e = StdEng{}
if oeint == 0 {
v.oe = StdEng{}
} else {
v.oe = nil
}
case 1:
// check is to prevent panics which Float64Engine will do if asked to allocate memory for non float64s
if of == Float64 {
v.e = Float64Engine{}
if oeint == 0 {
v.oe = Float64Engine{}
} else {
v.oe = nil
}
} else {
v.e = StdEng{}
if oeint == 0 {
v.oe = StdEng{}
} else {
v.oe = nil
}
}
case 2:
// check is to prevent panics which Float64Engine will do if asked to allocate memory for non float64s
if of == Float32 {
v.e = Float32Engine{}
if oeint == 0 {
v.oe = Float32Engine{}
} else {
v.oe = nil
}
} else {
v.e = StdEng{}
if oeint == 0 {
v.oe = StdEng{}
} else {
v.oe = nil
}
}
case 3:
v.e = dummyEngine(true)
v.oe = nil
}
return reflect.ValueOf(v)
}
// fakemem is a byteslice, while making it a Memory
type fakemem []byte
func (m fakemem) Uintptr() uintptr { return uintptr(unsafe.Pointer(&m[0])) }
func (m fakemem) MemSize() uintptr { return uintptr(len(m)) }
func (m fakemem) Pointer() unsafe.Pointer { return unsafe.Pointer(&m[0]) }
// dummyEngine implements Engine. The bool indicates whether the data is native-accessible
type dummyEngine bool
func (e dummyEngine) AllocAccessible() bool { return bool(e) }
func (e dummyEngine) Alloc(size int64) (Memory, error) {
ps := make(fakemem, int(size))
return ps, nil
}
func (e dummyEngine) Free(mem Memory, size int64) error { return nil }
func (e dummyEngine) Memset(mem Memory, val interface{}) error { return nil }
func (e dummyEngine) Memclr(mem Memory) {}
func (e dummyEngine) Memcpy(dst, src Memory) error {
if e {
var a, b storage.Header
a.Raw = storage.FromMemory(src.Uintptr(), src.MemSize())
b.Raw = storage.FromMemory(dst.Uintptr(), dst.MemSize())
copy(b.Raw, a.Raw)
return nil
}
return errors.New("Unable to copy ")
}
func (e dummyEngine) Accessible(mem Memory) (Memory, error) { return mem, nil }
func (e dummyEngine) WorksWith(order DataOrder) bool { return true }
// dummyEngine2 is used for testing additional methods that may not be provided in the stdeng
type dummyEngine2 struct {
e StdEng
}
func (e dummyEngine2) AllocAccessible() bool { return e.e.AllocAccessible() }
func (e dummyEngine2) Alloc(size int64) (Memory, error) { return e.e.Alloc(size) }
func (e dummyEngine2) Free(mem Memory, size int64) error { return e.e.Free(mem, size) }
func (e dummyEngine2) Memset(mem Memory, val interface{}) error { return e.e.Memset(mem, val) }
func (e dummyEngine2) Memclr(mem Memory) { e.e.Memclr(mem) }
func (e dummyEngine2) Memcpy(dst, src Memory) error { return e.e.Memcpy(dst, src) }
func (e dummyEngine2) Accessible(mem Memory) (Memory, error) { return e.e.Accessible(mem) }
func (e dummyEngine2) WorksWith(order DataOrder) bool { return e.e.WorksWith(order) }
func (e dummyEngine2) Argmax(t Tensor, axis int) (Tensor, error) { return e.e.Argmax(t, axis) }
func (e dummyEngine2) Argmin(t Tensor, axis int) (Tensor, error) { return e.e.Argmin(t, axis) }
func willerr(a *Dense, tc, eqtc *typeclass) (retVal, willFailEq bool) {
if err := typeclassCheck(a.Dtype(), eqtc); err == nil {
willFailEq = true
}
if err := typeclassCheck(a.Dtype(), tc); err != nil {
return true, willFailEq
}
retVal = retVal || !a.IsNativelyAccessible()
return
}
func qcErrCheck(t *testing.T, name string, a Dtyper, b interface{}, we bool, err error) (e error, retEarly bool) {
switch {
case !we && err != nil:
t.Errorf("Tests for %v (%v) was unable to proceed: %v", name, a.Dtype(), err)
return err, true
case we && err == nil:
if b == nil {
t.Errorf("Expected error when performing %v on %T of %v ", name, a, a.Dtype())
return errors.New("Error"), true
}
if bd, ok := b.(Dtyper); ok {
t.Errorf("Expected error when performing %v on %T of %v and %T of %v", name, a, a.Dtype(), b, bd.Dtype())
} else {
t.Errorf("Expected error when performing %v on %T of %v and %v of %T", name, a, a.Dtype(), b, b)
}
return errors.New("Error"), true
case we && err != nil:
return nil, true
}
return nil, false
}
func qcIsFloat(dt Dtype) bool {
if err := typeclassCheck(dt, floatcmplxTypes); err == nil {
return true
}
return false
}
func qcEqCheck(t *testing.T, dt Dtype, willFailEq bool, correct, got interface{}) bool {
isFloatTypes := qcIsFloat(dt)
if !willFailEq && (isFloatTypes && !allClose(correct, got) || (!isFloatTypes && !reflect.DeepEqual(correct, got))) {
t.Errorf("q.Dtype: %v", dt)
t.Errorf("correct\n%v", correct)
t.Errorf("got\n%v", got)
return false
}
return true
}
// DummyState is a dummy fmt.State, used to debug things
type DummyState struct {
*bytes.Buffer
}
func (d *DummyState) Width() (int, bool) { return 0, false }
func (d *DummyState) Precision() (int, bool) { return 0, false }
func (d *DummyState) Flag(c int) bool { return false }