@dataclass(unsafe_hash=True)
class SparseListLevelFType(SingleDimensionLevelFType, MutableStructFType):
_lvl_t: LevelFType
dimension_type: ftypes.FDTypeInteger = ftypes.intp
# None only as constructor input, __post_init__ defaults these to buffers
# of the child level's buffer factory.
ptr_type: Any = None
idx_type: Any = None
def __post_init__(self):
self.dimension_type = ftype(self.dimension_type)
if self.ptr_type is None:
self.ptr_type = self.buffer_factory(self.position_type)
if self.idx_type is None:
self.idx_type = self.buffer_factory(self.dimension_type)
@property
def struct_name(self):
return "SparseListLevelFType"
@property
def p_t(self):
return self.position_type
@property
def struct_fields(self):
return [
("lvl", self.lvl_t),
("dimension", self.dimension_type),
("ptr", self.ptr_type),
("idx", self.idx_type),
("qos_fill", self.position_type),
("qos_stop", self.position_type),
("prev_pos", self.position_type),
("frozen", ftypes.bool_),
]
def __str__(self):
return f"SparseListLevelFType({self.lvl_t})"
@property
def ndim(self):
return 1 + self.lvl_t.ndim
@property
def fill_type(self):
return self.lvl_t.fill_type
@property
def element_type(self):
"""
Returns the type of elements stored in the fibers.
"""
return self.lvl_t.element_type
@property
def shape_type(self):
"""
Returns the type of the shape of the fibers.
"""
return (self.dimension_type, *self.lvl_t.shape_type)
@property
def position_type(self):
"""
Returns the type of positions within the levels.
"""
return self.lvl_t.position_type
@property
def buffer_type(self):
return self.lvl_t.buffer_type
@property
def buffer_factory(self):
"""
Returns the ftype of the buffer used for the fibers.
"""
return self.lvl_t.buffer_factory
def level_cost(self, fields, stats, stats_factory, num_pos, lvl) -> float:
pos_size = np_dtype(self.position_type).itemsize
size_ptr = (num_pos + 1) * pos_size
reduce_fields = fields[lvl + 1 :]
if reduce_fields:
reduced_stats = stats_factory.aggregate(
ffuncs.or_, False, reduce_fields, stats_factory.support(stats)
)
else:
reduced_stats = stats
nnz_prefix = reduced_stats.estimate_non_fill_values()
size_idx = nnz_prefix * pos_size
return (
size_ptr
+ size_idx
+ self.lvl_t.level_cost(fields, stats, stats_factory, nnz_prefix, lvl + 1)
)
def construct(self, shape: tuple[Any, ...], *, pos: int) -> "SparseListLevel":
"""
Creates an instance of SparseListLevel.
Args:
shape: The shape to be used for the level. (mandatory)
Returns:
An instance of DenseLevel.
"""
lvl = self.lvl_t.construct(shape=shape[1:], pos=0)
return SparseListLevel(
lvl,
self.dimension_type(shape[0]),
self.ptr_type(int(pos) + 1),
self.idx_type(0),
)
def __call__(self, val: Any) -> "SparseListLevel":
"""
Convert a level to this sparse list level type.
Args:
val: A value to convert to this type.
Returns:
A SparseListLevel instance of this type.
"""
raise NotImplementedError(
f"Level conversion not yet implemented for {type(self).__name__}"
)
@property
def lvl_t(self):
return self._lvl_t
def level_format_properties(self, n):
return self.lvl_t.level_format_properties(n + 1)
def level_default_protocols(self):
return (ntn.Walk(), *self.lvl_t.level_default_protocols())
def level_protocols(self):
return (
(ntn.Walk(), ntn.Gallop(), ntn.Follow(), ntn.Extrude()),
*self.lvl_t.level_protocols(),
)
def level_lower_dim(self, ctx, lvl, r):
if r == 0:
return asm.GetAttr(lvl, asm.Literal("dimension"))
return self.lvl_t.level_lower_dim(
ctx, asm.GetAttr(lvl, asm.Literal("lvl")), r - 1
)
def level_lower_declare(self, ctx, lvl, init, op, shape, pos):
dimension = shape[0]
if dimension.result_type != self.dimension_type:
dimension = asm.Call(
asm.Literal(ffuncs.astype(self.dimension_type)), (dimension,)
)
ctx.exec(asm.SetAttr(lvl, asm.Literal("dimension"), dimension))
p_t = self.ptr_type.length_type
p = asm.Variable(ctx.freshen("p"), p_t)
stop = asm.Variable(ctx.freshen("pos_stop"), p_t)
to_size = asm.Literal(ffuncs.astype(p_t))
zero = asm.Literal(self.position_type(0))
expr = """finch
stop = to_size(pos) + 1
resize(lvl.ptr, stop)
for (p in 0:stop)
lvl.ptr[p] = zero
end
lvl.qos_fill = zero
lvl.qos_stop = zero
lvl.prev_pos = zero
lvl.frozen = false
"""
ctx.exec(parse_assembly(expr, locals(), position_type=p_t))
return self.lvl_t.level_lower_declare(
ctx, asm.GetAttr(lvl, asm.Literal("lvl")), init, op, shape[1:], zero
)
def level_lower_thaw(self, ctx, lvl, op, pos):
p_t = self.ptr_type.length_type
pos_stop = asm.Variable(ctx.freshen("pos_stop"), p_t)
p = asm.Variable(ctx.freshen("p"), p_t)
to_size = asm.Literal(ffuncs.astype(p_t))
to_pos = asm.Literal(ffuncs.astype(self.position_type))
zero = asm.Literal(self.position_type(0))
expr = """finch
pos_stop = to_size(pos)
if (lvl.frozen)
lvl.qos_fill = lvl.ptr[pos_stop]
lvl.qos_stop = lvl.qos_fill
lvl.prev_pos = zero
p = pos_stop
// Difference backwards while preceding entries are still prefix sums.
while (p > 0)
lvl.ptr[p] -= lvl.ptr[p - 1]
if (lvl.ptr[p] != zero)
if (lvl.prev_pos == zero)
lvl.prev_pos = to_pos(p)
end
end
p -= 1
end
lvl.frozen = false
end
"""
ctx.exec(parse_assembly(expr, locals(), position_type=p_t))
return self.lvl_t.level_lower_thaw(
ctx,
asm.GetAttr(lvl, asm.Literal("lvl")),
op,
asm.GetAttr(lvl, asm.Literal("qos_fill")),
)
def level_lower_freeze(self, ctx, lvl, op, pos):
p_t = self.ptr_type.length_type
pos_stop = asm.Variable(ctx.freshen("pos_stop"), p_t)
qos_stop = asm.Variable(ctx.freshen("qos_stop"), self.position_type)
p = asm.Variable(ctx.freshen("p"), p_t)
to_size = asm.Literal(ffuncs.astype(p_t))
to_idx_size = asm.Literal(ffuncs.astype(self.idx_type.length_type))
expr = """finch
pos_stop = to_size(pos)
resize(lvl.ptr, pos_stop + 1)
if (lvl.frozen == false)
for (p in 0:pos_stop)
lvl.ptr[p + 1] += lvl.ptr[p]
end
end
qos_stop = lvl.ptr[pos_stop]
resize(lvl.idx, to_idx_size(qos_stop))
lvl.qos_fill = qos_stop
lvl.qos_stop = qos_stop
lvl.frozen = true
"""
ctx.exec(parse_assembly(expr, locals(), position_type=p_t))
return self.lvl_t.level_lower_freeze(
ctx,
asm.GetAttr(lvl, asm.Literal("lvl")),
op,
asm.GetAttr(lvl, asm.Literal("qos_fill")),
)
def level_lower_increment(self, ctx, lvl, op, val, pos):
raise NotImplementedError(
"SparseListLevelFType does not support level_lower_increment."
)
def level_lower_assemble(self, ctx, lvl, start, stop):
p_t = self.ptr_type.length_type
p = asm.Variable(ctx.freshen("p"), p_t)
p_start = asm.Variable(ctx.freshen("p_start"), p_t)
p_stop = asm.Variable(ctx.freshen("p_stop"), p_t)
to_size = asm.Literal(ffuncs.astype(p_t))
length = asm.Length(asm.GetAttr(lvl, asm.Literal("ptr")))
zero = asm.Literal(self.position_type(0))
expr = """finch
p_start = to_size(start) + 1
p_stop = to_size(stop) + 1
if (length < p_stop)
resize(lvl.ptr, p_stop)
end
for (p in p_start:p_stop)
lvl.ptr[p] = zero
end
"""
ctx.exec(parse_assembly(expr, locals(), position_type=p_t))
def level_lower_unwrap(self, ctx, lvl, pos):
raise NotImplementedError(
"SparseListLevelFType does not support level_lower_unwrap."
)
def level_unfurl(self, ctx, lvl: ntn.Fiber, ext, mode: ntn.AccessMode, proto, pos):
match mode:
case ntn.Update():
return self.level_unfurl_update(ctx, lvl, ext, mode, proto, pos)
tns = lvl
level = tns.lvl
lvl_asm = ctx(level)
ptr_s = asm.GetAttr(lvl_asm, asm.Literal("ptr"))
idx_s = asm.GetAttr(lvl_asm, asm.Literal("idx"))
q = asm.Variable(ctx.freshen("q"), self.position_type)
q_stop = asm.Variable(ctx.freshen("q_stop"), self.position_type)
i_stop = asm.Variable(ctx.freshen("i_stop"), self.position_type)
i_last = asm.Variable(ctx.freshen("i_last"), self.position_type)
fill = (
ntn.Value(self.lower_fill(lvl_asm), self.element_type)
if is_dynamic(self.fill_type)
else ntn.Literal(self.fill_type.value)
)
full = ntn.Full(
fill,
tuple(
ntn.Value(self.level_lower_dim(ctx, lvl_asm, r), self.shape_type[r])
for r in range(1, self.ndim)
),
)
match proto:
case ntn.DefaultProtocol() | ntn.Walk():
step = lplt.Stepper
case ntn.Gallop():
step = lplt.Jumper
case ntn.Follow():
return self._level_unfurl_follow(ctx, lvl, pos, full)
case _:
raise NotImplementedError(
f"SparseListLevelFType does not support the {proto} protocol."
)
tmp_locals = locals()
def thunk_preamble(ctx, idx):
expr = """finch
q = ptr_s[pos]
q_stop = ptr_s[pos + 1]
i_stop = 1
i_last = 0
if (q < q_stop)
i_stop = idx_s[q]
i_last = idx_s[q_stop - 1]
end
"""
return parse_assembly(expr, tmp_locals, position_type=self.position_type)
def seek_fn(ctx, ext):
start = ctx.ctx(ext.get_start())
p_t = self.position_type
lo = asm.Variable(ctx.freshen("lo"), p_t)
hi = asm.Variable(ctx.freshen("hi"), p_t)
d = asm.Variable(ctx.freshen("d"), p_t)
m = asm.Variable(ctx.freshen("m"), p_t)
go = asm.Variable(ctx.freshen("go"), ffuncs.lt.ftype.return_type(p_t, p_t))
stop = asm.Literal(np.bool_(False))
# Move q to the first index of the fiber which is at least start.
# Gallop from q until an index is at least start, then bisect the
# last step. Expressions have one operation each, and loads only
# happen in bounds, since the assembly has no short circuits.
code = """finch
if (q < q_stop)
if (idx_s[q] < start)
lo = q
d = 1
hi = q + d
go = hi < q_stop
while (go)
if (idx_s[hi] < start)
lo = hi
d = d + d
hi = hi + d
hi = min(hi, q_stop)
go = hi < q_stop
else
go = stop
end
end
m = lo + 1
go = m < hi
while (go)
m = hi - lo
m = m >> 1
m = lo + m
if (idx_s[m] < start)
lo = m
else
hi = m
end
m = lo + 1
go = m < hi
end
q = hi
end
end
"""
return parse_assembly(
code,
tmp_locals
| {"start": start, "lo": lo, "hi": hi, "d": d, "m": m, "go": go}
| {"stop": stop},
position_type=p_t,
)
def chunk_tail_fn(ctx, idx):
pos_2 = asm.Variable(
ctx.freshen(idx, f"_pos_{self.ndim - 1}"), self.position_type
)
ctx.exec(asm.Assign(pos_2, q))
return lplt.Run(
ntn.Fiber(
ntn.Child(level),
ntn.Value(pos_2, self.position_type),
(*tns.idxs, idx),
)
)
return lplt.Thunk(
preamble=thunk_preamble,
body=lambda ctx, ext: lplt.Sequence(
head=lambda ctx, idx: step(
preamble=lambda ctx: asm.IfElse(
asm.Call(asm.L(ffuncs.lt), (q, q_stop)),
asm.Block((asm.Assign(i_stop, asm.Load(idx_s, q)),)),
asm.Block(
(
asm.Assign(
i_stop,
asm.GetAttr(lvl_asm, asm.Literal("dimension")),
),
)
),
),
stop=lambda ctx: ntn.Variable(i_stop.name, self.position_type),
chunk=lplt.Sequence(
head=lambda ctx, idx: lplt.Run(full),
split=lambda ctx, ext: ntn.Variable(
i_stop.name, self.position_type
),
tail=chunk_tail_fn,
),
next=lambda ctx: asm.Block(
(
asm.Assign(
q,
asm.Call(asm.L(ffuncs.add), (q, asm.L(self.p_t(1)))),
),
)
),
seek=seek_fn,
),
split=lambda ctx, idx: ntn.Call(
ntn.L(ffuncs.add),
(ntn.Variable(i_last.name, self.position_type), ext.get_unit()),
),
tail=lambda ctx, idx: lplt.Run(full),
),
)
def _level_unfurl_follow(self, ctx, fiber, pos, full):
lvl_asm = ctx(fiber.lvl)
p_t = self.position_type
def lookup(ctx, idx):
names = {
name: asm.Variable(ctx.freshen(name), p_t)
for name in ("q", "q_stop", "m")
}
found = asm.Variable(ctx.freshen("found"), ftypes.bool_)
# Bisect the fiber for the first index which is at least idx. Loads
# only happen in bounds, since the assembly has no short circuits.
code = """finch
q = ptr_s[pos]
q_stop = ptr_s[pos + 1]
hi = q_stop
while (q < hi)
m = hi - q
m = m >> 1
m = q + m
if (idx_s[m] < i)
q = m + 1
else
hi = m
end
end
found = false
if (q < q_stop)
if (idx_s[q] == i)
found = true
end
end
"""
ctx.exec(
parse_assembly(
code,
names
| {
"hi": asm.Variable(ctx.freshen("hi"), p_t),
"found": found,
"ptr_s": asm.GetAttr(lvl_asm, asm.Literal("ptr")),
"idx_s": asm.GetAttr(lvl_asm, asm.Literal("idx")),
"pos": pos,
"i": ctx.ctx(idx),
},
position_type=p_t,
)
)
return lplt.Switch(
found,
lplt.Run(
ntn.Fiber(
ntn.Child(fiber.lvl),
ntn.Value(names["q"], p_t),
(*fiber.idxs, idx),
)
),
lplt.Run(full),
)
return lplt.Lookup(lookup)
def level_unfurl_update(self, ctx, fiber, ext, mode, proto, pos):
# A list appends each index after the last.
match proto:
case ntn.DefaultProtocol() | ntn.Extrude():
pass
case _:
raise NotImplementedError(
f"SparseListLevelFType does not support the {proto} protocol."
)
lvl = ctx(fiber.lvl)
child = asm.GetAttr(lvl, asm.Literal("lvl"))
p_t = self.position_type
qos = asm.Variable(ctx.freshen("qos"), p_t)
dirty = ntn.Variable(ctx.freshen("dirty"), ftypes.bool_)
dirty_asm = ctx(dirty)
to_ptr_size = asm.Literal(ffuncs.astype(self.ptr_type.length_type))
to_idx_size = asm.Literal(ffuncs.astype(self.idx_type.length_type))
to_index = asm.Literal(ffuncs.astype(self.dimension_type))
to_pos = asm.Literal(ffuncs.astype(p_t))
bindings = locals()
def preamble(ctx, idx):
body = parse_assembly(
"""finch
qos = lvl.qos_fill
""",
bindings,
position_type=p_t,
)
if ctx.mode.safe or ctx.mode.debug:
return asm.Block(
(
*body.bodies,
asm.Assert(
asm.Call(
asm.Literal(ffuncs.not_),
(asm.GetAttr(lvl, asm.Literal("frozen")),),
)
),
asm.Assert(
asm.Call(
asm.Literal(ffuncs.le),
(asm.GetAttr(lvl, asm.Literal("prev_pos")), pos),
)
),
)
)
return body
def lookup(ctx, idx):
idx_asm = ctx.ctx(idx)
def prepare(ctx, _):
grow = ctx.ctx.block()
grow.exec(
parse_assembly(
"""finch
if (lvl.qos_stop == 0)
lvl.qos_stop = 1
else
lvl.qos_stop += lvl.qos_stop
end
resize(lvl.idx, to_idx_size(lvl.qos_stop))
""",
bindings,
position_type=p_t,
)
)
self.lvl_t.level_lower_assemble(
grow, child, qos, asm.GetAttr(lvl, asm.Literal("qos_stop"))
)
return asm.Block(
(
asm.If(
asm.Call(
asm.Literal(ffuncs.ge),
(qos, asm.GetAttr(lvl, asm.Literal("qos_stop"))),
),
asm.Block(tuple(grow.emit())),
),
asm.Assign(dirty_asm, asm.Literal(False)),
)
)
def finish(ctx, _):
written = parse_assembly(
"""finch
lvl.idx[to_idx_size(qos)] = to_index(idx_asm)
qos += 1
lvl.prev_pos = to_pos(pos + 1)
""",
bindings | {"idx_asm": idx_asm},
position_type=p_t,
)
match fiber:
case ntn.HollowFiber(dirty=parent_dirty):
written = asm.Block(
(
*written.bodies,
asm.Assign(ctx.ctx(parent_dirty), asm.Literal(True)),
)
)
return asm.If(dirty_asm, written)
return lplt.Thunk(
preamble=prepare,
body=lambda ctx, ext: lplt.Run(
ntn.HollowFiber(
ntn.Child(fiber.lvl),
ntn.Value(qos, p_t),
(*fiber.idxs, idx),
dirty=dirty,
)
),
epilogue=finish,
)
def epilogue(ctx, idx):
return parse_assembly(
"""finch
lvl.ptr[to_ptr_size(pos + 1)] += qos - lvl.qos_fill
lvl.qos_fill = qos
""",
bindings,
position_type=p_t,
)
return lplt.Thunk(
preamble=preamble,
body=lambda ctx, ext: lplt.Lookup(lookup),
epilogue=epilogue,
)
def from_fields(
self,
lvl,
dimension,
ptr,
idx,
qos_fill=None,
qos_stop=None,
prev_pos=0,
frozen=True,
) -> "SparseListLevel":
return SparseListLevel(
lvl, dimension, ptr, idx, qos_fill, qos_stop, prev_pos, frozen
)