-
Notifications
You must be signed in to change notification settings - Fork 2
/
intrinsic.go
321 lines (289 loc) · 8.37 KB
/
intrinsic.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
package shapes
import (
"fmt"
"sort"
"github.com/pkg/errors"
)
// intrinsic.go describes all the intrinsic operations that a shape can do.
// By intrinsic operation, I mean that these are symbolic versions of the shape operations.
// All comments/documentation will contain a phrase "symbolic version of: XXX"
// IndexOf gets the size of a given shape (expression) at the given index.
//
// IndexOf is the symbolic version of doing s[i], where s is a Shape.
type IndexOf struct {
I Size
A Expr
}
func (i IndexOf) isExpr() {}
func (i IndexOf) Format(s fmt.State, r rune) { fmt.Fprintf(s, "%v[%d]", i.A, i.I) }
func (i IndexOf) apply(ss substitutions) substitutable {
return IndexOf{
I: i.I,
A: i.A.apply(ss).(Expr),
}
}
func (i IndexOf) freevars() varset { return i.A.freevars() }
func (i IndexOf) subExprs() []substitutableExpr {
return []substitutableExpr{i.I, i.A.(substitutableExpr)}
}
func (i IndexOf) depth() int { return i.A.depth() + 1 }
func (i IndexOf) isValid() bool { return true }
func (i IndexOf) resolveSize() (Size, error) {
if len(i.A.freevars()) > 0 {
return 0, errors.Errorf("Cannot resolve IndexOf %v - free vars found", i)
}
switch at := i.A.(type) {
case Shapelike:
if at.Dims() <= int(i.I) {
return 0, errors.Errorf("Expression %v has %d Dims. Want to get index of %d", at, at.Dims(), i.I)
}
sz, err := at.DimSize(int(i.I))
if err != nil {
return 0, errors.Wrapf(err, "Cannot get Index %d of %v", i.I, i.A)
}
switch s := sz.(type) {
case Size:
return s, nil
case sizeOp:
return s.resolveSize()
default:
return 0, errors.Errorf("Sizelike of %v (Index %d of %v)is unresolvable ", sz, i.I, i.A)
}
default:
return 0, errors.Errorf("Cannot resolve IndexOf %v - expression of %T is unhandled", i.A, i.A)
}
}
// TransposeOf is the symbolic version of doing s.T(axes...)
type TransposeOf struct {
Axes Axes
A Expr
}
func (t TransposeOf) isExpr() {}
func (t TransposeOf) Format(s fmt.State, r rune) { fmt.Fprintf(s, "T%x %v", t.Axes, t.A) }
func (t TransposeOf) apply(ss substitutions) substitutable {
return TransposeOf{
Axes: t.Axes,
A: t.A.apply(ss).(Expr),
}
}
func (t TransposeOf) freevars() varset { return t.A.freevars() }
func (t TransposeOf) subExprs() []substitutableExpr {
return []substitutableExpr{t.Axes, t.A.(substitutableExpr)}
}
func (t TransposeOf) depth() int { return t.A.depth() + 1 }
func (t TransposeOf) resolve() (Expr, error) {
switch at := t.A.(type) {
case Shapelike:
retVal, err := at.T(t.Axes...)
_, ok := err.(NoOpError)
if !ok && err != nil {
return nil, err
}
return retVal.(Expr), nil
default:
return nil, errors.Errorf("Cannot transpose Expression %v of %T", t.A, t.A)
}
panic("Unreachable")
}
// SliceOf is an intrinsic operation, symbolically representing a slicing operation.
type SliceOf struct {
Slice Slicelike
A Expr
}
func (s SliceOf) isExpr() {}
func (s SliceOf) Format(st fmt.State, r rune) {
switch s.Slice.(type) {
case Slice:
fmt.Fprintf(st, "%v%v", s.A, s.Slice)
case Slices:
fmt.Fprintf(st, "%v%v", s.A, s.Slice)
case Var:
fmt.Fprintf(st, "%v[%v]", s.A, s.Slice)
}
}
func (s SliceOf) apply(ss substitutions) substitutable {
return SliceOf{
Slice: s.Slice.apply(ss).(Slicelike),
A: s.A.apply(ss).(Expr),
}
}
func (s SliceOf) freevars() varset { return s.A.freevars() }
func (s SliceOf) subExprs() []substitutableExpr {
return []substitutableExpr{s.Slice, s.A.(substitutableExpr)}
}
func (s SliceOf) depth() int { return s.A.depth() + 1 }
func (s SliceOf) resolve() (Expr, error) {
switch at := s.A.(type) {
case Shapelike:
switch sl := s.Slice.(type) {
case Slice:
retVal, err := at.S(sl)
if err != nil {
return nil, errors.Wrapf(err, "Unable to resolve %v - .S() failed", s)
}
return retVal.(Expr), nil
case Slices:
retVal, err := at.S(sl...)
if err != nil {
return nil, errors.Wrapf(err, "Unable to resolve %v - .S() failed", s)
}
return retVal.(Expr), nil
default:
return s, nil
}
default:
return nil, errors.Errorf("Cannot slice Expression %v of %T", s.A, s.A)
}
}
// isValid makes SliceOf an Operation.
func (s SliceOf) isValid() bool {
_, isVar := s.Slice.(Var)
if isVar {
return false
}
switch a := s.A.(type) {
case Size:
return true
case Operation:
return a.isValid()
default:
return false
}
}
type ConcatOf struct {
Along Axis
A, B Expr
}
func (c ConcatOf) isExpr() {}
func (c ConcatOf) Format(s fmt.State, r rune) { fmt.Fprintf(s, "%v :{%d}: %v", c.A, c.Along, c.B) }
func (c ConcatOf) apply(ss substitutions) substitutable {
return ConcatOf{
Along: c.Along,
A: c.A.apply(ss).(Expr),
B: c.B.apply(ss).(Expr),
}
}
func (c ConcatOf) freevars() varset { return (exprtup{c.A, c.B}).freevars() }
func (c ConcatOf) subExprs() []substitutableExpr {
return []substitutableExpr{c.Along, c.A.(substitutableExpr), c.B.(substitutableExpr)}
}
func (c ConcatOf) depth() int { return max(c.A.depth(), c.B.depth()) + 1 }
type RepeatOf struct {
Along Axis
Repeats []Size
A Expr
}
func (r RepeatOf) isExpr() {}
func (r RepeatOf) Format(s fmt.State, ru rune) {
fmt.Fprintf(s, "Repeat%x{%v} %v", r.Along, r.Repeats, r.A)
}
func (r RepeatOf) apply(ss substitutions) substitutable {
return RepeatOf{
Along: r.Along,
Repeats: r.Repeats,
A: r.A.apply(ss).(Expr),
}
}
func (r RepeatOf) freevars() varset { return r.A.freevars() }
func (r RepeatOf) subExprs() []substitutableExpr {
return []substitutableExpr{r.Along, r.A.(substitutableExpr)}
}
func (r RepeatOf) depth() int { return r.A.depth() + 1 }
func (r RepeatOf) resolve() (Expr, error) {
switch at := r.A.(type) {
case Shapelike:
retVal, _, _, err := at.Repeat(r.Along, sizesToInts(r.Repeats)...)
if err != nil {
return nil, errors.Wrapf(err, "Unable to resolve %v. .Repeat() failed", r)
}
return retVal.(Expr), nil
default:
return nil, errors.Errorf("Cannot Repeat Expression %v of %T", r.A, r.A)
}
}
// BroadcastOf represents the results of mutually broadcasting A and B expr.
type BroadcastOf struct {
A, B Expr
}
func (b BroadcastOf) isExpr() {}
func (b BroadcastOf) Format(s fmt.State, r rune) { fmt.Fprintf(s, "(%v||%v)", b.A, b.B) }
func (b BroadcastOf) apply(ss substitutions) substitutable {
return BroadcastOf{
A: b.A.apply(ss).(Expr),
B: b.B.apply(ss).(Expr),
}
}
func (b BroadcastOf) freevars() varset { return append(b.A.freevars(), b.B.freevars()...) }
func (b BroadcastOf) subExprs() []substitutableExpr {
return []substitutableExpr{b.A.(substitutableExpr), b.B.(substitutableExpr)}
}
func (b BroadcastOf) depth() int { return max(b.A.depth(), b.B.depth()) + 1 }
func (b BroadcastOf) resolve() (Expr, error) {
A, aok := b.A.(Shape)
B, bok := b.B.(Shape)
if aok && bok {
return CalcBroadcastShape(A, B), nil
}
return b, noopError{}
}
// ReductOf represents the results of reducing A along the given axis.
type ReductOf struct {
A Expr
Along Axis
}
func Reduce(a Expr, along Axes) ReductOf {
ints := axesToInts(along)
sort.Sort(sort.Reverse(sort.IntSlice(ints)))
// innermost reductions first
var retVal Expr = a
for i := range ints {
retVal = ReductOf{retVal, Axis(ints[i])}
}
return retVal.(ReductOf)
}
func (r ReductOf) isExpr() {}
func (r ReductOf) Format(s fmt.State, c rune) { fmt.Fprintf(s, "/%x%v", r.Along, r.A) }
func (r ReductOf) apply(ss substitutions) substitutable {
return ReductOf{
A: r.A.apply(ss).(Expr),
Along: r.Along,
}
}
func (r ReductOf) freevars() varset { return r.A.freevars() }
func (r ReductOf) subExprs() []substitutableExpr {
return []substitutableExpr{r.A.(substitutableExpr)}
}
func (r ReductOf) depth() int { return r.A.depth() + 1 }
func (r ReductOf) resolve() (Expr, error) {
A := r.A
for {
switch at := A.(type) {
case Shape:
along := ResolveAxis(r.Along, at)
if along == AllAxes {
return ScalarShape(), nil
}
retVal := make(Shape, len(at)-1)
copy(retVal, at[:along])
copy(retVal[along:], at[along+1:])
return retVal, nil
case Abstract:
along := ResolveAxis(r.Along, at)
if along == AllAxes {
return ScalarShape(), nil
}
retVal := make(Abstract, len(at)-1)
copy(retVal, at[:along])
copy(retVal[along:], at[along+1:])
return retVal, nil
case resolver:
expr, err := at.resolve()
if err != nil {
return nil, err
}
A = expr
default:
return nil, errors.Errorf("Cannot reduce Expression %v of %T", r.A, r.A)
}
}
}