-
Notifications
You must be signed in to change notification settings - Fork 0
/
mul_test.go
173 lines (154 loc) · 4.26 KB
/
mul_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
package nunela_test
import (
"testing"
"github.com/bababax11/nunela"
"github.com/vorduin/nune"
)
func TestTensorDotWithOneAxis(t *testing.T) {
nune.EnvConfig.Interactive = true
cases := map[string]struct {
tensor0 nune.Tensor[int]
tensor1 nune.Tensor[int]
axes []int
expected nune.Tensor[int]
}{
"matrix multiplation": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 12, 1).Reshape(4, 3),
[]int{1, 0},
nune.FromBuffer([]int{
42, 48, 54,
114, 136, 158,
}).Reshape(2, 3),
},
"3D tensordot with different axes": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 12, 1).Reshape(2, 2, 3),
[]int{0, 1},
nune.FromBuffer([]int{
12, 16, 20,
36, 40, 44,
15, 21, 27,
51, 57, 63,
18, 26, 34,
66, 74, 82,
21, 31, 41,
81, 91, 101,
}).Reshape(4, 2, 3),
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
val := nunela.TensorDot([]*nune.Tensor[int]{
&tt.tensor0, &tt.tensor1,
}, tt.axes)
if !nunela.Equal(val, &tt.expected) {
t.Error(name, val.Ravel(), tt.expected.Ravel())
}
})
}
}
func TestStrassenDotAx(t *testing.T) {
nune.EnvConfig.Interactive = true
cases := map[string]struct {
tensor0 nune.Tensor[int]
tensor1 nune.Tensor[int]
axes []int
expected nune.Tensor[int]
}{
"matrix multiplation": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 16, 1).Reshape(4, 4),
[]int{1, 0},
nune.FromBuffer([]int{
56, 62, 68, 74,
152, 174, 196, 218,
}).Reshape(2, 4),
},
"3D tensordot with different axes": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 16, 1).Reshape(2, 2, 4),
[]int{0, 1},
nune.FromBuffer([]int{
16, 20, 24, 28,
48, 52, 56, 60,
20, 26, 32, 38,
68, 74, 80, 86,
24, 32, 40, 48,
88, 96, 104, 112,
28, 38, 48, 58,
108, 118, 128, 138,
}).Reshape(4, 2, 4),
},
"matrix multiplation with odd sizes": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 12, 1).Reshape(4, 3),
[]int{1, 0},
nune.FromBuffer([]int{
42, 48, 54,
114, 136, 158,
}).Reshape(2, 3),
},
"3D tensordot with different axes with odd sizes": {
nune.Range[int](0, 8, 1).Reshape(2, 4),
nune.Range[int](0, 12, 1).Reshape(2, 2, 3),
[]int{0, 1},
nune.FromBuffer([]int{
12, 16, 20,
36, 40, 44,
15, 21, 27,
51, 57, 63,
18, 26, 34,
66, 74, 82,
21, 31, 41,
81, 91, 101,
}).Reshape(4, 2, 3),
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
val := nunela.StrassenDot(
&tt.tensor0, &tt.tensor1, tt.axes[0], tt.axes[1])
if !nunela.Equal(val, &tt.expected) {
t.Error(name, val.Ravel(), tt.expected.Ravel())
}
})
}
}
func BenchmarkTensorDot(b *testing.B) {
benchmarkOp(b, func(tensor nune.Tensor[TestsT]) {
nunela.TensorDot([]*nune.Tensor[TestsT]{&tensor, &tensor}, []int{1, 0})
})
}
func BenchmarkStrassen(b *testing.B) {
benchmarkOp(b, func(tensor nune.Tensor[TestsT]) {
nunela.StrassenDot(&tensor, &tensor, 1, 0)
})
}
func FuzzTensorDot(f *testing.F) {
f.Add(2, 1, 3, 1, 4, 1, 5, 1)
f.Fuzz(func(t *testing.T, x0, x1, x2, x3, y0, y1, y2, y3 int) {
tensor0 := nune.FromBuffer([]int{x0, x1, x2, x3}).Reshape(2, 2)
tensor1 := nune.FromBuffer([]int{y0, y1, y2, y3}).Reshape(2, 2)
tensorDot := nunela.TensorDot([]*nune.Tensor[int]{&tensor0, &tensor1}, []int{1, 0})
strassenDot := nunela.StrassenDot(&tensor0, &tensor1, 1, 0)
if !nunela.Equal(tensorDot, strassenDot) {
f.Failed()
}
})
}
func FuzzTensorDotWithDifferentShapes(f *testing.F) {
f.Add(2, 1, 3, 1, 4, 1, 5, 1, 3)
f.Fuzz(func(t *testing.T, start0, step0, shape0X, shape01Y, shape0Z, start1, step1, shape1X, shape1Z int) {
if shape0X <= 0 || shape01Y <= 0 || shape0Z <= 0 || shape1X <= 0 || shape1Z <= 0 || step0 <= 0 || step1 <= 0 {
return
}
tensor0 := nune.Range[int](start0, start0+shape0X*shape01Y*shape0Z*step0, step0).Reshape(shape0X, shape01Y, shape0Z)
tensor1 := nune.Range[int](start1, start1+shape1X*shape01Y*shape1Z*step1, step1).Reshape(shape1X, shape01Y, shape1Z)
tensorDot := nunela.TensorDot([]*nune.Tensor[int]{&tensor0, &tensor1}, []int{1, 1})
strassenDot := nunela.StrassenDot(&tensor0, &tensor1, 1, 1)
if !nunela.Equal(tensorDot, strassenDot) {
f.Failed()
}
})
}