-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
defaultengine_prep.go
226 lines (197 loc) · 5.36 KB
/
defaultengine_prep.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
package tensor
import (
"reflect"
"github.com/pkg/errors"
"gorgonia.org/tensor/internal/storage"
// "log"
)
func handleFuncOpts(expShape Shape, expType Dtype, o DataOrder, strict bool, opts ...FuncOpt) (reuse DenseTensor, safe, toReuse, incr, same bool, err error) {
fo := ParseFuncOpts(opts...)
reuseT, incr := fo.IncrReuse()
safe = fo.Safe()
same = fo.Same()
toReuse = reuseT != nil
if toReuse {
if reuse, err = getDenseTensor(reuseT); err != nil {
returnOpOpt(fo)
err = errors.Wrapf(err, "Cannot reuse a Tensor that isn't a DenseTensor. Got %T instead", reuseT)
return
}
if reuse != nil && !reuse.IsNativelyAccessible() {
returnOpOpt(fo)
err = errors.Errorf(inaccessibleData, reuse)
return
}
if (strict || same) && reuse.Dtype() != expType {
returnOpOpt(fo)
err = errors.Errorf(typeMismatch, expType, reuse.Dtype())
err = errors.Wrapf(err, "Cannot use reuse")
return
}
if reuse.len() != expShape.TotalSize() && !expShape.IsScalar() {
returnOpOpt(fo)
err = errors.Errorf(shapeMismatch, reuse.Shape(), expShape)
err = errors.Wrapf(err, "Cannot use reuse: shape mismatch - reuse.len() %v, expShape.TotalSize() %v", reuse.len(), expShape.TotalSize())
return
}
if !reuse.Shape().Eq(expShape) {
cloned := expShape.Clone()
if err = reuse.Reshape(cloned...); err != nil {
return
}
ReturnInts([]int(cloned))
}
if !incr && reuse != nil {
reuse.setDataOrder(o)
// err = reuse.reshape(expShape...)
}
}
returnOpOpt(fo)
return
}
func binaryCheck(a, b Tensor, tc *typeclass) (err error) {
// check if the tensors are accessible
if !a.IsNativelyAccessible() {
return errors.Errorf(inaccessibleData, a)
}
if !b.IsNativelyAccessible() {
return errors.Errorf(inaccessibleData, b)
}
at := a.Dtype()
bt := b.Dtype()
if tc != nil {
if err = typeclassCheck(at, tc); err != nil {
return errors.Wrapf(err, typeclassMismatch, "a")
}
if err = typeclassCheck(bt, tc); err != nil {
return errors.Wrapf(err, typeclassMismatch, "b")
}
}
if at.Kind() != bt.Kind() {
return errors.Errorf(typeMismatch, at, bt)
}
if !a.Shape().Eq(b.Shape()) {
return errors.Errorf(shapeMismatch, b.Shape(), a.Shape())
}
return nil
}
func unaryCheck(a Tensor, tc *typeclass) error {
if !a.IsNativelyAccessible() {
return errors.Errorf(inaccessibleData, a)
}
at := a.Dtype()
if tc != nil {
if err := typeclassCheck(at, tc); err != nil {
return errors.Wrapf(err, typeclassMismatch, "a")
}
}
return nil
}
// scalarDtypeCheck checks that a scalar value has the same dtype as the dtype of a given tensor.
func scalarDtypeCheck(a Tensor, b interface{}) error {
var dt Dtype
switch bt := b.(type) {
case Dtyper:
dt = bt.Dtype()
default:
t := reflect.TypeOf(b)
dt = Dtype{t}
}
if a.Dtype() != dt {
return errors.Errorf("Expected scalar to have the same Dtype as the tensor (%v). Got %T instead ", a.Dtype(), b)
}
return nil
}
// prepDataVV prepares the data given the input and reuse tensors. It also retruns several indicators
//
// useIter indicates that the iterator methods should be used.
// swap indicates that the operands are swapped.
func prepDataVV(a, b Tensor, reuse Tensor) (dataA, dataB, dataReuse *storage.Header, ait, bit, iit Iterator, useIter, swap bool, err error) {
// get data
dataA = a.hdr()
dataB = b.hdr()
if reuse != nil {
dataReuse = reuse.hdr()
}
// iter
useIter = a.RequiresIterator() ||
b.RequiresIterator() ||
(reuse != nil && reuse.RequiresIterator()) ||
!a.DataOrder().HasSameOrder(b.DataOrder()) ||
(reuse != nil && (!a.DataOrder().HasSameOrder(reuse.DataOrder()) || !b.DataOrder().HasSameOrder(reuse.DataOrder())))
if useIter {
ait = a.Iterator()
bit = b.Iterator()
if reuse != nil {
iit = reuse.Iterator()
}
}
// swap
if _, ok := a.(*CS); ok {
if _, ok := b.(DenseTensor); ok {
swap = true
dataA, dataB = dataB, dataA
ait, bit = bit, ait
}
}
return
}
func prepDataVS(a Tensor, b interface{}, reuse Tensor) (dataA, dataB, dataReuse *storage.Header, ait, iit Iterator, useIter bool, newAlloc bool, err error) {
// get data
dataA = a.hdr()
dataB, newAlloc = scalarToHeader(b)
if reuse != nil {
dataReuse = reuse.hdr()
}
if a.IsScalar() {
return
}
useIter = a.RequiresIterator() ||
(reuse != nil && reuse.RequiresIterator()) ||
(reuse != nil && !reuse.DataOrder().HasSameOrder(a.DataOrder()))
if useIter {
ait = a.Iterator()
if reuse != nil {
iit = reuse.Iterator()
}
}
return
}
func prepDataSV(a interface{}, b Tensor, reuse Tensor) (dataA, dataB, dataReuse *storage.Header, bit, iit Iterator, useIter bool, newAlloc bool, err error) {
// get data
dataA, newAlloc = scalarToHeader(a)
dataB = b.hdr()
if reuse != nil {
dataReuse = reuse.hdr()
}
// get iterator
if b.IsScalar() {
return
}
useIter = b.RequiresIterator() ||
(reuse != nil && reuse.RequiresIterator()) ||
(reuse != nil && !reuse.DataOrder().HasSameOrder(b.DataOrder()))
if useIter {
bit = b.Iterator()
if reuse != nil {
iit = reuse.Iterator()
}
}
return
}
func prepDataUnary(a Tensor, reuse Tensor) (dataA, dataReuse *storage.Header, ait, rit Iterator, useIter bool, err error) {
// get data
dataA = a.hdr()
if reuse != nil {
dataReuse = reuse.hdr()
}
// get iterator
if a.RequiresIterator() || (reuse != nil && reuse.RequiresIterator()) {
ait = a.Iterator()
if reuse != nil {
rit = reuse.Iterator()
}
useIter = true
}
return
}