Skip to content

Index

DenseLevel dataclass

Bases: SingleDimensionLevel

A class representing dense level.

Source code in src/finch/tensor/level/dense_level.py
@dataclass
class DenseLevel(SingleDimensionLevel):
    """
    A class representing dense level.
    """

    lvl: Level
    dimension: np.integer
    stride: np.integer = field(init=False)

    def __post_init__(self):
        # The stride is stored so that kernels can write it back; it must be
        # recomputed whenever the dimensions of the dense levels below change.
        dim_t = ftype(self.dimension)
        match self.lvl:
            case DenseLevel():
                self.stride = dim_t(self.lvl.dimension * self.lvl.stride)
            case _:
                self.stride = dim_t(1)

    @property
    def shape(self) -> tuple:
        return (self.dimension, *self.lvl.shape)

    @property
    def ftype(self) -> DenseLevelFType:
        return DenseLevelFType(self.lvl.ftype, ftype(self.dimension))

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

    def __str__(self):
        return f"DenseLevel(lvl={self.lvl}, dim={self.dimension})"

DenseLevelFType dataclass

Bases: SingleDimensionLevelFType, MutableStructFType

Source code in src/finch/tensor/level/dense_level.py
@dataclass(unsafe_hash=True)
class DenseLevelFType(SingleDimensionLevelFType, MutableStructFType):
    _lvl_t: LevelFType
    dimension_type: ftypes.FDTypeInteger = ftypes.intp

    @property
    def struct_name(self):
        return "DenseLevelFType"

    @property
    def struct_fields(self):
        return [
            ("lvl", self.lvl_t),
            ("dimension", self.dimension_type),
            ("stride", self.dimension_type),
        ]

    def struct_setattr(self, obj, attr, value):
        setattr(obj, attr, value)

    def level_cost(self, fields, stats, stats_factory, num_pos, lvl) -> float:
        n = stats.get_dim_size(fields[lvl])
        return self.lvl_t.level_cost(fields, stats, stats_factory, num_pos * n, lvl + 1)

    def __post_init__(self):
        self.dimension_type = ftype(self.dimension_type)

    def construct(self, shape: tuple[Any, ...], *, pos: int) -> DenseLevel:
        """
        Creates an instance of DenseLevel with the given ftype.

        Args:
            shape: The shape to be used for the level. (mandatory)
        Returns:
            An instance of DenseLevel.
        """
        dimension = self.dimension_type(shape[0])
        lvl = self.lvl_t.construct(
            shape=shape[1:],
            pos=int(pos) * int(dimension),
        )
        return DenseLevel(lvl, dimension)

    def __call__(self, val: Any) -> DenseLevel:
        """
        Convert a level to this dense level type.

        Args:
            val: A value to convert to this type.
        Returns:
            A DenseLevel instance of this type.
        """
        raise NotImplementedError(
            f"Level conversion not yet implemented for {type(self).__name__}"
        )

    def __str__(self):
        return f"DenseLevelFType({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

    @property
    def lvl_t(self):
        return self._lvl_t

    def level_format_properties(self, n):
        return [Dense(tuple(range(n + 1)))] + self.lvl_t.level_format_properties(n + 1)

    def level_default_protocols(self):
        return (ntn.Follow(), *self.lvl_t.level_default_protocols())

    def level_protocols(self):
        return (
            (ntn.Follow(), ntn.Extrude(), ntn.Laminate()),
            *self.lvl_t.level_protocols(),
        )

    def level_lower_dim(self, ctx, obj, r):
        if r == 0:
            return asm.GetAttr(obj, asm.Literal("dimension"))
        return self.lvl_t.level_lower_dim(
            ctx, asm.GetAttr(obj, asm.Literal("lvl")), r - 1
        )

    def level_lower_declare(self, ctx, tns, 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(tns, asm.Literal("dimension"), dimension))
        pos = asm.Call(
            asm.Literal(ffuncs.mul),
            (pos, asm.GetAttr(tns, asm.Literal("dimension"))),
        )
        child = asm.GetAttr(tns, asm.Literal("lvl"))
        self.lvl_t.level_lower_declare(ctx, child, init, op, shape[1:], pos)
        # Compiled structs store the stride, so refresh it after sizing the child.
        match self.lvl_t:
            case DenseLevelFType():
                stride = asm.Call(
                    asm.Literal(ffuncs.mul),
                    (
                        asm.GetAttr(child, asm.Literal("dimension")),
                        asm.GetAttr(child, asm.Literal("stride")),
                    ),
                )
            case _:
                stride = asm.Literal(self.dimension_type(1))
        if stride.result_type != self.dimension_type:
            stride = asm.Call(
                asm.Literal(ffuncs.astype(self.dimension_type)), (stride,)
            )
        ctx.exec(asm.SetAttr(tns, asm.Literal("stride"), stride))

    def level_lower_freeze(self, ctx, lvl, op, pos):
        pos = asm.Call(
            asm.Literal(ffuncs.mul),
            (pos, asm.GetAttr(lvl, asm.Literal("dimension"))),
        )
        return self.lvl_t.level_lower_freeze(
            ctx, asm.GetAttr(lvl, asm.Literal("lvl")), op, pos
        )

    def level_lower_thaw(self, ctx, lvl, op, pos):
        pos = asm.Call(
            asm.Literal(ffuncs.mul),
            (pos, asm.GetAttr(lvl, asm.Literal("dimension"))),
        )
        return self.lvl_t.level_lower_thaw(
            ctx, asm.GetAttr(lvl, asm.Literal("lvl")), op, pos
        )

    def level_lower_assemble(self, ctx, lvl, start, stop):
        dimension = asm.GetAttr(lvl, asm.Literal("dimension"))
        self.lvl_t.level_lower_assemble(
            ctx,
            asm.GetAttr(lvl, asm.Literal("lvl")),
            asm.Call(asm.Literal(ffuncs.mul), (start, dimension)),
            asm.Call(asm.Literal(ffuncs.mul), (stop, dimension)),
        )

    def level_lower_increment(self, ctx, obj, op, val, pos):
        raise NotImplementedError(
            "DenseLevelFType does not support level_lower_increment."
        )

    def level_lower_unwrap(self, ctx, obj, pos):
        raise NotImplementedError(
            "DenseLevelFType does not support level_lower_unwrap."
        )

    def level_unfurl(
        self,
        ctx: AssemblyContext,
        fiber: ntn.Fiber,
        ext: SymbolicExtent,
        mode,
        proto,
        pos: asm.AssemblyExpression,
    ):
        match mode, proto:
            case ntn.Read(), ntn.DefaultProtocol() | ntn.Follow():
                pass
            case ntn.Update(), ntn.DefaultProtocol() | ntn.Extrude() | ntn.Laminate():
                pass
            case _:
                raise NotImplementedError(
                    f"DenseLevelFType does not support the {proto} protocol."
                )
        tns = fiber
        level = tns.lvl
        lvl = ctx(level)

        def child_accessor(ctx: LoopletContext, idx: ntn.Variable):
            if idx.type_ is None:
                raise TypeError(f"Expected loop variable type for {idx.name}")
            pos_2 = asm.Variable(
                ctx.freshen(idx, f"_pos_{self.ndim - 1}"), self.position_type
            )
            child_pos = asm.Call(
                asm.Literal(ffuncs.add),
                (
                    asm.Call(
                        asm.Literal(ffuncs.mul),
                        (pos, asm.GetAttr(lvl, asm.Literal("dimension"))),
                    ),
                    ctx.ctx(idx),
                ),
            )
            if child_pos.result_type != self.position_type:
                child_pos = asm.Call(
                    asm.Literal(ffuncs.astype(self.position_type)), (child_pos,)
                )
            ctx.exec(asm.Assign(pos_2, child_pos))
            return lplt.Run(
                replace(
                    tns,
                    lvl=ntn.Child(level),
                    pos=ntn.Value(pos_2, self.position_type),
                    idxs=(*tns.idxs, idx),
                )
            )

        return lplt.Lookup(child_accessor)

    def from_fields(self, lvl, dimension, stride) -> DenseLevel:
        return DenseLevel(lvl=lvl, dimension=dimension)

buffer_factory property

Returns the ftype of the buffer used for the fibers.

element_type property

Returns the type of elements stored in the fibers.

position_type property

Returns the type of positions within the levels.

shape_type property

Returns the type of the shape of the fibers.

__call__(val)

Convert a level to this dense level type.

Parameters:

Name Type Description Default
val Any

A value to convert to this type.

required

Returns: A DenseLevel instance of this type.

Source code in src/finch/tensor/level/dense_level.py
def __call__(self, val: Any) -> DenseLevel:
    """
    Convert a level to this dense level type.

    Args:
        val: A value to convert to this type.
    Returns:
        A DenseLevel instance of this type.
    """
    raise NotImplementedError(
        f"Level conversion not yet implemented for {type(self).__name__}"
    )

construct(shape, *, pos)

Creates an instance of DenseLevel with the given ftype.

Parameters:

Name Type Description Default
shape tuple[Any, ...]

The shape to be used for the level. (mandatory)

required

Returns: An instance of DenseLevel.

Source code in src/finch/tensor/level/dense_level.py
def construct(self, shape: tuple[Any, ...], *, pos: int) -> DenseLevel:
    """
    Creates an instance of DenseLevel with the given ftype.

    Args:
        shape: The shape to be used for the level. (mandatory)
    Returns:
        An instance of DenseLevel.
    """
    dimension = self.dimension_type(shape[0])
    lvl = self.lvl_t.construct(
        shape=shape[1:],
        pos=int(pos) * int(dimension),
    )
    return DenseLevel(lvl, dimension)

ElementLevel dataclass

Bases: Level

A class representing the leaf level of Finch tensors.

Source code in src/finch/tensor/level/element_level.py
@dataclass
class ElementLevel(Level):
    """
    A class representing the leaf level of Finch tensors.
    """

    _format: ElementLevelFType = field(repr=False)
    _val: Any | None = None
    # The fill belongs to the level, so that a dynamic fill may change without
    # changing the format. Only a static fill can default to that of the format.
    _fill: Any = None

    def __post_init__(self):
        if self._val is None:
            self._val = self._format.buffer_type(len=0)
        if self._fill is None:
            fill_type = self._format.fill_type
            assert fill_type is not None
            self._fill = resolve_fill(fill_type).value

    @property
    def shape(self) -> tuple:
        return ()

    @property
    def ftype(self) -> ElementLevelFType:
        return self._format

    def with_fill(self, fill_value: AbstractFill) -> "ElementLevel":
        return replace(
            self,
            _format=self._format.with_fill(fill_value.ftype),
            _fill=fill_value.value,
        )

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

    @property
    def fill_value(self) -> Any:
        return self._fill

    def __str__(self):
        return f"ElementLevel(val={self._val})"

ElementLevelFType dataclass

Bases: LevelFType, MutableStructFType

Source code in src/finch/tensor/level/element_level.py
@dataclass(unsafe_hash=True)
class ElementLevelFType(LevelFType, MutableStructFType):
    # None only as constructor input, __post_init__ always resolves these.
    fill_type: AbstractFillFType | None = None  # ty: ignore[invalid-property-type-override]
    element_type: FType | None = None  # ty: ignore[invalid-property-type-override]
    position_type: FType | None = None
    buffer_factory: Any = NumpyBufferFType
    buffer_type: Any = None

    @property
    def struct_name(self):
        return "ElementLevelFType"

    @property
    def struct_fields(self):
        fields = [
            ("val", self.buffer_type),
        ]
        if is_dynamic(self.fill_type):
            # The fill value is bound at call time through a struct field.
            fields.append(("fill", self.element_type))
        return fields

    def struct_getattr(self, obj, attr):
        # A dynamic fill is bound at call time through the "fill" field.
        if attr == "fill":
            return obj.fill_value
        return getattr(obj, attr)

    def struct_setattr(self, obj, attr, value):
        match attr:
            case "val":
                obj._val = value
            case "fill":
                obj._fill = value

    def level_cost(self, fields, stats, stats_factory, num_pos, lvl) -> float:
        # no inner level
        # cost = num_pos * bytes per value
        assert self.element_type is not None
        val_size = np_dtype(self.element_type).itemsize
        return num_pos * val_size

    def __post_init__(self):
        # Ensure element_type is an FType
        if self.element_type is None:
            assert self.fill_type is not None, (
                "Must provide either element_type or fill_type."
            )
            self.element_type = self.fill_type.element_type
        assert isinstance(self.element_type, FType), (
            "element_type must be an instance of FType"
        )
        if self.buffer_type is None:
            self.buffer_type = self.buffer_factory(self.element_type)
        if self.position_type is None:
            self.position_type = ftype(np.intp)
        self.position_type = ftype(self.position_type)
        self.element_type = self.buffer_type.element_type
        # The fill defaults to a static zero, and takes the buffer's element type.
        fill_type = self.fill_type
        if fill_type is None:
            fill_type = StaticFillFType(self.element_type(0))
        self.fill_type = fill_type.astype(self.element_type)

    def construct(self, shape: tuple[Any, ...], *, pos: int) -> "ElementLevel":
        """
        Creates an instance of ElementLevel with the given ftype.

        Args:
            shape: Should be always `()`, used for validation.
        Returns:
            An instance of ElementLevel.
        """
        if len(shape) != 0:
            raise ValueError("ElementLevelFType must be called with an empty shape.")
        val = self.buffer_type(len=pos)
        return ElementLevel(self, val)

    def __call__(self, val: Any) -> "ElementLevel":
        """
        Convert a level to this element level type.

        Args:
            val: A value to convert to this type.
        Returns:
            An ElementLevel instance of this type.
        """
        raise NotImplementedError(
            f"Level conversion not yet implemented for {type(self).__name__}"
        )

    def __str__(self):
        match self.fill_type:
            case StaticFillFType() as fill_type:
                return f"ElementLevelFType(fv={fill_type.value})"
            case fill_type:
                return f"ElementLevelFType(fv={fill_type})"

    @property
    def ndim(self):
        return 0

    @property
    def shape_type(self):
        return ()

    @property
    def lvl_t(self):
        raise Exception("ElementLevelFType is the leaf level.")

    def level_format_properties(self, n):
        return []

    def with_fill(self, fill_type: Any) -> "ElementLevelFType":
        return ElementLevelFType(
            fill_type=fill_type,
            element_type=self.element_type,
            position_type=self.position_type,
            buffer_factory=self.buffer_factory,
            buffer_type=self.buffer_type,
        )

    def lower_fill(self, lvl_expr):
        return asm.GetAttr(lvl_expr, asm.Literal("fill"))

    def from_fields(self, val=None, fill=None) -> "ElementLevel":
        # Wrap numpy arrays in NumpyBuffer and flatten, similar to BufferizedNDArray
        if val is not None and isinstance(val, np.ndarray):
            from finch.codegen import NumpyBuffer

            val = NumpyBuffer(np.asarray(val).reshape(-1, copy=False))
        return ElementLevel(_format=self, _val=val, _fill=fill)

    def level_lower_declare(self, ctx, tns, init, op, shape, pos):
        if is_dynamic(self.fill_type):
            # The init becomes the fill of the tensor.
            elem_t = self.fill_type.element_type
            if init.result_type != elem_t:
                init = asm.Call(asm.Literal(ffuncs.astype(elem_t)), (init,))
            ctx.exec(asm.SetAttr(tns, asm.Literal("fill"), init))
            init = asm.GetAttr(tns, asm.Literal("fill"))
        buf = asm.GetAttr(tns, asm.Literal("val"))
        if pos.result_type != self.buffer_type.length_type:
            pos = asm.Call(
                asm.Literal(ffuncs.astype(self.buffer_type.length_type)), (pos,)
            )
        ctx.exec(asm.Resize(buf, pos))
        i_var = asm.Variable("i", self.buffer_type.length_type)
        body = asm.Store(buf, i_var, init)
        ctx.exec(asm.ForLoop(i_var, asm.Literal(np.intp(0)), asm.Length(buf), body))

    def level_lower_unwrap(self, ctx, obj, pos):
        buf = asm.GetAttr(ctx(obj.lvl), asm.Literal("val"))
        if pos.result_type != self.buffer_type.length_type:
            pos = asm.Call(
                asm.Literal(ffuncs.astype(self.buffer_type.length_type)), (pos,)
            )
        return asm.Load(buf, pos)

    def level_lower_increment(
        self,
        ctx: AssemblyContext,
        obj,
        op: ntn.Literal,
        val: ntn.NotationExpression,
        pos: asm.AssemblyExpression,
    ):
        buf = asm.GetAttr(ctx(obj.lvl), asm.Literal("val"))
        if pos.result_type != self.buffer_type.length_type:
            pos = asm.Call(
                asm.Literal(ffuncs.astype(self.buffer_type.length_type)), (pos,)
            )
        pos_e, op_e, val_e = pos, ctx(op), ctx(val)
        ctx.exec(
            asm.Store(
                buf,
                pos_e,
                asm.Call(op_e, (asm.Load(buf, pos_e), val_e)),
            )
        )
        match obj:
            case ntn.HollowFiber(dirty=dirty):
                fill = (
                    asm.Literal(self.fill_type.value)
                    if is_static(self.fill_type)
                    else self.lower_fill(ctx(obj.lvl))
                )
                ctx.exec(
                    asm.If(
                        asm.Call(
                            asm.Literal(ffuncs.not_),
                            (
                                asm.Call(
                                    asm.Literal(ffuncs.same),
                                    (asm.Load(buf, pos_e), fill),
                                ),
                            ),
                        ),
                        asm.Assign(ctx(dirty), asm.Literal(True)),
                    )
                )

    def level_lower_assemble(self, ctx, lvl, start, stop):
        buf = asm.GetAttr(lvl, asm.Literal("val"))
        p_t = self.buffer_type.length_type
        to_size = asm.Literal(ffuncs.astype(p_t))
        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)
        length = asm.Length(buf)
        fill = (
            asm.Literal(self.fill_type.value)
            if is_static(self.fill_type)
            else self.lower_fill(lvl)
        )
        expr = """finch
        p_start = to_size(start)
        p_stop = to_size(stop)
        if (length < p_stop)
            resize(buf, p_stop)
        end
        for (p in p_start:p_stop)
            buf[p] = fill
        end
        """
        ctx.exec(parse_assembly(expr, locals(), position_type=p_t))

    def level_lower_freeze(self, ctx, lvl, op, pos):
        buf = asm.GetAttr(lvl, asm.Literal("val"))
        size = asm.Call(
            asm.Literal(ffuncs.astype(self.buffer_type.length_type)), (pos,)
        )
        length = asm.Length(buf)
        expr = """finch
        if (length != size)
            resize(buf, size)
        end
        """
        ctx.exec(parse_assembly(expr, locals()))

    def level_lower_thaw(self, ctx, lvl, op, pos):
        pass

    def level_lower_dim(self, ctx, obj, r):
        raise NotImplementedError("ElementLevelFType does not support level_lower_dim.")

    def level_unfurl(self, ctx, lvl, ext, mode, proto, pos):
        raise NotImplementedError("ElementLevelFType does not support level_unfurl.")

__call__(val)

Convert a level to this element level type.

Parameters:

Name Type Description Default
val Any

A value to convert to this type.

required

Returns: An ElementLevel instance of this type.

Source code in src/finch/tensor/level/element_level.py
def __call__(self, val: Any) -> "ElementLevel":
    """
    Convert a level to this element level type.

    Args:
        val: A value to convert to this type.
    Returns:
        An ElementLevel instance of this type.
    """
    raise NotImplementedError(
        f"Level conversion not yet implemented for {type(self).__name__}"
    )

construct(shape, *, pos)

Creates an instance of ElementLevel with the given ftype.

Parameters:

Name Type Description Default
shape tuple[Any, ...]

Should be always (), used for validation.

required

Returns: An instance of ElementLevel.

Source code in src/finch/tensor/level/element_level.py
def construct(self, shape: tuple[Any, ...], *, pos: int) -> "ElementLevel":
    """
    Creates an instance of ElementLevel with the given ftype.

    Args:
        shape: Should be always `()`, used for validation.
    Returns:
        An instance of ElementLevel.
    """
    if len(shape) != 0:
        raise ValueError("ElementLevelFType must be called with an empty shape.")
    val = self.buffer_type(len=pos)
    return ElementLevel(self, val)

Level dataclass

Bases: FTyped[LT], ABC

An abstract base class representing a fiber allocator that manages fibers in a tensor.

Source code in src/finch/tensor/level/level.py
@dataclass
class Level(FTyped[LT], ABC):
    """
    An abstract base class representing a fiber allocator that manages fibers in
    a tensor.
    """

    @property
    @abstractmethod
    def shape(self) -> tuple:
        """
        Shape of the fibers in the structure.
        """
        ...

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

    @property
    def ndim(self):
        return self.ftype.ndim

    @property
    def fill_value(self):
        """The fill value itself; see `self.ftype.fill_type` for
        dynamic vs static. A level with a dynamic fill holds its own value."""
        fill_type = self.ftype.fill_type
        if is_static(fill_type):
            return fill_type.value
        raise NotImplementedError(
            f"{type(self).__name__} must hold its dynamic fill value"
        )

    @property
    def element_type(self):
        return self.ftype.element_type

    @property
    def shape_type(self):
        return self.ftype.shape_type

    @property
    def position_type(self):
        return self.ftype.position_type

    @property
    def buffer_factory(self):
        return self.ftype.buffer_factory

    @property
    def buffer_type(self):
        return self.ftype.buffer_type

    @abstractmethod
    def with_fill(self, fill_value: AbstractFill) -> Self: ...

fill_value property

The fill value itself; see self.ftype.fill_type for dynamic vs static. A level with a dynamic fill holds its own value.

shape abstractmethod property

Shape of the fibers in the structure.

LevelFType dataclass

Bases: FType, ABC

An abstract base class representing the ftype of levels.

Source code in src/finch/tensor/level/level.py
@dataclass
class LevelFType(FType, ABC):
    """
    An abstract base class representing the ftype of levels.
    """

    def level_get_child_type(self, attr: str) -> LevelFType:
        match self:
            case StructFType() if self.struct_hasattr(attr):
                child = self.struct_attrtype(attr)
                match child:
                    case LevelFType():
                        return child
        raise TypeError(f"{self} does not support child {attr!r}")

    def level_get_child(
        self, obj: asm.AssemblyExpression, attr: str
    ) -> asm.AssemblyExpression:
        self.level_get_child_type(attr)
        return asm.GetAttr(obj, asm.Literal(attr))

    def with_fill(self, fill_type: AbstractFillFType) -> LevelFType:
        """Rebuild this level ftype with the leaf fill replaced."""
        # Level ftypes are dataclasses with a `_lvl_t` child field; non-
        # dataclass levels must override.
        return replace(self, _lvl_t=self.lvl_t.with_fill(fill_type))  # type: ignore[type-var]

    def lower_fill(self, lvl_expr):
        """Assembly expression reading the runtime fill from the leaf level's
        fill field, descending from this level's struct expression."""
        return self.lvl_t.lower_fill(asm.GetAttr(lvl_expr, asm.Literal("lvl")))

    @property
    @abstractmethod
    def ndim(self):
        """
        Number of dimensions of the fibers in the structure.
        """
        ...

    @property
    @abstractmethod
    def fill_type(self) -> AbstractFillFType:
        """
        Fill of the fibers, and whether kernels may specialize on its value.
        """
        ...

    @property
    @abstractmethod
    def element_type(self) -> FType:
        """
        Type of elements stored in the fibers.
        """
        ...

    @property
    @abstractmethod
    def shape_type(self):
        """
        Tuple of types of the dimensions in the shape.
        """
        ...

    @property
    @abstractmethod
    def position_type(self):
        """
        Type of positions within the levels.
        """
        ...

    @property
    @abstractmethod
    def buffer_factory(self):
        """
        Function to create default buffers for the fibers.
        """
        ...

    @property
    @abstractmethod
    def buffer_type(self): ...

    @property
    @abstractmethod
    def lvl_t(self):
        """
        Get the nested level.
        """
        ...

    @abstractmethod
    def level_cost(self, fields, stats, stats_factory, num_pos, lvl):
        """
        Defines cost for a level
        """
        ...

    @abstractmethod
    def level_unfurl(self, ctx, lvl, ext, mode, proto, pos):
        """
        Emit code to unfurl the fiber at position `pos` in the level.
        """
        ...

    @abstractmethod
    def level_lower_freeze(self, ctx, lvl, op, pos):
        """
        Emit code to freeze `pos` previously assembled positions in the level.
        """
        ...

    def level_lower_assemble(self, ctx, lvl, start, stop):
        """Assemble parent positions in the half-open range [start, stop)."""
        raise NotImplementedError(
            f"Assembly is not implemented for {type(self).__name__}"
        )

    @abstractmethod
    def level_lower_thaw(self, ctx, lvl, op, pos):
        """
        Emit code to thaw `pos` previously assembled positions in the level.
        """
        ...

    @abstractmethod
    def level_lower_unwrap(self, ctx, obj, pos):
        """
        Emit code to return the unwrapped scalar at position `pos` in the level.
        """
        ...

    @abstractmethod
    def level_lower_increment(self, ctx, obj, op, val, pos):
        """
        Emit code to increment position `pos` in the level.
        """
        ...

    @abstractmethod
    def level_lower_declare(self, ctx, tns, init, op, shape, pos):
        """
        Emit code to lower a declare of `pos` previously assembled positions in
        the level.
        """
        ...

    @abstractmethod
    def level_lower_dim(self, ctx, obj, r):
        """
        Emit code to return the size of dimension `r` of the subtensors in the level.
        """
        ...

    @abstractmethod
    def construct(self, shape: tuple[Any, ...], *, pos: int) -> Level:
        """
        Construct a level instance with the given shape.
        """
        ...

    @abstractmethod
    def level_format_properties(self, n: int) -> list[FormatProperty]:
        """
        Return the format properties contributed by this level type and children.

        ``n`` is the outer dimension index represented by this level. Nested
        levels use increasing indices, so the returned properties can describe how
        this dimension constrains dimensions further inside the fiber tree.
        """
        ...

    def level_default_protocols(self) -> tuple[ntn.Protocol, ...]:
        """
        The protocol which a loop reads each dimension of the fibers in the
        structure with, when the access gives none.
        """
        return (ntn.DefaultProtocol(),) * self.ndim

    def level_protocols(self) -> tuple[tuple[ntn.Protocol, ...], ...]:
        """
        The protocols which a loop can read or write each dimension of the
        fibers in the structure with.
        """
        return ((ntn.DefaultProtocol(),),) * self.ndim

buffer_factory abstractmethod property

Function to create default buffers for the fibers.

element_type abstractmethod property

Type of elements stored in the fibers.

fill_type abstractmethod property

Fill of the fibers, and whether kernels may specialize on its value.

lvl_t abstractmethod property

Get the nested level.

ndim abstractmethod property

Number of dimensions of the fibers in the structure.

position_type abstractmethod property

Type of positions within the levels.

shape_type abstractmethod property

Tuple of types of the dimensions in the shape.

construct(shape, *, pos) abstractmethod

Construct a level instance with the given shape.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def construct(self, shape: tuple[Any, ...], *, pos: int) -> Level:
    """
    Construct a level instance with the given shape.
    """
    ...

level_cost(fields, stats, stats_factory, num_pos, lvl) abstractmethod

Defines cost for a level

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_cost(self, fields, stats, stats_factory, num_pos, lvl):
    """
    Defines cost for a level
    """
    ...

level_default_protocols()

The protocol which a loop reads each dimension of the fibers in the structure with, when the access gives none.

Source code in src/finch/tensor/level/level.py
def level_default_protocols(self) -> tuple[ntn.Protocol, ...]:
    """
    The protocol which a loop reads each dimension of the fibers in the
    structure with, when the access gives none.
    """
    return (ntn.DefaultProtocol(),) * self.ndim

level_format_properties(n) abstractmethod

Return the format properties contributed by this level type and children.

n is the outer dimension index represented by this level. Nested levels use increasing indices, so the returned properties can describe how this dimension constrains dimensions further inside the fiber tree.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_format_properties(self, n: int) -> list[FormatProperty]:
    """
    Return the format properties contributed by this level type and children.

    ``n`` is the outer dimension index represented by this level. Nested
    levels use increasing indices, so the returned properties can describe how
    this dimension constrains dimensions further inside the fiber tree.
    """
    ...

level_lower_assemble(ctx, lvl, start, stop)

Assemble parent positions in the half-open range [start, stop).

Source code in src/finch/tensor/level/level.py
def level_lower_assemble(self, ctx, lvl, start, stop):
    """Assemble parent positions in the half-open range [start, stop)."""
    raise NotImplementedError(
        f"Assembly is not implemented for {type(self).__name__}"
    )

level_lower_declare(ctx, tns, init, op, shape, pos) abstractmethod

Emit code to lower a declare of pos previously assembled positions in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_declare(self, ctx, tns, init, op, shape, pos):
    """
    Emit code to lower a declare of `pos` previously assembled positions in
    the level.
    """
    ...

level_lower_dim(ctx, obj, r) abstractmethod

Emit code to return the size of dimension r of the subtensors in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_dim(self, ctx, obj, r):
    """
    Emit code to return the size of dimension `r` of the subtensors in the level.
    """
    ...

level_lower_freeze(ctx, lvl, op, pos) abstractmethod

Emit code to freeze pos previously assembled positions in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_freeze(self, ctx, lvl, op, pos):
    """
    Emit code to freeze `pos` previously assembled positions in the level.
    """
    ...

level_lower_increment(ctx, obj, op, val, pos) abstractmethod

Emit code to increment position pos in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_increment(self, ctx, obj, op, val, pos):
    """
    Emit code to increment position `pos` in the level.
    """
    ...

level_lower_thaw(ctx, lvl, op, pos) abstractmethod

Emit code to thaw pos previously assembled positions in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_thaw(self, ctx, lvl, op, pos):
    """
    Emit code to thaw `pos` previously assembled positions in the level.
    """
    ...

level_lower_unwrap(ctx, obj, pos) abstractmethod

Emit code to return the unwrapped scalar at position pos in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_lower_unwrap(self, ctx, obj, pos):
    """
    Emit code to return the unwrapped scalar at position `pos` in the level.
    """
    ...

level_protocols()

The protocols which a loop can read or write each dimension of the fibers in the structure with.

Source code in src/finch/tensor/level/level.py
def level_protocols(self) -> tuple[tuple[ntn.Protocol, ...], ...]:
    """
    The protocols which a loop can read or write each dimension of the
    fibers in the structure with.
    """
    return ((ntn.DefaultProtocol(),),) * self.ndim

level_unfurl(ctx, lvl, ext, mode, proto, pos) abstractmethod

Emit code to unfurl the fiber at position pos in the level.

Source code in src/finch/tensor/level/level.py
@abstractmethod
def level_unfurl(self, ctx, lvl, ext, mode, proto, pos):
    """
    Emit code to unfurl the fiber at position `pos` in the level.
    """
    ...

lower_fill(lvl_expr)

Assembly expression reading the runtime fill from the leaf level's fill field, descending from this level's struct expression.

Source code in src/finch/tensor/level/level.py
def lower_fill(self, lvl_expr):
    """Assembly expression reading the runtime fill from the leaf level's
    fill field, descending from this level's struct expression."""
    return self.lvl_t.lower_fill(asm.GetAttr(lvl_expr, asm.Literal("lvl")))

with_fill(fill_type)

Rebuild this level ftype with the leaf fill replaced.

Source code in src/finch/tensor/level/level.py
def with_fill(self, fill_type: AbstractFillFType) -> LevelFType:
    """Rebuild this level ftype with the leaf fill replaced."""
    # Level ftypes are dataclasses with a `_lvl_t` child field; non-
    # dataclass levels must override.
    return replace(self, _lvl_t=self.lvl_t.with_fill(fill_type))  # type: ignore[type-var]

SparseByteMapLevel dataclass

Bases: SingleDimensionLevel

Byte map with sorted, zero-based packed child positions in srt.

Each child position is parent * dimension + index; ptr groups these positions by parent, and tbl marks the stored children. An occupied parent p owns srt[ptr[p]:ptr[p + 1]], while an unoccupied parent's ptr[p] is -1. While assembling, srt[:qos_fill] lists the stored children in insertion order, and qos_stop is its capacity.

Source code in src/finch/tensor/level/sparse_bytemap_level.py
@dataclass
class SparseByteMapLevel(SingleDimensionLevel):
    """Byte map with sorted, zero-based packed child positions in ``srt``.

    Each child position is ``parent * dimension + index``; ``ptr`` groups
    these positions by parent, and ``tbl`` marks the stored children. An
    occupied parent ``p`` owns ``srt[ptr[p]:ptr[p + 1]]``, while an unoccupied
    parent's ``ptr[p]`` is -1. While assembling, ``srt[:qos_fill]`` lists the
    stored children in insertion order, and ``qos_stop`` is its capacity.
    """

    lvl: Level
    dimension: np.integer
    ptr: Any | None = None
    tbl: Any | None = None
    srt: Any | None = None
    qos_fill: Any = None
    qos_stop: Any = None

    def __post_init__(self) -> None:
        if self.ptr is None:
            self.ptr = self.lvl.buffer_factory(self.lvl.position_type)(1)
        if self.tbl is None:
            self.tbl = self.lvl.buffer_factory(ftypes.bool_)(0)
        if self.srt is None:
            self.srt = self.lvl.buffer_factory(self.lvl.position_type)(0)
        p_t = self.lvl.position_type
        self.qos_fill = p_t(
            self.srt.length() if self.qos_fill is None else self.qos_fill
        )
        self.qos_stop = p_t(
            self.srt.length() if self.qos_stop is None else self.qos_stop
        )

    @property
    def shape(self) -> tuple:
        return (self.dimension, *self.lvl.shape)

    @property
    def ftype(self) -> SparseByteMapLevelFType:
        return SparseByteMapLevelFType(self.lvl.ftype, ftype(self.dimension))  # type: ignore[abstract]

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

    def __str__(self) -> str:
        return f"SparseByteMapLevel(lvl={self.lvl}, dim={self.dimension})"

SparseHashLevel dataclass

Bases: SingleDimensionLevel

Hash slots map to child positions with separate (parent, index, state) keys.

Positions and indices are zero-based. Key states are free (0), pending writer counts (1–127), or retained (128). The odd position multiplier must be preserved with the table's buffers. Frozen, perm[ptr[p]:ptr[p + 1]] lists parent p's child positions by index. While assembling, key[:qos_stop] holds the child records, and free_head starts the chain of free child positions, linked through their parent fields.

Source code in src/finch/tensor/level/sparse_hash_level.py
@dataclass(init=False)
class SparseHashLevel(SingleDimensionLevel):
    """Hash slots map to child positions with separate ``(parent, index, state)`` keys.

    Positions and indices are zero-based. Key states are free (0), pending
    writer counts (1–127), or retained (128). The odd position multiplier must
    be preserved with the table's buffers. Frozen, ``perm[ptr[p]:ptr[p + 1]]``
    lists parent ``p``'s child positions by index. While assembling,
    ``key[:qos_stop]`` holds the child records, and ``free_head`` starts the
    chain of free child positions, linked through their parent fields.
    """

    lvl: Level
    dimension: np.integer
    ptr: Any | None
    tbl_ctrl: Any | None
    tbl: Any | None
    key: Any | None
    perm: Any | None
    tbl_count: Any | None
    subtables: np.intp
    pos_multiplier: np.uintp
    qos_stop: Any
    free_head: Any
    frozen: bool

    def __init__(
        self,
        lvl: Level,
        dimension: np.integer,
        ptr: Any | None = None,
        tbl_ctrl: Any | None = None,
        tbl: Any | None = None,
        key: Any | None = None,
        perm: Any | None = None,
        tbl_count: Any | None = None,
        *,
        subtables: int = 1,
        pos_multiplier: int | np.integer = _POS_MULTIPLIER,
        qos_stop: Any = None,
        free_head: Any = _NO_POS,
        frozen: bool = True,
    ) -> None:
        self.lvl = lvl
        self.dimension = dimension
        self.ptr = ptr
        self.tbl_ctrl = tbl_ctrl
        self.tbl = tbl
        self.key = key
        self.perm = perm
        self.tbl_count = tbl_count
        if not isinstance(subtables, (int, np.integer)):
            raise ValueError(
                "SparseHashLevel subtables must be a positive power of two"
            )
        self.subtables = np.intp(subtables)
        if (
            not isinstance(pos_multiplier, (int, np.integer))
            or not int(pos_multiplier) & 1
        ):
            raise ValueError(
                "SparseHashLevel position multiplier must be an odd integer"
            )
        self.pos_multiplier = np.uintp(pos_multiplier)
        self.qos_stop = qos_stop
        self.free_head = free_head
        self.frozen = bool(frozen)
        self.__post_init__()

    def __post_init__(self) -> None:
        subtables = int(self.subtables)
        if subtables < 1 or subtables & (subtables - 1) != 0:
            raise ValueError(
                "SparseHashLevel subtables must be a positive power of two"
            )
        if self.ptr is None:
            self.ptr = self.lvl.buffer_factory(self.lvl.position_type)(1)
        if self.tbl_ctrl is None:
            self.tbl_ctrl = self.lvl.buffer_factory(ftypes.uint8)(0)
        if self.tbl is None:
            self.tbl = self.lvl.buffer_factory(self.lvl.position_type)(0)
        if self.key is None:
            key_entry_type = TupleFType.from_tuple(
                (self.lvl.position_type, ftype(self.dimension), ftypes.uint8)
            )
            self.key = self.lvl.buffer_factory(key_entry_type)(0)
        if self.perm is None:
            self.perm = self.lvl.buffer_factory(self.lvl.position_type)(0)
        if self.tbl_count is None:
            self.tbl_count = self.lvl.buffer_factory(ftypes.intp)(self.subtables)
        p_t = self.lvl.position_type
        self.qos_stop = p_t(
            self.key.length() if self.qos_stop is None else self.qos_stop
        )
        self.free_head = p_t(self.free_head)

    @property
    def shape(self) -> tuple:
        return (self.dimension, *self.lvl.shape)

    @property
    def ftype(self) -> SparseHashLevelFType:
        return SparseHashLevelFType(
            self.lvl.ftype,
            ftype(self.dimension),
        )  # type: ignore[abstract]

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

    def __str__(self) -> str:
        return f"SparseHashLevel(lvl={self.lvl}, dim={self.dimension})"

SparseHashLevelFType dataclass

Bases: SingleDimensionLevelFType, MutableStructFType

Source code in src/finch/tensor/level/sparse_hash_level.py
 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
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
@dataclass(unsafe_hash=True)
class SparseHashLevelFType(SingleDimensionLevelFType, MutableStructFType):
    _lvl_t: LevelFType
    dimension_type: ftypes.FDTypeInteger = ftypes.intp

    def __post_init__(self) -> None:
        if not isinstance(self._lvl_t, LevelFType):
            raise TypeError("SparseHashLevelFType lvl_t must be a level type")
        self.dimension_type = ftype(self.dimension_type)

    @property
    def struct_name(self) -> str:
        return "SparseHashLevelFType"

    @property
    def struct_fields(self) -> list[tuple[str, FType]]:
        return [
            ("lvl", self.lvl_t),
            ("dimension", self.dimension_type),
            ("subtables", ftypes.intp),
            ("pos_multiplier", ftype(np.uintp)),
            ("ptr", self.ptr_type),
            ("tbl_ctrl", self.tbl_ctrl_type),
            ("tbl", self.tbl_type),
            ("key", self.key_type),
            ("perm", self.perm_type),
            ("tbl_count", self.tbl_count_type),
            ("qos_stop", self.position_type),
            ("free_head", self.position_type),
            ("frozen", ftypes.bool_),
        ]

    def __str__(self) -> str:
        return f"SparseHashLevelFType({self.lvl_t})"

    @property
    def ndim(self) -> int:
        return 1 + self.lvl_t.ndim

    @property
    def fill_type(self):
        return self.lvl_t.fill_type

    @property
    def element_type(self) -> FType:
        return self.lvl_t.element_type

    @property
    def shape_type(self) -> tuple[FType, ...]:
        return (self.dimension_type, *self.lvl_t.shape_type)

    @property
    def position_type(self) -> FType:
        return self.lvl_t.position_type

    @property
    def buffer_type(self) -> FType:
        return self.lvl_t.buffer_type

    @property
    def buffer_factory(self) -> Any:
        return self.lvl_t.buffer_factory

    @property
    def ptr_type(self) -> BufferFType:
        return self.buffer_factory(self.position_type)

    @property
    def tbl_ctrl_type(self) -> FType:
        return self.buffer_factory(ftypes.uint8)

    @property
    def key_entry_type(self) -> TupleFType:
        return TupleFType.from_tuple(
            (self.position_type, self.dimension_type, ftypes.uint8)
        )

    @property
    def tbl_type(self) -> FType:
        return self.buffer_factory(self.position_type)

    @property
    def key_type(self) -> FType:
        return self.buffer_factory(self.key_entry_type)

    @property
    def perm_type(self) -> FType:
        return self.buffer_factory(self.position_type)

    @property
    def tbl_count_type(self) -> FType:
        return self.buffer_factory(ftypes.intp)

    @property
    def lvl_t(self) -> LevelFType:
        return self._lvl_t

    def level_cost(self, fields, stats, stats_factory, num_pos, lvl) -> float:
        pos_size = ftypes.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 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(), ntn.Laminate()),
            *self.lvl_t.level_protocols(),
        )

    def construct(self, shape: tuple[Any, ...], *, pos: int) -> "SparseHashLevel":
        lvl = self.lvl_t.construct(shape=shape[1:], pos=0)
        return SparseHashLevel(
            lvl,
            self.dimension_type(shape[0]),
            self.ptr_type(int(pos) + 1),
            self.tbl_ctrl_type(0),
            self.tbl_type(0),
            self.key_type(0),
            self.perm_type(0),
            self.tbl_count_type(1),
        )

    def __call__(self, val: Any) -> "SparseHashLevel":
        raise NotImplementedError(
            f"Level conversion not yet implemented for {type(self).__name__}"
        )

    def level_lower_dim(self, ctx, obj, r):
        if r == 0:
            return asm.GetAttr(obj, asm.Literal("dimension"))
        return self.lvl_t.level_lower_dim(
            ctx, asm.GetAttr(obj, asm.Literal("lvl")), r - 1
        )

    def level_lower_declare(self, ctx, tns, init, op, shape, pos):
        hl = _SparseHashLowering(self, ctx, tns)
        r, h, q, k = hl.var("r"), hl.var("h"), hl.var("q"), hl.key_var()
        hashing, x = hl.record_hash(k)
        parts, base, off, mask, _ = hl.slot_parts(x)
        # Clear the table without releasing its capacity. Visiting an entry
        # costs a hash and a random probe, while filling the control array costs
        # well under a nanosecond per slot, so only visit the frozen entries at
        # very low occupancy. Slots don't move while clearing, so probes continue
        # past emptied slots.
        visit = (
            *hl.parse(
                """
                q = to_len(perm[r])
                k = key[q]
                """,
                q=q,
                r=r,
                k=k,
            ),
            *hashing,
            *parts,
            *hl.parse(
                """
                h = add(base, off)
                while (bor(tbl_ctrl[h] == EMPTY, tbl[h] != to_pos(q)))
                    off = band(add(off, 1), mask)
                    h = add(base, off)
                end
                tbl_ctrl[h] = EMPTY
                """,
                **locals(),
            ),
        )
        ctx.exec(
            asm.IfElse(
                hl.expr("band(lvl.frozen, n_perm <= fld(n_ctrl, 4096))"),
                asm.ForLoop(
                    r, asm.Literal(np.intp(0)), hl.bindings["n_perm"], asm.Block(visit)
                ),
                asm.Block(
                    hl.parse(
                        """
                        for (h in 0:n_ctrl)
                            tbl_ctrl[h] = EMPTY
                        end
                        """,
                        h=h,
                    )
                ),
            )
        )
        exec_block(
            ctx,
            asm.Block(
                hl.parse(
                    """
                    resize(key, 0)
                    resize(perm, 0)
                    for (b in 0:n_count)
                        tbl_count[b] = 0
                    end
                    lvl.qos_stop = zero_pos
                    lvl.free_head = no_pos
                    lvl.frozen = false
                    """,
                    b=hl.var("b"),
                )
            ),
        )
        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(tns, asm.Literal("dimension"), dimension))
        return self.lvl_t.level_lower_declare(
            ctx,
            asm.GetAttr(tns, asm.Literal("lvl")),
            init,
            op,
            shape[1:],
            asm.Literal(self.position_type(0)),
        )

    def level_lower_assemble(self, ctx, lvl, start, stop):
        pass

    def level_lower_thaw(self, ctx, lvl, op, pos):
        hl = _SparseHashLowering(self, ctx, lvl)
        k = hl.key_var()
        # The free chain is local to a write phase. Rebuild it from the holes
        # of a frozen level; a dense set of child positions needs no scan.
        ctx.exec(
            asm.Block(
                hl.parse(
                    """
                    if (lvl.frozen)
                        lvl.qos_stop = to_pos(n_key)
                        lvl.free_head = no_pos
                        if (n_perm < n_key)
                            for (j in 0:n_key)
                                q = sub(sub(n_key, j), 1)
                                k = key[q]
                                if (k.element_2 == FREE)
                                    key[q] = freed
                                    lvl.free_head = to_pos(q)
                                end
                            end
                        end
                        lvl.frozen = false
                    end
                    """,
                    j=hl.var("j"),
                    q=hl.var("q"),
                    k=k,
                    freed=hl.expr("make_tuple(lvl.free_head, k.element_1, FREE)", k=k),
                )
            )
        )
        return self.lvl_t.level_lower_thaw(
            ctx,
            asm.GetAttr(lvl, asm.Literal("lvl")),
            op,
            asm.GetAttr(lvl, asm.Literal("qos_stop")),
        )

    def level_lower_freeze(self, ctx, lvl, op, pos):
        hl = _SparseHashLowering(self, ctx, lvl)
        pos_stop, qos_stop, n, extent, q, r, p = (
            hl.var(name)
            for name in ("pos_stop", "qos_stop", "n", "extent", "q", "r", "p")
        )
        k = hl.key_var()
        # Collect the live child positions, trimming free tails of key so that
        # an empty hash has an empty child.
        exec_block(
            ctx,
            asm.Block(
                hl.parse(
                    """
                    pos_stop = to_len(pos)
                    qos_stop = to_len(lvl.qos_stop)
                    n = 0
                    extent = 0
                    for (q in 0:qos_stop)
                        k = key[q]
                        if (k.element_2 != FREE)
                            n = add(n, 1)
                            extent = add(q, 1)
                        end
                    end
                    resize(perm, n)
                    r = 0
                    for (q in 0:qos_stop)
                        k = key[q]
                        if (k.element_2 != FREE)
                            perm[r] = to_pos(q)
                            r = add(r, 1)
                        end
                    end
                    """,
                    **locals(),
                )
            ),
        )

        # Order the entries for reading: perm[ptr[p]:ptr[p + 1]] lists parent
        # p's child positions by index.
        def lt(a, b):
            a_p, a_i, b_p, b_i = (
                field
                for q in (a, b)
                for field in hl.key_fields(hl.expr("key[to_len(q)]", q=q))
            )
            return hl.expr(
                "bor(a_p < b_p, band(a_p == b_p, a_i < b_i))",
                a_p=a_p,
                a_i=a_i,
                b_p=b_p,
                b_i=b_i,
            )

        lower_heapsort(ctx, hl.bindings["perm"], n, lt)
        exec_block(
            ctx,
            asm.Block(
                hl.parse(
                    """
                    resize(ptr, add(pos_stop, 1))
                    for (p in 0:pos_stop + 1)
                        ptr[p] = zero_pos
                    end
                    for (r in 0:n)
                        k = key[to_len(perm[r])]
                        p = add(to_len(k.element_0), 1)
                        ptr[p] = add(ptr[p], one_pos)
                    end
                    for (p in 0:pos_stop)
                        ptr[add(p, 1)] = add(ptr[add(p, 1)], ptr[p])
                    end
                    resize(key, extent)
                    """,
                    **locals(),
                )
            ),
        )
        self._lower_count_buckets(ctx, hl, n)
        exec_block(
            ctx,
            asm.Block(
                hl.parse(
                    """
                    lvl.qos_stop = to_pos(extent)
                    lvl.free_head = no_pos
                    lvl.frozen = true
                    """,
                    extent=extent,
                )
            ),
        )
        return self.lvl_t.level_lower_freeze(
            ctx, asm.GetAttr(lvl, asm.Literal("lvl")), op, extent
        )

    def _lower_count_buckets(self, ctx, hl, n):
        """
        Checkpoint the frozen bucket counts every B ranks of perm, as
        `sparse_hash_count_buckets!`: walking perm backward, each block starts
        from the one after it.
        """
        m, jj, block, b, r, r_stop = (
            hl.var(name) for name in ("m", "jj", "block", "b", "r", "r_stop")
        )
        k = hl.key_var()
        hashing, x = hl.record_hash(k)
        count = (
            *hl.parse("k = key[to_len(perm[r])]", k=k, r=r),
            *hashing,
            *hl.parse(
                """
                bucket = add(block, b_x)
                tbl_count[bucket] = add(tbl_count[bucket], 1)
                """,
                bucket=hl.var("bucket"),
                block=block,
                b_x=hl.subtable(x),
            ),
        )
        exec_block(
            ctx,
            asm.Block(
                hl.parse(
                    """
                    m = add(fld(add(n, sub(B, 1)), B), 1)
                    resize(tbl_count, mul(m, B))
                    for (b in 0:B)
                        tbl_count[add(mul(sub(m, 1), B), b)] = 0
                    end
                    """,
                    m=m,
                    n=n,
                    b=hl.var("b"),
                )
            ),
        )
        ctx.exec(
            asm.ForLoop(
                jj,
                asm.Literal(np.intp(0)),
                hl.expr("sub(m, 1)", m=m),
                asm.Block(
                    (
                        *hl.parse(
                            """
                            block = mul(sub(sub(m, 2), jj), B)
                            for (b in 0:B)
                                tbl_count[add(block, b)] = after
                            end
                            r_stop = min(add(block, B), n)
                            """,
                            block=block,
                            m=m,
                            jj=jj,
                            b=b,
                            after=hl.expr(
                                "tbl_count[add(add(block, B), b)]", block=block, b=b
                            ),
                            r_stop=r_stop,
                            n=n,
                        ),
                        asm.ForLoop(r, block, r_stop, asm.Block(count)),
                    )
                ),
            )
        )

    def level_lower_increment(self, ctx, obj, op, val, pos):
        raise NotImplementedError(
            "SparseHashLevelFType does not support level_lower_increment."
        )

    def level_lower_unwrap(self, ctx, obj, pos):
        raise NotImplementedError(
            "SparseHashLevelFType does not support level_lower_unwrap."
        )

    def level_unfurl(
        self, ctx, fiber: ntn.Fiber, ext, mode: ntn.AccessMode, proto, pos
    ):
        match mode:
            case ntn.Update():
                return self.level_unfurl_update(ctx, fiber, ext, mode, proto, pos)
        level = fiber.lvl
        lvl = ctx(level)
        hl = _SparseHashLowering(self, ctx, lvl)
        p_t, d_t = self.position_type, self.dimension_type
        fill = (
            ntn.Value(self.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.level_lower_dim(ctx, lvl, 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, fiber, pos, full)
            case _:
                raise NotImplementedError(
                    f"SparseHashLevelFType does not support the {proto} protocol."
                )
        bindings = {
            "r": hl.var("r"),
            "r_stop": hl.var("r_stop"),
            "i_stop": hl.var("i_stop", d_t),
            "i_last": hl.var("i_last", d_t),
            "k": hl.key_var(),
            "pos": pos,
            "dimension": hl.expr("to_idx(lvl.dimension)"),
            "no_idx": asm.Literal(d_t(_NO_POS)),
        }
        r, i_stop, i_last = bindings["r"], bindings["i_stop"], bindings["i_last"]

        def stepper_preamble(ctx):
            return asm.Block(
                hl.parse(
                    """
                    if (r < r_stop)
                        k = key[to_len(perm[r])]
                        i_stop = k.element_1
                    else
                        i_stop = dimension
                    end
                    """,
                    **bindings,
                )
            )

        def thunk_preamble(ctx, idx):
            return asm.Block(
                hl.parse(
                    """
                    r = to_len(ptr[to_len(pos)])
                    r_stop = to_len(ptr[add(to_len(pos), 1)])
                    i_stop = dimension
                    i_last = no_idx
                    if (r < r_stop)
                        k = key[to_len(perm[r])]
                        i_stop = k.element_1
                        k = key[to_len(perm[sub(r_stop, 1)])]
                        i_last = k.element_1
                    end
                    """,
                    **bindings,
                )
            )

        def seek_fn(ctx, ext):
            start = hl.expr("to_idx(start)", start=ctx.ctx(ext.get_start()))
            return asm.Block(
                (
                    *self._lower_scansearch(hl, bindings, start),
                    *stepper_preamble(ctx).bodies,
                )
            )

        def chunk_tail_fn(ctx, idx):
            pos_2 = asm.Variable(ctx.freshen(idx, f"_pos_{self.ndim - 1}"), p_t)
            ctx.exec(asm.Assign(pos_2, hl.expr("perm[r]", r=r)))
            return lplt.Run(
                ntn.Fiber(ntn.Child(level), ntn.Value(pos_2, p_t), (*fiber.idxs, idx))
            )

        return lplt.Thunk(
            preamble=thunk_preamble,
            body=lambda ctx, ext: lplt.Sequence(
                head=lambda ctx, idx: step(
                    preamble=stepper_preamble,
                    stop=lambda ctx: ntn.Variable(i_stop.name, d_t),
                    chunk=lplt.Sequence(
                        head=lambda ctx, idx: lplt.Run(full),
                        split=lambda ctx, ext: ntn.Variable(i_stop.name, d_t),
                        tail=chunk_tail_fn,
                    ),
                    next=lambda ctx: asm.Assign(r, hl.expr("add(r, 1)", r=r)),
                    seek=seek_fn,
                ),
                split=lambda ctx, idx: ntn.Call(
                    ntn.L(ffuncs.add),
                    (ntn.Variable(i_last.name, d_t), ext.get_unit()),
                ),
                tail=lambda ctx, idx: lplt.Run(full),
            ),
        )

    def _level_unfurl_follow(self, ctx, fiber, pos, full):
        lvl = ctx(fiber.lvl)
        p_t, d_t = self.position_type, self.dimension_type

        def lookup(ctx, idx):
            hl = _SparseHashLowering(self, ctx, lvl)
            p, i, q = hl.var("p", p_t), hl.var("i", d_t), hl.var("q", p_t)
            found = hl.var("found", ftypes.bool_)
            hashing, x = hl.key_hash(p, i)
            finding, h, _ = hl.find(p, i, x)
            # A table without slots holds no keys, and can't be probed.
            ctx.exec(
                asm.Block(
                    (
                        *hl.parse(
                            """
                            p = to_pos(pos)
                            i = to_idx(idx)
                            found = false
                            q = zero_pos
                            """,
                            p=p,
                            i=i,
                            q=q,
                            found=found,
                            pos=pos,
                            idx=ctx.ctx(idx),
                        ),
                        asm.If(
                            hl.expr("n_ctrl > 0"),
                            asm.Block(
                                (
                                    *hashing,
                                    *finding,
                                    *hl.parse(
                                        """
                                        if (tbl_ctrl[h] != EMPTY)
                                            found = true
                                            q = tbl[h]
                                        end
                                        """,
                                        h=h,
                                        q=q,
                                        found=found,
                                    ),
                                )
                            ),
                        ),
                    )
                )
            )
            return lplt.Switch(
                found,
                lplt.Run(
                    ntn.Fiber(
                        ntn.Child(fiber.lvl), ntn.Value(q, p_t), (*fiber.idxs, idx)
                    )
                ),
                lplt.Run(full),
            )

        return lplt.Lookup(lookup)

    def _lower_scansearch(self, hl, bindings, x):
        """
        Advance the rank `r` to the first one before `r_stop` whose index is at
        least `x`, galloping as `sparse_hash_scansearch`.
        """
        return hl.parse(
            """
            if (r < r_stop)
                k = key[to_len(perm[r])]
                if (k.element_1 < x)
                    hi = sub(r_stop, 1)
                    d = 1
                    p = r
                    going = true
                    while (going)
                        going = false
                        if (p < hi)
                            k = key[to_len(perm[p])]
                            if (k.element_1 < x)
                                d = add(d, d)
                                p = add(p, d)
                                going = true
                            end
                        end
                    end
                    lo = sub(p, d)
                    hi = add(min(p, hi), 1)
                    while (lo < sub(hi, 1))
                        m = add(lo, fld(sub(hi, lo), 2))
                        k = key[to_len(perm[m])]
                        if (k.element_1 < x)
                            lo = m
                        else
                            hi = m
                        end
                    end
                    r = hi
                end
            end
            """,
            **bindings,
            **{name: hl.var(name) for name in ("d", "p", "lo", "hi", "m")},
            going=hl.var("going", ftypes.bool_),
            x=x,
        )

    def level_unfurl_update(self, ctx, fiber, ext, mode, proto, pos):
        match proto:
            case ntn.DefaultProtocol() | ntn.Extrude() | ntn.Laminate():
                pass
            case _:
                raise NotImplementedError(
                    f"SparseHashLevelFType does not support the {proto} protocol."
                )
        lvl = ctx(fiber.lvl)
        child = asm.GetAttr(lvl, asm.Literal("lvl"))
        p_t, d_t = self.position_type, self.dimension_type

        def lookup(ctx, idx):
            hl = _SparseHashLowering(self, ctx, lvl)
            p, i, qos = hl.var("p", p_t), hl.var("i", d_t), hl.var("qos", p_t)
            b, old, q_stop, k = (
                hl.var("b"),
                hl.var("old"),
                hl.var("q_stop"),
                hl.key_var(),
            )
            shared = hl.var("shared", ftype(np.bool_))
            dirty = ntn.Variable(ctx.freshen("dirty"), ftypes.bool_)
            dirty_asm = ctx.ctx(dirty)
            hashing, x = hl.key_hash(p, i)
            finding, h, y = hl.find(p, i, x)
            names = locals()

            def prepare(ctx, _):
                grow = ctx.ctx.block()
                exec_block(
                    grow,
                    asm.Block(
                        hl.parse(
                            """
                            old = n_key
                            q_stop = max(mul(2, n_key), add(to_len(qos), 1))
                            resize(key, q_stop)
                            """,
                            **names,
                        )
                    ),
                )
                self.lvl_t.level_lower_assemble(grow, child, old, q_stop)
                # A new key gets a child position and is inserted tentatively
                # before its child is written.
                insert = (
                    *hl.parse(
                        """
                        tbl_count[b] = add(tbl_count[b], 1)
                        if (lvl.free_head == no_pos)
                            qos = lvl.qos_stop
                            lvl.qos_stop = add(lvl.qos_stop, one_pos)
                        else
                            qos = lvl.free_head
                            k = key[to_len(qos)]
                            lvl.free_head = k.element_0
                        end
                        """,
                        **names,
                    ),
                    asm.If(
                        hl.expr("to_len(qos) >= n_key", qos=qos),
                        asm.Block(tuple(grow.emit())),
                    ),
                    *hl.parse(
                        """
                        key[to_len(qos)] = make_tuple(p, i, ONE_U8)
                        tbl_ctrl[h] = ctrl
                        tbl[h] = qos
                        """,
                        **names,
                        ctrl=hl.ctrl(y),
                    ),
                )
                # Later writers join a tentative entry by its stable child
                # position. A retained entry needs no release.
                join = hl.parse(
                    """
                    qos = tbl[h]
                    k = key[to_len(qos)]
                    shared = k.element_2 != RETAINED
                    """,
                    **names,
                )
                if ctx.mode.safe or ctx.mode.debug:
                    join += (
                        asm.If(
                            shared,
                            asm.Assert(hl.expr("k.element_2 < MAX_PENDING", k=k)),
                        ),
                    )
                join += hl.parse(
                    """
                    if (shared)
                        key[to_len(qos)] = joined
                    end
                    """,
                    **names,
                    joined=hl.restate(k, hl.expr("add(k.element_2, ONE_U8)", k=k)),
                )
                return asm.Block(
                    (
                        *hl.parse(
                            """
                            p = to_pos(pos)
                            i = to_idx(idx)
                            """,
                            p=p,
                            i=i,
                            pos=pos,
                            idx=ctx.ctx(idx),
                        ),
                        *hashing,
                        asm.Assign(b, hl.subtable(x)),
                        # Grow before probing, so the probed slot stays valid if
                        # the key turns out to be new.
                        asm.If(
                            hl.expr(
                                "mul(mul(2, B), add(tbl_count[b], 1)) > n_ctrl", b=b
                            ),
                            asm.Block(
                                hl.resize_table(
                                    hl.expr("max(mul(2, n_ctrl), mul(4, B))")
                                )
                            ),
                        ),
                        *finding,
                        asm.Assign(shared, asm.Literal(np.bool_(True))),
                        asm.Assign(qos, hl.bindings["zero_pos"]),
                        asm.IfElse(
                            hl.expr("tbl_ctrl[h] == EMPTY", h=h),
                            asm.Block(insert),
                            asm.Block(join),
                        ),
                        asm.Assign(dirty_asm, asm.Literal(False)),
                    )
                )

            def finish(ctx, _):
                # The first dirty writer retains the entry. Otherwise, the last
                # writer backward-shift deletes it and frees its child position.
                k_2 = hl.key_var()
                p_2, i_2 = hl.key_fields(k_2)
                deleting, h_2, _ = hl.find(p_2, i_2, x)
                release = (
                    *deleting,
                    *hl.delete(h_2, x),
                    *hl.parse(
                        """
                        key[to_len(qos)] = make_tuple(lvl.free_head, k.element_1, FREE)
                        tbl_count[b] = sub(tbl_count[b], 1)
                        lvl.free_head = qos
                        """,
                        qos=qos,
                        k=k_2,
                        b=b,
                    ),
                )
                retain, unshare = (
                    hl.parse(
                        "key[to_len(qos)] = record",
                        qos=qos,
                        record=hl.restate(k_2, hl.expr(state, k=k_2)),
                    )
                    for state in ("RETAINED", "sub(k.element_2, ONE_U8)")
                )
                stmts = asm.If(
                    shared,
                    asm.Block(
                        (
                            *hl.parse("k = key[to_len(qos)]", k=k_2, qos=qos),
                            asm.If(
                                hl.expr("k.element_2 != RETAINED", k=k_2),
                                asm.IfElse(
                                    dirty_asm,
                                    asm.Block(retain),
                                    asm.IfElse(
                                        hl.expr("k.element_2 > ONE_U8", k=k_2),
                                        asm.Block(unshare),
                                        asm.Block(release),
                                    ),
                                ),
                            ),
                        )
                    ),
                )
                match fiber:
                    case ntn.HollowFiber(dirty=parent_dirty):
                        return asm.Block(
                            (
                                asm.If(
                                    dirty_asm,
                                    asm.Assign(
                                        ctx.ctx(parent_dirty), asm.Literal(True)
                                    ),
                                ),
                                stmts,
                            )
                        )
                return stmts

            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,
            )

        return lplt.Lookup(lookup)

    def from_fields(
        self,
        lvl,
        dimension,
        subtables,
        pos_multiplier,
        ptr,
        tbl_ctrl,
        tbl,
        key,
        perm,
        tbl_count,
        qos_stop=None,
        free_head=_NO_POS,
        frozen=True,
    ) -> "SparseHashLevel":
        return SparseHashLevel(
            lvl,
            dimension,
            ptr,
            tbl_ctrl,
            tbl,
            key,
            perm,
            tbl_count,
            subtables=subtables,
            pos_multiplier=pos_multiplier,
            qos_stop=qos_stop,
            free_head=free_head,
            frozen=frozen,
        )

SparseListLevel dataclass

Bases: SingleDimensionLevel

A class representing sparse list level.

Source code in src/finch/tensor/level/sparse_list_level.py
@dataclass
class SparseListLevel(SingleDimensionLevel):
    """
    A class representing sparse list level.
    """

    lvl: Level
    dimension: np.integer
    ptr: Any = None
    idx: Any = None
    # These cursors must survive serialization while ptr holds per-parent counts.
    qos_fill: Any = None
    qos_stop: Any = None
    # One past the last occupied parent position; zero means none.
    prev_pos: Any = 0
    frozen: bool = True

    @property
    def shape(self) -> tuple:
        return (self.dimension, *self.lvl.shape)

    def __post_init__(self):
        if self.ptr is None:
            self.ptr = self.lvl.buffer_factory(self.lvl.position_type)(1)
        if self.idx is None:
            self.idx = self.lvl.buffer_factory(ftype(self.dimension))(0)
        p_t = self.lvl.position_type
        self.qos_fill = p_t(
            self.idx.length() if self.qos_fill is None else self.qos_fill
        )
        self.qos_stop = p_t(
            self.idx.length() if self.qos_stop is None else self.qos_stop
        )
        self.prev_pos = p_t(self.prev_pos)

    @property
    def ftype(self) -> SparseListLevelFType:
        return SparseListLevelFType(self.lvl.ftype, ftype(self.dimension))

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

    def __str__(self):
        return f"SparseListLevel(lvl={self.lvl}, dim={self.dimension})"

SparseListLevelFType dataclass

Bases: SingleDimensionLevelFType, MutableStructFType

Source code in src/finch/tensor/level/sparse_list_level.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
@dataclass(unsafe_hash=True)
class SparseListLevelFType(SingleDimensionLevelFType, MutableStructFType):
    _lvl_t: LevelFType
    dimension_type: ftypes.FDTypeInteger = ftypes.intp

    def __post_init__(self):
        self.dimension_type = ftype(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

    @property
    def ptr_type(self):
        return self.buffer_factory(self.position_type)

    @property
    def idx_type(self):
        return self.buffer_factory(self.dimension_type)

    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, obj, r):
        if r == 0:
            return asm.GetAttr(obj, asm.Literal("dimension"))
        return self.lvl_t.level_lower_dim(
            ctx, asm.GetAttr(obj, asm.Literal("lvl")), r - 1
        )

    def level_lower_declare(self, ctx, tns, 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(tns, 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(tns.ptr, stop)
        for (p in 0:stop)
            tns.ptr[p] = zero
        end
        tns.qos_fill = zero
        tns.qos_stop = zero
        tns.prev_pos = zero
        tns.frozen = false
        """
        ctx.exec(parse_assembly(expr, locals(), position_type=p_t))
        return self.lvl_t.level_lower_declare(
            ctx, asm.GetAttr(tns, 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, obj, 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, obj, pos):
        raise NotImplementedError(
            "SparseListLevelFType does not support level_lower_unwrap."
        )

    def level_unfurl(
        self, ctx, fiber: ntn.Fiber, ext, mode: ntn.AccessMode, proto, pos
    ):
        match mode:
            case ntn.Update():
                return self.level_unfurl_update(ctx, fiber, ext, mode, proto, pos)
        tns = fiber
        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, fiber, 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
        )

buffer_factory property

Returns the ftype of the buffer used for the fibers.

element_type property

Returns the type of elements stored in the fibers.

position_type property

Returns the type of positions within the levels.

shape_type property

Returns the type of the shape of the fibers.

__call__(val)

Convert a level to this sparse list level type.

Parameters:

Name Type Description Default
val Any

A value to convert to this type.

required

Returns: A SparseListLevel instance of this type.

Source code in src/finch/tensor/level/sparse_list_level.py
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__}"
    )

construct(shape, *, pos)

Creates an instance of SparseListLevel.

Parameters:

Name Type Description Default
shape tuple[Any, ...]

The shape to be used for the level. (mandatory)

required

Returns: An instance of DenseLevel.

Source code in src/finch/tensor/level/sparse_list_level.py
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),
    )

element(fill_type=None, element_type=None, position_type=None, buffer_factory=NumpyBufferFType, buffer_type=None)

Creates an ElementLevelFType with the given parameters.

Parameters:

Name Type Description Default
fill_type Any

The fill of the level's format, or the value of a static fill.

None
element_type FType | None

The type of elements stored in the level.

None
position_type FType | None

The type of positions within the level.

None
buffer_factory Any

The factory used to create buffers for the level.

NumpyBufferFType
buffer_type Any

Format of the value stored in the level.

None

Returns:

Type Description
ElementLevelFType

An instance of ElementLevelFType.

Source code in src/finch/tensor/level/element_level.py
def element(
    fill_type: Any = None,
    element_type: FType | None = None,
    position_type: FType | None = None,
    buffer_factory: Any = NumpyBufferFType,
    buffer_type: Any = None,
) -> ElementLevelFType:
    """
    Creates an ElementLevelFType with the given parameters.

    Args:
        fill_type: The fill of the level's format, or the value of a static
            fill.
        element_type: The type of elements stored in the level.
        position_type: The type of positions within the level.
        buffer_factory: The factory used to create buffers for the level.
        buffer_type: Format of the value stored in the level.

    Returns:
        An instance of ElementLevelFType.
    """
    match fill_type:
        case AbstractFillFType() | None:
            pass
        case AbstractFill():
            raise TypeError(f"element takes the fill of a format, not {fill_type}")
        case _:
            fill_type = StaticFillFType(fill_type)
    return ElementLevelFType(
        fill_type=fill_type,
        element_type=element_type,
        position_type=position_type,
        buffer_factory=buffer_factory,
        buffer_type=buffer_type,
    )