@dataclass(unsafe_hash=True)
class LoTriMaskFType(LevelFType, ImmutableStructFType):
body: SingleDimensionLevelFType | MultiDimensionLevelFType
@property
def element_type(self):
return self.body.element_type
@property
def buffer_type(self):
return self.body.buffer_type
@property
def position_type(self):
return self.body.position_type
@property
def fill_type(self):
return self.body.fill_type
@property
def shape_type(self):
return self.body.shape_type
@property
def buffer_factory(self):
return self.body.buffer_factory
@property
def ndim(self):
return self.body.ndim
@property
def lvl_t(self):
return self.body.lvl_t
def level_cost(self, fields, stats, stats_factory, num_pos, lvl): ...
def construct(self, shape: tuple[Any, ...], *, pos: int) -> "LoTriMask":
return LoTriMask(self.lvl_t.construct(shape=shape, pos=pos))
def __call__(self, val: Any) -> "LoTriMask":
"""
Convert a level to this lower triangular mask level type.
Args:
val: A value to convert to this type.
Returns:
A LoTriMask instance of this type.
"""
raise NotImplementedError(
f"Level conversion not yet implemented for {type(self).__name__}"
)
def level_lower_freeze(self, ctx, lvl, op, pos):
return self.body.level_lower_freeze(
ctx, asm.GetAttr(lvl, asm.Literal("body")), op, pos
)
def level_lower_thaw(self, ctx, lvl, op, pos):
return self.body.level_lower_thaw(
ctx, asm.GetAttr(lvl, asm.Literal("body")), op, pos
)
def level_lower_unwrap(self, ctx, lvl, pos):
body = ntn.Fiber(
ntn.Child(lvl.lvl, "body"),
lvl.pos,
lvl.idxs,
)
return self.body.level_lower_unwrap(
ctx,
body,
pos,
)
def level_lower_increment(self, ctx, lvl, op, val, pos):
body = ntn.Fiber(
ntn.Child(lvl.lvl, "body"),
lvl.pos,
lvl.idxs,
)
return self.body.level_lower_increment(
ctx,
body,
op,
val,
pos,
)
def level_lower_declare(self, ctx, lvl, init, op, shape, pos):
return self.body.level_lower_declare(
ctx, asm.GetAttr(lvl, asm.Literal("body")), init, op, shape, pos
)
def level_unfurl(self, ctx, lvl: ntn.Fiber, ext, mode, proto, pos):
# The mask walks the triangle itself, so its body reads with its default.
match proto:
case ntn.Walk():
proto = ntn.DefaultProtocol()
tns = lvl
level = tns.lvl
def child_accessor(ctx, idx):
body_view = ntn.Fiber(
ntn.Child(level, "body"),
tns.pos,
tns.idxs,
)
return self.body.level_unfurl(ctx, body_view, ext, mode, proto, pos)
lvl_o = asm.GetAttr(ctx(tns.lvl), asm.Literal("body"))
fill = (
ntn.Value(self.body.lower_fill(lvl_o), self.element_type)
if is_dynamic(self.fill_type)
else ntn.Literal(self.fill_type.value)
)
full = ntn.Full(
fill,
tuple(
ntn.Value(self.body.level_lower_dim(ctx, lvl_o, r), self.shape_type[r])
for r in range(1, self.ndim)
),
)
visited_idxs = tns.idxs
return lplt.Sequence(
head=lambda ctx, idx: child_accessor(ctx, idx),
split=lambda ctx, ext: ntn.Call(
ntn.L(ffuncs.add), (visited_idxs[-1], ext.get_unit())
),
tail=lambda ctx, idx: lplt.Run(full),
)
def level_lower_dim(self, ctx, lvl, r):
return self.body.level_lower_dim(ctx, asm.GetAttr(lvl, asm.Literal("body")), r)
def from_fields(self, lvl) -> "LoTriMask":
return LoTriMask(lvl)
def __str__(self):
return f"LoTriMaskFType({self.body})"
@property
def struct_name(self):
return "LoTriMaskFType"
@property
def struct_fields(self):
return [("body", self.body)]
def level_format_properties(self, n: int) -> list[FormatProperty]:
raise NotImplementedError
def level_default_protocols(self):
# The mask walks its body within the triangle.
return (ntn.Walk(), *self.body.level_default_protocols()[1:])
def level_protocols(self):
return ((ntn.Walk(),), *self.body.level_protocols()[1:])