-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
224 lines (190 loc) · 5.72 KB
/
math.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
package gosor
import (
"fmt"
"math"
)
// Add allocates memory for a result tensor and does element wise addition
// into the result tensor using t1 and t2.
func Add(t1, t2 *Tensor) (*Tensor, error) {
return AddInto(nil, t1, t2)
}
// AddInto does element wise addition into the result tensor. If the result is
// nil, a new tensor is allocated.
func AddInto(result, t1, t2 *Tensor) (*Tensor, error) {
result, err := elementWiseOperationInto(result, t1, t2, func(f1, f2 float64) float64 { return f1 + f2 })
if err != nil {
return nil, err
}
addAddGradientTrackerToRes(result, t1, t2)
return result, nil
}
// Sub allocates memory for a result tensor and does element wise subtraction
// into the result tensor using t1 and t2.
func Sub(t1, t2 *Tensor) (*Tensor, error) {
return SubInto(nil, t1, t2)
}
// SubInto does element wise subtraction into the result tensor. If the result is
// nil, a new tensor is allocated.
func SubInto(result, t1, t2 *Tensor) (*Tensor, error) {
result, err := elementWiseOperationInto(result, t1, t2, func(f1, f2 float64) float64 { return f1 - f2 })
if err != nil {
return nil, err
}
addSubGradientTrackerToRes(result, t1, t2)
return result, nil
}
// Mul allocates memory for a result tensor and does element wise multiplication
// into the result tensor using t1 and t2.
func Mul(t1, t2 *Tensor) (*Tensor, error) {
return MulInto(nil, t1, t2)
}
// MulInto does element wise multiplication into the result tensor. If the
// result is nil, a new tensor is allocated.
func MulInto(result, t1, t2 *Tensor) (*Tensor, error) {
result, err := elementWiseOperationInto(result, t1, t2, func(f1, f2 float64) float64 { return f1 * f2 })
if err != nil {
return nil, err
}
addMulGradientTrackerToRes(result, t1, t2)
return result, nil
}
// Div allocates memory for a result tensor and does element wise division
// into the result tensor using t1 and t2.
func Div(t1, t2 *Tensor) (*Tensor, error) {
return DivInto(nil, t1, t2)
}
// DivInto does element wise division into the result tensor. If the
// result is nil, a new tensor is allocated.
func DivInto(result, t1, t2 *Tensor) (*Tensor, error) {
result, err := elementWiseOperationInto(result, t1, t2, func(f1, f2 float64) float64 { return f1 / f2 })
if err != nil {
return nil, err
}
addAddGradientTrackerToRes(result, t1, t2)
return result, nil
}
// Div allocates memory for a result tensor and does element wise division
// into the result tensor using t1 and t2.
func Pow(t1, t2 *Tensor) (*Tensor, error) {
return PowInto(nil, t1, t2)
}
// DivInto does element wise division into the result tensor. If the
// result is nil, a new tensor is allocated.
func PowInto(result, t1, t2 *Tensor) (*Tensor, error) {
result, err := elementWiseOperationInto(result, t1, t2, func(f1, f2 float64) float64 { return math.Pow(f1, f2) })
if err != nil {
return nil, err
}
addPowGradientTrackerToRes(result, t1, t2)
return result, nil
}
func Square(t *Tensor) (*Tensor, error) {
return SquareInto(nil, t)
}
func SquareInto(result, t1 *Tensor) (*Tensor, error) {
two, _ := New(WithValues(2))
return PowInto(result, t1, two)
}
func elementWiseOperationInto(
result,
t1,
t2 *Tensor,
operation func(float64, float64) float64,
) (t *Tensor, err error) {
if t1 == nil || t2 == nil {
return nil, fmt.Errorf("%w: nil tensor", ErrInvalidTensor)
}
if len(t1.sizes) == 0 || len(t2.sizes) == 0 {
return nil, fmt.Errorf("%w: no size", ErrInvalidTensor)
}
t1, t2, err = Broadcast(t1, t2)
if err != nil {
return nil, err
}
if result == nil {
result, err = New(WithSize(t1.sizes...), withIsNotLeaf())
if err != nil {
return nil, err
}
} else {
if len(t1.sizes) != len(result.sizes) {
return nil, fmt.Errorf("wrong result size, want: %v, got: %v", t1.sizes, result.sizes)
}
for i := 0; i < len(t1.sizes); i++ {
if t1.sizes[i] != result.sizes[i] {
return nil, fmt.Errorf("wrong result size, want: %v, got: %v", t1.sizes, result.sizes)
}
}
}
length := 1
for i := 0; i < len(t1.sizes); i++ {
length *= t1.sizes[i]
}
for i := 0; i < length; i++ {
result.storage[result.getStorageIndex(i)] = operation(
t1.storage[t1.getStorageIndex(i)],
t2.storage[t2.getStorageIndex(i)],
)
}
return result, nil
}
// Broadcast broadcast two tensors to be compatible for element wise
// operations. Broadcasting follows two rules:
// - If the tensors have different ranks, the smaller tensor is padded with
// ones on its left until both tensors have the same rank.
// - If the corresponding dimensions of the two tensors have the same size,
// or one of them is one, these tensors are compatible.
func Broadcast(a, b *Tensor) (*Tensor, *Tensor, error) {
a = a.ShallowCopy()
b = b.ShallowCopy()
a.sizes, b.sizes = makeSameSize(a.sizes, b.sizes, 1)
a.strides, b.strides = makeSameSize(a.strides, b.strides, 0)
for i := 0; i < len(a.sizes); i++ {
if a.sizes[i] == b.sizes[i] {
continue
}
if a.sizes[i] == 1 {
a.strides[i] = 0
a.sizes[i] = b.sizes[i]
continue
}
if b.sizes[i] == 1 {
b.strides[i] = 0
b.sizes[i] = a.sizes[i]
continue
}
return nil, nil, ErrFieldsMismatch
}
return a, b, nil
}
func makeSameSize(a, b []int, filler int) ([]int, []int) {
if len(a) > len(b) {
b, a = makeSameSize(b, a, filler)
return a, b
}
if len(a) < len(b) {
numToAdd := len(b) - len(a)
numLeftToAdd := numToAdd
newA := make([]int, len(b))
for i := 0; i < len(newA); i++ {
if numLeftToAdd > 0 {
newA[i] = filler
numLeftToAdd--
continue
}
newA[i] = a[i-numToAdd]
}
return newA, b
}
return a, b
}
func (t *Tensor) getStorageIndex(i int) int {
index := t.offset
v := i
for j := len(t.sizes) - 1; j >= 0; j-- {
dimensionIndex := v % t.sizes[j]
v /= t.sizes[j]
index += dimensionIndex * t.strides[j]
}
return index
}