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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 | 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.
self.modes: dict[lgc.Alias, ntn.AccessMode] = {
var: ntn.Read() for var in bindings
}
def freeze(self, var: lgc.Alias) -> tuple[ntn.NotationStatement, ...]:
"""Statements which leave `var` frozen, so that it can be read."""
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 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.NotationExpression, op: ntn.Literal
) -> tuple[ntn.NotationStatement, ...]:
"""Statements which reset `var` to `init`, leaving it thawed, so that it
can be updated with `op`."""
stmts = self.freeze(var)
self.modes[var] = ntn.Update(op)
return (*stmts, ntn.Declare(self.slots[var], init, op, ()))
def lower_init(self, init: lgc.LogicExpression) -> ntn.NotationExpression:
"""The value of `init`, a zero-dimensional expression. A fill expression
whose fill is static is a literal, so that kernels may specialize on it."""
if lgc.is_fill_expression(init):
fill = init.fill_type(
{var: val.fill_type for var, val in self.bindings.items()}
)
if is_static(fill):
return ntn.Literal(fill.value)
return PointwiseContext(self)(init, {})
def freeze_reads(self, expr: lgc.LogicNode) -> tuple[ntn.NotationStatement, ...]:
"""Statements which leave every tensor that `expr` reads frozen."""
reads = dict.fromkeys(
node for node in PostOrderDFS(expr) if isinstance(node, lgc.Alias)
)
return tuple(stmt for var in reads for stmt in self.freeze(var))
def overwrite(
self, var: lgc.Alias, idxs: tuple[lgc.Field, ...], rhs: lgc.Reorder
) -> ntn.NotationStatement:
"""Overwrite `var` with `rhs`, reducing the fields `var` lacks."""
# Every element of var is written, so var is reset to its fill value
# first. An empty reduction writes nothing and leaves var as it was, so
# var is only reset when every reduced extent is nonzero.
arg_dims = rhs.arg.dimmap(merge_shapes, self.shapes)
dims = dict(zip(rhs.arg.fields(), arg_dims, strict=True))
reduced = [dims.get(idx) for idx in rhs.idxs if idx not in idxs]
nonempty = tuple(
ntn.Call(ntn.Literal(ffuncs.gt), (dim, ntn.Literal(dim.result_type(0))))
for dim in reduced
if dim is not None
)
# An overwrite keeps the static fill of var, and otherwise gives var the
# fill of rhs.
fill = self.bindings[var].fill_type
init = (
ntn.Literal(fill.value)
if is_static(fill)
else self.lower_init(lgc.fill_expression(rhs))
)
if not nonempty:
return self.fold(var, idxs, ffuncs.overwrite, rhs, init)
# Each branch must leave every tensor in the same mode.
stmts = (*self.freeze_reads(rhs), *self.freeze(var))
body = self.fold(var, idxs, ffuncs.overwrite, rhs, init)
cond = nonempty[0]
if len(nonempty) > 1:
cond = ntn.Call(ntn.Literal(ffuncs.and_), nonempty)
return ntn.Block((*stmts, ntn.If(cond, ntn.Block((body, *self.freeze(var))))))
def fold(
self,
var: lgc.Alias,
idxs: tuple[lgc.Field, ...],
op: FinchOperator,
rhs: lgc.Reorder,
init: ntn.NotationExpression | None,
) -> ntn.NotationStatement:
"""Fold `rhs` into `var` with `op`, after resetting `var` to `init`
unless it is None."""
stmts = self.freeze_reads(rhs)
if init is None:
stmts += self.thaw(var, ntn.Literal(op))
else:
reduced = any(idx not in idxs for idx in rhs.fields())
match init:
case ntn.Literal(val) if op == ffuncs.overwrite and not reduced:
op = ffuncs.init_write(val)
stmts += self.declare(var, init, ntn.Literal(op))
match rhs:
case lgc.Reorder(lgc.Table() as arg, loop_order):
body = self._lower_query_of_reorder(var, op, arg, idxs, loop_order)
case lgc.Reorder():
body = self._lower_query_of_aggregate(var, op, rhs, idxs)
return ntn.Block((*stmts, body))
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):
stmts = (self(body) for body in bodies)
return ntn.Block(tuple(s for s in stmts if s != ntn.Block(())))
case lgc.Query(lgc.Table(lgc.Alias() as lhs, idxs), lgc.Table() as arg):
# A copy starts from the fill value of arg, and the fields of
# arg which lhs lacks are reduced by overwriting.
loop_order = (*idxs, *(idx for idx in arg.idxs if idx not in idxs))
return self.fold(
lhs,
idxs,
ffuncs.overwrite,
lgc.Reorder(arg, loop_order),
self.lower_init(lgc.fill_expression(arg)),
)
case lgc.Query(
lgc.Table(lgc.Alias() as lhs, idxs),
lgc.Aggregate(lgc.Literal(op), init, lgc.Reorder() as arg, _),
):
stmts = self.freeze_reads(init)
body = self.fold(lhs, idxs, op, arg, self.lower_init(init))
return ntn.Block((*stmts, body))
case lgc.QueryInto(
lgc.Table(lgc.Alias() as lhs, idxs),
lgc.Literal(ffuncs.overwrite),
lgc.Literal(init),
):
op = ntn.Literal(ffuncs.overwrite)
fill = self.bindings[lhs].fill_type
if is_static(fill) and not bool(ffuncs.same(init, fill.value)):
# A static fill can't change, so lhs is declared with its
# fill, and then every element is overwritten with init.
stmts = self.declare(lhs, ntn.Literal(fill.value), op)
arg = lgc.Reorder(lgc.Literal(init), idxs)
body = self._lower_query_of_aggregate(
lhs, ffuncs.overwrite, arg, idxs
)
return ntn.Block((*stmts, body))
return ntn.Block(self.declare(lhs, ntn.Literal(init), op))
case lgc.QueryInto(
lgc.Table(lgc.Alias() as lhs, idxs),
lgc.Literal(op),
lgc.Reorder() as rhs,
):
if op == ffuncs.overwrite:
return self.overwrite(lhs, idxs, rhs)
return self.fold(lhs, idxs, op, rhs, None)
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}")
|