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
350
351
352
353
354
355
356
357
358
359
360 | class NotationContext:
"""
Compiles Finch Logic to Finch Notation. Holds the state of the
compilation process.
"""
def __init__(
self,
bindings: dict[lgc.Alias, TensorFType],
args: dict[lgc.Alias, ntn.Variable],
slots: dict[lgc.Alias, ntn.Slot],
shapes: dict[lgc.Alias, tuple[ntn.Variable | None, ...]],
shape_types: dict[lgc.Alias, tuple[FType | None, ...]] | None = None,
epilogue: Iterable[ntn.NotationStatement] | None = None,
):
self.bindings = bindings
self.args = args
self.slots = slots
self.shapes = shapes
self.equiv: dict[ntn.Variable, ntn.Variable] = {}
if shape_types is None:
shape_types = {var: val.shape_type for var, val in bindings.items()}
self.shape_types = shape_types
if epilogue is None:
epilogue = ()
self.epilogue = epilogue
# The mode of each tensor at the current statement: frozen tensors are
# Read, and thawed tensors are Updated with the op they were thawed
# with. Initializations are only declared once we know how the tensor
# is used next, so their inits are held here until then.
self.modes: dict[lgc.Alias, ntn.AccessMode] = {
var: ntn.Read() for var in bindings
}
self.inits: dict[lgc.Alias, ntn.Literal] = {}
def freeze(self, var: lgc.Alias) -> tuple[ntn.NotationStatement, ...]:
"""Statements which leave `var` frozen, so that it can be read."""
if var in self.inits:
# Nothing was folded into the init, so it is declared as it is.
op = ntn.Literal(ffuncs.overwrite)
init = self.inits.pop(var)
return (
ntn.Declare(self.slots[var], init, op, ()),
ntn.Freeze(self.slots[var], op),
)
match self.modes[var]:
case ntn.Update(op):
self.modes[var] = ntn.Read()
return (ntn.Freeze(self.slots[var], op),)
return ()
def thaw(
self, var: lgc.Alias, op: ntn.Literal
) -> tuple[ntn.NotationStatement, ...]:
"""Statements which leave `var` thawed, so that it can be updated with
`op`."""
if var in self.inits:
self.modes[var] = ntn.Update(op)
return (ntn.Declare(self.slots[var], self.inits.pop(var), op, ()),)
if self.modes[var] == ntn.Update(op):
return ()
stmts = self.freeze(var)
self.modes[var] = ntn.Update(op)
return (*stmts, ntn.Thaw(self.slots[var], op))
def declare(
self, var: lgc.Alias, init: ntn.Literal, op: ntn.Literal
) -> tuple[ntn.NotationStatement, ...]:
"""Statements which reset `var` to `init`, leaving it thawed, so that it
can be updated with `op`."""
self.inits.pop(var, None)
stmts = self.freeze(var)
self.modes[var] = ntn.Update(op)
return (*stmts, ntn.Declare(self.slots[var], init, op, ()))
def fill_literal(self, var: lgc.Alias) -> ntn.Literal:
fill = self.bindings[var].fill_value
return ntn.Literal(fill if is_dynamic(fill) else fill.value)
def _lower_query_of_reorder(
self,
query_lhs: lgc.Alias,
op: FinchOperator,
arg: lgc.Table,
reorder_idxs: tuple[lgc.Field, ...],
loop_order: tuple[lgc.Field, ...],
):
# The table is broadcast over the fields only the output has.
arg_dims = arg.dimmap(merge_shapes, self.shapes)
shapes_map = {
**dict(zip(reorder_idxs, self.shapes[query_lhs], strict=True)),
**dict(zip(arg.idxs, arg_dims, strict=True)),
}
shapes = {
idx: shapes_map.get(idx) or ntn.Literal(ftypes.intp(1))
for idx in loop_order
}
arg_types = arg.shape_type(self.shape_types)
shape_type_map = {
**dict(zip(reorder_idxs, self.shape_types[query_lhs], strict=True)),
**dict(zip(arg.idxs, arg_types, strict=True)),
}
shape_type = {idx: shape_type_map.get(idx) or ftypes.intp for idx in loop_order}
# Visit the output in loop order. Revisit an input field with a fresh
# index when its storage order conflicts, restricting that loop to the
# original index below. This keeps sparse reads and writes concordant.
loop_idxs = []
remap_idxs = {}
read_idxs = []
pending = iter(arg.idxs)
read_idx = next(pending, None)
for idx in loop_order:
loop_idxs.append(idx)
if idx == read_idx:
read_idxs.append(idx)
read_idx = next(pending, None)
while read_idx is not None and read_idx in loop_idxs:
new_idx = lgc.Field(gensym(f"{read_idx.name}_"))
remap_idxs[new_idx] = read_idx
loop_idxs.append(new_idx)
read_idxs.append(new_idx)
read_idx = next(pending, None)
assert read_idx is None
loops = {
idx: ntn.Variable(
gensym(idx.name),
shape_type.get(idx) or shape_type[remap_idxs[idx]],
)
for idx in loop_idxs
}
ctx = PointwiseContext(self)
rhs = ctx(lgc.Table(arg.tns, tuple(read_idxs)), loops)
lhs_access = ntn.Access(
self.slots[query_lhs],
ntn.Update(ntn.Literal(op)),
tuple(loops[idx] for idx in reorder_idxs),
)
body: ntn.NotationStatement = ntn.Increment(lhs_access, rhs)
for idx in reversed(loop_idxs):
stop = shapes.get(idx) or shapes[remap_idxs[idx]]
ext = ntn.Call(
ntn.Literal(make_extent),
(ntn.Literal(stop.result_type(0)), stop),
)
if idx in remap_idxs:
body = ntn.If(
ntn.Call(
ntn.Literal(ffuncs.eq),
(loops[idx], loops[remap_idxs[idx]]),
),
body,
)
body = ntn.Loop(
loops[idx],
ext,
body,
)
return body
def _lower_query_of_aggregate(
self,
query_lhs: lgc.Alias,
agg_op: FinchOperator,
agg_arg: lgc.Reorder,
output_idxs: tuple[lgc.Field, ...],
):
# The loop order holds every field of the output, and the argument is
# broadcast over the fields only the output has.
loop_idxs = agg_arg.idxs
arg_dims = agg_arg.arg.dimmap(merge_shapes, self.shapes)
shapes_map = {
**dict(zip(output_idxs, self.shapes[query_lhs], strict=True)),
**dict(zip(agg_arg.arg.fields(), arg_dims, strict=True)),
}
shapes = {idx: shapes_map.get(idx) or ntn.Literal(1) for idx in loop_idxs}
arg_types = agg_arg.arg.shape_type(self.shape_types)
shape_type_map = {
**dict(zip(output_idxs, self.shape_types[query_lhs], strict=True)),
**dict(zip(agg_arg.arg.fields(), arg_types, strict=True)),
}
shape_type = {idx: shape_type_map.get(idx) or ftypes.intp for idx in loop_idxs}
loops = {
idx: ntn.Variable(gensym(idx.name), shape_type[idx]) for idx in loop_idxs
}
ctx = PointwiseContext(self)
rhs = ctx(agg_arg.arg, loops)
lhs_access = ntn.Access(
self.slots[query_lhs],
ntn.Update(ntn.Literal(agg_op)),
tuple(loops[idx] for idx in output_idxs),
)
body: ntn.NotationStatement = ntn.Increment(lhs_access, rhs)
for idx in reversed(loop_idxs):
ext = ntn.Call(
ntn.Literal(make_extent),
(ntn.Literal(shape_type[idx](0)), shapes[idx]),
)
body = ntn.Loop(
loops[idx],
ext,
body,
)
return body
def __call__(self, prgm: lgc.LogicStatement) -> ntn.NotationStatement:
"""
Lower Finch Logic to Finch Notation. First we check for early
simplifications, then we call the normal lowering for the outermost
node.
"""
match prgm:
case lgc.Plan(bodies):
# Initializations lower to nothing until the tensor is used.
stmts = (self(body) for body in bodies)
return ntn.Block(tuple(s for s in stmts if s != ntn.Block(())))
case lgc.QueryInto(
lgc.Table(lgc.Alias() as lhs, _),
lgc.Literal(ffuncs.overwrite),
lgc.Literal(init),
):
# An initialization is declared by the statement which next
# uses the tensor, since the declaration needs to know the op
# the tensor will be updated with. An earlier init which is still
# pending is overwritten, so it is dropped.
self.inits.pop(lhs, None)
stmts = self.freeze(lhs)
self.inits[lhs] = ntn.Literal(init)
return ntn.Block(stmts)
case lgc.QueryInto(
lgc.Table(lgc.Alias() as lhs, idxs), lgc.Literal(op), rhs
):
reads = dict.fromkeys(
node for node in PostOrderDFS(rhs) if isinstance(node, lgc.Alias)
)
stmts = tuple(stmt for var in reads for stmt in self.freeze(var))
if op == ffuncs.overwrite:
# Overwriting every element of lhs resets it, so it is
# declared, starting from its init or else its fill value.
init = self.inits.pop(lhs, None)
if init is None:
init = self.fill_literal(lhs)
reduced = any(idx not in idxs for idx in rhs.fields())
if not reduced and not is_dynamic(init.val):
op = ffuncs.init_write(init.val)
stmts += self.declare(lhs, init, ntn.Literal(op))
else:
stmts += self.thaw(lhs, ntn.Literal(op))
match rhs:
case lgc.Reorder(lgc.Table() as arg, loop_order):
body = self._lower_query_of_reorder(
lhs, op, arg, idxs, loop_order
)
case lgc.Reorder():
body = self._lower_query_of_aggregate(lhs, op, rhs, idxs)
return ntn.Block((*stmts, body))
case lgc.Produces(args):
vars: list[lgc.Alias] = []
for var in args:
assert isinstance(var, lgc.Alias)
vars.append(var)
return ntn.Block(
(
*(stmt for var in self.bindings for stmt in self.freeze(var)),
*self.epilogue,
ntn.Return(
ntn.Call(
ntn.Literal(ffuncs.make_tuple),
tuple(self.args[var] for var in vars),
)
),
)
)
case _:
raise Exception(f"Unrecognized logic: {prgm}")
|