-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
defaultengine_argmethods.go
177 lines (148 loc) · 4.13 KB
/
defaultengine_argmethods.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
package tensor
import "github.com/pkg/errors"
func (e StdEng) Argmax(t Tensor, axis int) (retVal Tensor, err error) {
switch tt := t.(type) {
case DenseTensor:
return e.argmaxDenseTensor(tt, axis)
default:
return nil, errors.Errorf(typeNYI, "StdEng.Argmax", t)
}
}
func (e StdEng) argmaxDenseTensor(t DenseTensor, axis int) (retVal *Dense, err error) {
if err = unaryCheck(t, ordTypes); err != nil {
return nil, errors.Wrapf(err, opFail, "Argmax")
}
if axis >= len(t.Shape()) {
return nil, errors.Errorf(dimMismatch, len(t.Shape()), axis)
}
dataA := t.hdr()
typ := t.rtype()
// SPECIAL CASE: FLAT ARGMAX
if axis == AllAxes {
var index int
if mt, ok := t.(MaskedTensor); ok && mt.IsMasked() {
if index = e.E.ArgmaxFlatMasked(typ, dataA, mt.Mask()); index == -1 {
return nil, errors.Errorf("t is not supported - %T of %v", t, t.Dtype())
}
} else {
if index = e.E.ArgmaxFlat(typ, dataA); index == -1 {
return nil, errors.Errorf("t is not supported - %T of %v", t, t.Dtype())
}
}
return New(FromScalar(index)), nil
}
// ARGMAX ALONG AXIS
var indices []int
axes := make([]int, len(t.Shape()))
for i := range t.Shape() {
switch {
case i < axis:
axes[i] = i
case i == axis:
axes[len(axes)-1] = i
case i > axis:
axes[i-1] = i
}
}
// be a good citizen - borrow and return, since we're only using this AP to figure out the moves
newAP, _, err := t.Info().T(axes...)
if _, ok := err.(NoOpError); !ok && err != nil {
return
} else if ok {
t.Info().CloneTo(&newAP)
}
it := IteratorFromDense(t)
iteratorLoadAP(it, &newAP)
lastSize := it.Shape()[len(it.Shape())-1]
newShape := it.Shape().Clone()
newShape = newShape[:len(newShape)-1]
// cleanup
defer func() {
newAP.zero()
ReturnInts(newShape)
}()
if mt, ok := t.(MaskedTensor); ok && mt.IsMasked() {
mask := mt.Mask()
if indices, err = e.E.ArgmaxIterMasked(typ, dataA, mask, it, lastSize); err != nil {
return
}
} else {
if indices, err = e.E.ArgmaxIter(typ, dataA, it, lastSize); err != nil {
return
}
}
return New(WithShape(newShape...), WithBacking(indices)), nil
}
func (e StdEng) Argmin(t Tensor, axis int) (retVal Tensor, err error) {
switch tt := t.(type) {
case DenseTensor:
return e.argminDenseTensor(tt, axis)
default:
return nil, errors.Errorf(typeNYI, "StdEng.Argmin", t)
}
}
func (e StdEng) argminDenseTensor(t DenseTensor, axis int) (retVal *Dense, err error) {
if err = unaryCheck(t, ordTypes); err != nil {
return nil, errors.Wrapf(err, opFail, "Argmin")
}
if axis >= len(t.Shape()) {
return nil, errors.Errorf(dimMismatch, len(t.Shape()), axis)
}
dataA := t.hdr()
typ := t.rtype()
// SPECIAL CASE: FLAT ARGMAX
if axis == AllAxes {
var index int
if mt, ok := t.(MaskedTensor); ok && mt.IsMasked() {
if index = e.E.ArgminFlatMasked(typ, dataA, mt.Mask()); index == -1 {
return nil, errors.Errorf("t is not supported - %T of %v", t, t.Dtype())
}
} else {
if index = e.E.ArgminFlat(typ, dataA); index == -1 {
return nil, errors.Errorf("t is not supported - %T of %v", t, t.Dtype())
}
}
return New(FromScalar(index)), nil
}
// ARGMAX ALONG AXIS
var indices []int
axes := make([]int, len(t.Shape()))
for i := range t.Shape() {
switch {
case i < axis:
axes[i] = i
case i == axis:
axes[len(axes)-1] = i
case i > axis:
axes[i-1] = i
}
}
// be a good citizen - borrow and return, since we're only using this AP to figure out the moves
newAP, _, err := t.Info().T(axes...)
if _, ok := err.(NoOpError); !ok && err != nil {
return
} else if ok {
newAP = t.Info().Clone()
}
it := IteratorFromDense(t)
iteratorLoadAP(it, &newAP)
lastSize := it.Shape()[len(it.Shape())-1]
newShape := it.Shape().Clone()
newShape = newShape[:len(newShape)-1]
// cleanup
defer func() {
newAP.zero()
ReturnInts(newShape)
}()
if mt, ok := t.(MaskedTensor); ok && mt.IsMasked() {
mask := mt.Mask()
if indices, err = e.E.ArgminIterMasked(typ, dataA, mask, it, lastSize); err != nil {
return
}
} else {
if indices, err = e.E.ArgminIter(typ, dataA, it, lastSize); err != nil {
return
}
}
return New(WithShape(newShape...), WithBacking(indices)), nil
}