forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefaultengine_minmax.go
349 lines (324 loc) · 9.3 KB
/
defaultengine_minmax.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
// Code generated by genlib2. DO NOT EDIT.
package tensor
import (
"github.com/pkg/errors"
"github.com/pdevine/tensor/internal/storage"
)
var (
_ MinBetweener = StdEng{}
_ MaxBetweener = StdEng{}
)
func (e StdEng) MinBetween(a Tensor, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if err = binaryCheck(a, b, ordTypes); err != nil {
return nil, errors.Wrapf(err, "MinBetween failed")
}
var reuse DenseTensor
var safe bool
if reuse, safe, _, _, _, err = handleFuncOpts(a.Shape(), a.Dtype(), a.DataOrder(), true, opts...); err != nil {
return nil, errors.Wrap(err, "Unable to handle funcOpts")
}
typ := a.Dtype().Type
var dataA, dataB, dataReuse *storage.Header
var ait, bit, iit Iterator
var useIter, swap bool
if dataA, dataB, dataReuse, ait, bit, iit, useIter, swap, err = prepDataVV(a, b, reuse); err != nil {
return nil, errors.Wrapf(err, "StdEng.MinBetween")
}
// check to see if anything needs to be created
if reuse == nil {
if swap {
reuse = NewDense(b.Dtype(), b.Shape().Clone(), WithEngine(e))
} else {
reuse = NewDense(a.Dtype(), a.Shape().Clone(), WithEngine(e))
}
dataReuse = reuse.hdr()
if useIter {
iit = IteratorFromDense(reuse)
}
}
if useIter {
switch {
case !safe && reuse == nil:
err = e.E.MinBetweenIter(typ, dataA, dataB, ait, bit)
retVal = a
case safe && reuse != nil:
storage.CopyIter(typ, dataReuse, dataA, iit, ait)
ait.Reset()
iit.Reset()
err = e.E.MinBetweenIter(typ, dataReuse, dataB, iit, bit)
retVal = reuse
default: // safe && bool
panic("Unreachable")
}
return
}
// standard
switch {
case !safe && reuse == nil:
err = e.E.MinBetween(typ, dataA, dataB)
retVal = a
case safe && reuse != nil:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MinBetween(typ, dataReuse, dataB)
retVal = reuse
default:
panic("Unreachable")
}
return
}
func (e StdEng) MaxBetween(a Tensor, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if err = binaryCheck(a, b, ordTypes); err != nil {
return nil, errors.Wrapf(err, "MaxBetween failed")
}
var reuse DenseTensor
var safe bool
if reuse, safe, _, _, _, err = handleFuncOpts(a.Shape(), a.Dtype(), a.DataOrder(), true, opts...); err != nil {
return nil, errors.Wrap(err, "Unable to handle funcOpts")
}
typ := a.Dtype().Type
var dataA, dataB, dataReuse *storage.Header
var ait, bit, iit Iterator
var useIter, swap bool
if dataA, dataB, dataReuse, ait, bit, iit, useIter, swap, err = prepDataVV(a, b, reuse); err != nil {
return nil, errors.Wrapf(err, "StdEng.MaxBetween")
}
// check to see if anything needs to be created
if reuse == nil {
if swap {
reuse = NewDense(b.Dtype(), b.Shape().Clone(), WithEngine(e))
} else {
reuse = NewDense(a.Dtype(), a.Shape().Clone(), WithEngine(e))
}
dataReuse = reuse.hdr()
if useIter {
iit = IteratorFromDense(reuse)
}
}
if useIter {
switch {
case !safe && reuse == nil:
err = e.E.MaxBetweenIter(typ, dataA, dataB, ait, bit)
retVal = a
case safe && reuse != nil:
storage.CopyIter(typ, dataReuse, dataA, iit, ait)
ait.Reset()
iit.Reset()
err = e.E.MaxBetweenIter(typ, dataReuse, dataB, iit, bit)
retVal = reuse
default: // safe && bool
panic("Unreachable")
}
return
}
// standard
switch {
case !safe && reuse == nil:
err = e.E.MaxBetween(typ, dataA, dataB)
retVal = a
case safe && reuse != nil:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MaxBetween(typ, dataReuse, dataB)
retVal = reuse
default:
panic("Unreachable")
}
return
}
func (e StdEng) MinBetweenScalar(t Tensor, s interface{}, leftTensor bool, opts ...FuncOpt) (retVal Tensor, err error) {
if err = unaryCheck(t, ordTypes); err != nil {
return nil, errors.Wrapf(err, "MinBetween failed")
}
if err = scalarDtypeCheck(t, s); err != nil {
return nil, errors.Wrap(err, "MinBetween failed")
}
var reuse DenseTensor
var safe bool
if reuse, safe, _, _, _, err = handleFuncOpts(t.Shape(), t.Dtype(), t.DataOrder(), true, opts...); err != nil {
return nil, errors.Wrap(err, "Unable to handle funcOpts")
}
a := t
typ := t.Dtype().Type
var ait, bit, iit Iterator
var dataA, dataB, dataReuse, scalarHeader *storage.Header
var useIter, newAlloc bool
if leftTensor {
if dataA, dataB, dataReuse, ait, iit, useIter, newAlloc, err = prepDataVS(t, s, reuse); err != nil {
return nil, errors.Wrapf(err, opFail, "StdEng.MinBetween")
}
scalarHeader = dataB
} else {
if dataA, dataB, dataReuse, bit, iit, useIter, newAlloc, err = prepDataSV(s, t, reuse); err != nil {
return nil, errors.Wrapf(err, opFail, "StdEng.MinBetween")
}
scalarHeader = dataA
}
// check to see if anything needs to be created
if reuse == nil {
reuse = NewDense(a.Dtype(), a.Shape().Clone(), WithEngine(e))
dataReuse = reuse.hdr()
if useIter {
iit = IteratorFromDense(reuse)
}
}
if useIter {
switch {
case !safe && reuse == nil:
err = e.E.MinBetweenIter(typ, dataA, dataB, ait, bit)
retVal = a
case safe && reuse != nil && !leftTensor:
storage.CopyIter(typ, dataReuse, dataB, iit, bit)
bit.Reset()
iit.Reset()
err = e.E.MinBetweenIter(typ, dataA, dataReuse, ait, bit)
retVal = reuse
case safe && reuse != nil && leftTensor:
storage.CopyIter(typ, dataReuse, dataA, iit, ait)
ait.Reset()
iit.Reset()
err = e.E.MinBetweenIter(typ, dataReuse, dataB, iit, bit)
retVal = reuse
default: // safe && bool
panic("Unreachable")
}
if newAlloc {
freeScalar(scalarHeader.Raw)
}
returnHeader(scalarHeader)
return
}
// handle special case where A and B have both len 1
if len(dataA.Raw) == int(typ.Size()) && len(dataB.Raw) == int(typ.Size()) {
switch {
case safe && reuse != nil && leftTensor:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MinBetween(typ, dataReuse, dataB)
retVal = reuse
return
case safe && reuse != nil && !leftTensor:
storage.Copy(typ, dataReuse, dataB)
err = e.E.MinBetween(typ, dataReuse, dataA)
retVal = reuse
return
}
}
// standard
switch {
case !safe && reuse == nil:
err = e.E.MinBetween(typ, dataA, dataB)
retVal = a
case safe && reuse != nil && leftTensor:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MinBetween(typ, dataReuse, dataB)
retVal = reuse
case safe && reuse != nil && !leftTensor:
storage.Copy(typ, dataReuse, dataB)
err = e.E.MinBetween(typ, dataA, dataReuse)
retVal = reuse
default:
panic("Unreachable")
}
if newAlloc {
freeScalar(scalarHeader.Raw)
}
returnHeader(scalarHeader)
return
}
func (e StdEng) MaxBetweenScalar(t Tensor, s interface{}, leftTensor bool, opts ...FuncOpt) (retVal Tensor, err error) {
if err = unaryCheck(t, ordTypes); err != nil {
return nil, errors.Wrapf(err, "MaxBetween failed")
}
if err = scalarDtypeCheck(t, s); err != nil {
return nil, errors.Wrap(err, "MaxBetween failed")
}
var reuse DenseTensor
var safe bool
if reuse, safe, _, _, _, err = handleFuncOpts(t.Shape(), t.Dtype(), t.DataOrder(), true, opts...); err != nil {
return nil, errors.Wrap(err, "Unable to handle funcOpts")
}
a := t
typ := t.Dtype().Type
var ait, bit, iit Iterator
var dataA, dataB, dataReuse, scalarHeader *storage.Header
var useIter, newAlloc bool
if leftTensor {
if dataA, dataB, dataReuse, ait, iit, useIter, newAlloc, err = prepDataVS(t, s, reuse); err != nil {
return nil, errors.Wrapf(err, opFail, "StdEng.MaxBetween")
}
scalarHeader = dataB
} else {
if dataA, dataB, dataReuse, bit, iit, useIter, newAlloc, err = prepDataSV(s, t, reuse); err != nil {
return nil, errors.Wrapf(err, opFail, "StdEng.MaxBetween")
}
scalarHeader = dataA
}
// check to see if anything needs to be created
if reuse == nil {
reuse = NewDense(a.Dtype(), a.Shape().Clone(), WithEngine(e))
dataReuse = reuse.hdr()
if useIter {
iit = IteratorFromDense(reuse)
}
}
if useIter {
switch {
case !safe && reuse == nil:
err = e.E.MaxBetweenIter(typ, dataA, dataB, ait, bit)
retVal = a
case safe && reuse != nil && !leftTensor:
storage.CopyIter(typ, dataReuse, dataB, iit, bit)
bit.Reset()
iit.Reset()
err = e.E.MaxBetweenIter(typ, dataA, dataReuse, ait, bit)
retVal = reuse
case safe && reuse != nil && leftTensor:
storage.CopyIter(typ, dataReuse, dataA, iit, ait)
ait.Reset()
iit.Reset()
err = e.E.MaxBetweenIter(typ, dataReuse, dataB, iit, bit)
retVal = reuse
default: // safe && bool
panic("Unreachable")
}
if newAlloc {
freeScalar(scalarHeader.Raw)
}
returnHeader(scalarHeader)
return
}
// handle special case where A and B have both len 1
if len(dataA.Raw) == int(typ.Size()) && len(dataB.Raw) == int(typ.Size()) {
switch {
case safe && reuse != nil && leftTensor:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MaxBetween(typ, dataReuse, dataB)
retVal = reuse
return
case safe && reuse != nil && !leftTensor:
storage.Copy(typ, dataReuse, dataB)
err = e.E.MaxBetween(typ, dataReuse, dataA)
retVal = reuse
return
}
}
// standard
switch {
case !safe && reuse == nil:
err = e.E.MaxBetween(typ, dataA, dataB)
retVal = a
case safe && reuse != nil && leftTensor:
storage.Copy(typ, dataReuse, dataA)
err = e.E.MaxBetween(typ, dataReuse, dataB)
retVal = reuse
case safe && reuse != nil && !leftTensor:
storage.Copy(typ, dataReuse, dataB)
err = e.E.MaxBetween(typ, dataA, dataReuse)
retVal = reuse
default:
panic("Unreachable")
}
if newAlloc {
freeScalar(scalarHeader.Raw)
}
returnHeader(scalarHeader)
return
}