-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdefaultengine_misc.go
81 lines (74 loc) · 2.25 KB
/
defaultengine_misc.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
package tensor
import (
"github.com/pkg/errors"
"gorgonia.org/tensor/internal/storage"
)
func (e StdEng) Clamp(a Tensor, min, max interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
if err = unaryCheck(a, nonComplexNumberTypes); err != nil {
return nil, errors.Wrap(err, "Clamp failed")
}
var reuse DenseTensor
var safe, toReuse, incr bool
if reuse, safe, toReuse, incr, _, err = handleFuncOpts(a.Shape(), a.Dtype(), a.DataOrder(), false, opts...); err != nil {
return nil, errors.Wrap(err, "Unable to handle funcOpts")
}
typ := a.Dtype().Type
var ait, rit Iterator
var dataA, dataReuse *storage.Header
var useIter bool
if dataA, dataReuse, ait, rit, useIter, err = prepDataUnary(a, reuse); err != nil {
return nil, errors.Wrapf(err, opFail, "StdEng.Neg")
}
if useIter {
switch {
case incr:
cloned := a.Clone().(Tensor)
if err = e.E.ClampIter(typ, cloned.hdr(), ait, min, max); err != nil {
return nil, errors.Wrapf(err, "Unable to perform Clamp")
}
ait.Reset()
err = e.E.AddIter(typ, dataReuse, cloned.hdr(), rit, ait)
retVal = reuse
case toReuse:
storage.CopyIter(typ, dataReuse, dataA, rit, ait)
rit.Reset()
err = e.E.ClampIter(typ, dataReuse, rit, min, max)
retVal = reuse
case !safe:
err = e.E.ClampIter(typ, dataA, ait, min, max)
retVal = a
default:
cloned := a.Clone().(Tensor)
err = e.E.ClampIter(typ, cloned.hdr(), ait, min, max)
retVal = cloned
}
return
}
switch {
case incr:
cloned := a.Clone().(Tensor)
if err = e.E.Clamp(typ, cloned.hdr(), min, max); err != nil {
return nil, errors.Wrapf(err, "Unable to perform Clamp")
}
err = e.E.Add(typ, dataReuse, cloned.hdr())
retVal = reuse
case toReuse:
storage.Copy(typ, dataReuse, dataA)
err = e.E.Clamp(typ, dataReuse, min, max)
retVal = reuse
case !safe:
err = e.E.Clamp(typ, dataA, min, max)
retVal = a
default:
cloned := a.Clone().(Tensor)
err = e.E.Clamp(typ, cloned.hdr(), min, max)
retVal = cloned
}
return
}
func (e StdEng) FMA(a, x, y Tensor) (Tensor, error) {
return e.Mul(a, x, WithIncr(y))
}
func (e StdEng) FMAScalar(a Tensor, x interface{}, y Tensor) (Tensor, error) {
return e.MulScalar(a, x, true, WithIncr(y))
}