Skip to content

masks

LoTriMask dataclass

Bases: Level

Lower triangular mask level.

Source code in src/finch/tensor/masks.py
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
@dataclass
class LoTriMask(Level):
    """
    Lower triangular mask level.
    """

    lvl: SingleDimensionLevel | MultiDimensionLevel

    @property
    def ftype(self) -> LoTriMaskFType:
        ftype = self.lvl.ftype
        assert isinstance(ftype, SingleDimensionLevelFType | MultiDimensionLevelFType)
        return LoTriMaskFType(ftype)

    @property
    def val(self) -> Any:
        return self.lvl.val

    @property
    def shape(self):
        return self.lvl.shape

    @property
    def body(self):
        return self.lvl

    def with_fill(self, fill_value: AbstractFill) -> Self:
        return type(self)(self.lvl.with_fill(fill_value))

LoTriMaskFType dataclass

Bases: LevelFType, ImmutableStructFType

Source code in src/finch/tensor/masks.py
 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
@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, obj, pos):
        body = ntn.Fiber(
            ntn.Child(obj.lvl, "body"),
            obj.pos,
            obj.idxs,
        )
        return self.body.level_lower_unwrap(
            ctx,
            body,
            pos,
        )

    def level_lower_increment(self, ctx, obj, op, val, pos):
        body = ntn.Fiber(
            ntn.Child(obj.lvl, "body"),
            obj.pos,
            obj.idxs,
        )
        return self.body.level_lower_increment(
            ctx,
            body,
            op,
            val,
            pos,
        )

    def level_lower_declare(self, ctx, tns, init, op, shape, pos):
        return self.body.level_lower_declare(
            ctx, asm.GetAttr(tns, asm.Literal("body")), init, op, shape, pos
        )

    def level_unfurl(self, ctx, fiber: ntn.Fiber, ext, mode, proto, pos):
        tns = fiber
        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 = asm.GetAttr(ctx(tns.lvl), asm.Literal("body"))
        fill = (
            ntn.Value(self.body.lower_fill(lvl), 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, 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, obj, r):
        return self.body.level_lower_dim(ctx, asm.GetAttr(obj, 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

__call__(val)

Convert a level to this lower triangular mask level type.

Parameters:

Name Type Description Default
val Any

A value to convert to this type.

required

Returns: A LoTriMask instance of this type.

Source code in src/finch/tensor/masks.py
63
64
65
66
67
68
69
70
71
72
73
74
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__}"
    )