Skip to content

interop

JuliaBufferContext

Keep Julia-owned tensor buffers alive across kernel invocations.

Source code in src/finch/compile_jl/interop.py
class JuliaBufferContext:
    """Keep Julia-owned tensor buffers alive across kernel invocations."""

    def __init__(self):
        self._tensors: dict[tuple[Any, ...], tuple[Any, Any]] = {}

    @staticmethod
    def _cache_key(obj):
        # defer() can create a fresh wrapper around the same NumPy
        # allocation, so wrapper identity alone would miss reuse.
        if isinstance(obj, BufferizedNDArray):
            arr = obj.to_numpy()
            pointer = arr.__array_interface__["data"][0]
            return ("numpy", pointer, arr.shape, arr.strides, arr.dtype.str)
        if isinstance(obj, NumPyWrapper):
            arr = obj._data
            pointer = arr.__array_interface__["data"][0]
            return ("numpy", pointer, arr.shape, arr.strides, arr.dtype.str)
        # FiberTensors reuse their ids so we restrict cache keys to id.
        return ("object", id(obj))

    def tensor_to_jl(self, obj, *, pin_fill: bool = False):
        key = (self._cache_key(obj), pin_fill)
        cached = self._tensors.get(key)
        if cached is not None:
            return cached[1]

        jl_obj = tensor_to_jl(obj, pin_fill=pin_fill)
        self._tensors[key] = (obj, jl_obj)
        return jl_obj

    def tensor_to_python(self, obj):
        result = jl_tensor_to_python(obj)
        if isinstance(result, FiberTensor):
            self._tensors[self._cache_key(result)] = (result, obj)
        return result

    def close(self):
        self._tensors.clear()

level_to_jl(level, pin_fill=False)

Convert a level to its Julia counterpart, optionally pinning its fill.

Source code in src/finch/compile_jl/interop.py
def level_to_jl(level: Level, pin_fill: bool = False):
    """Convert a level to its Julia counterpart, optionally pinning its fill."""
    match level:
        case ElementLevel():
            fill = level.fill_value
            if pin_fill:
                fill = jl_dtypes._zero_value(ftype(fill))
            return jl.ElementLevel(
                _as_julia_scalar(fill),
                _buffer_to_jl(level.val),
            )
        case DenseLevel(lvl=lvl, dimension=dimension):
            return jl.DenseLevel(level_to_jl(lvl, pin_fill), int(dimension))
        case SparseListLevel(lvl=lvl, dimension=dimension, ptr=ptr, idx=idx):
            if ptr is None or idx is None:
                raise ValueError("SparseListLevel must have ptr and idx buffers")
            constructor = jl.SparseListLevel[jl_dtypes.to_jl_type(ftype(dimension))]
            return constructor(
                level_to_jl(lvl, pin_fill),
                int(dimension),
                _buffer_to_jl(cast(Buffer, ptr), offset=1),
                _buffer_to_jl(cast(Buffer, idx), offset=1),
            )
        case SparseByteMapLevel(
            lvl=lvl, dimension=dimension, ptr=ptr, tbl=tbl, srt=srt
        ):
            if ptr is None or tbl is None or srt is None:
                raise ValueError(
                    "SparseByteMapLevel must have ptr, tbl, and srt buffers"
                )
            constructor = jl.SparseByteMapLevel[jl_dtypes.to_jl_type(ftype(dimension))]
            return constructor(
                level_to_jl(lvl, pin_fill),
                int(dimension),
                _buffer_to_jl(cast(Buffer, ptr), offset=1),
                _buffer_to_jl(cast(Buffer, tbl)),
                _buffer_to_jl(cast(Buffer, srt), offset=1),
            )
        case SparseCOOLevel(lvl=lvl, coo_shape=coo_shape, ptr=ptr, tbl=tbl):
            assert isinstance(tbl, tuple)
            # Julia's COO lists its dims fastest first, Python's slowest first
            return jl.SparseCOOLevel(
                level_to_jl(lvl, pin_fill),
                tuple(int(dim) for dim in reversed(coo_shape)),
                _buffer_to_jl(ptr, offset=1),
                tuple(_buffer_to_jl(idx, offset=1) for idx in reversed(tbl)),
            )
        case SparseHashLevel(
            lvl=lvl,
            dimension=dimension,
            ptr=ptr,
            tbl_ctrl=tbl_ctrl,
            tbl=tbl,
            key=key,
            perm=perm,
            tbl_count=tbl_count,
            subtables=subtables,
            pos_multiplier=pos_multiplier,
        ):
            if (
                ptr is None
                or tbl_ctrl is None
                or tbl is None
                or key is None
                or perm is None
                or tbl_count is None
            ):
                raise ValueError(
                    "SparseHashLevel must have ptr, tbl_ctrl, tbl, key, perm, "
                    "and tbl_count buffers"
                )
            constructor = jl.SparseHashLevel[jl_dtypes.to_jl_type(ftype(dimension))]
            return constructor(
                level_to_jl(lvl, pin_fill),
                int(dimension),
                int(subtables),
                pos_multiplier,
                _buffer_to_jl(cast(Buffer, ptr), offset=1),
                _buffer_to_jl(cast(Buffer, tbl_ctrl)),
                _buffer_to_jl(cast(Buffer, tbl), offset=1),
                _buffer_to_jl(cast(Buffer, key), offset=1, index_fields=(0, 1)),
                _buffer_to_jl(cast(Buffer, perm), offset=1),
                _buffer_to_jl(cast(Buffer, tbl_count)),
            )
        case _:
            raise ValueError(f"Unsupported Finch level type: {type(level)}")

tensor_to_jl(obj, pin_fill=False)

Convert a tensor to its Julia counterpart, optionally pinning its fill.

Source code in src/finch/compile_jl/interop.py
def tensor_to_jl(obj, pin_fill: bool = False):
    """Convert a tensor to its Julia counterpart, optionally pinning its fill."""
    if hasattr(obj, "raw_julia_obj"):
        return obj.raw_julia_obj
    if is_julia_obj(obj) and jl.isa(obj, jl.Finch.Tensor):
        return obj
    if isinstance(obj, Temp0D):
        # A kernel resets a Temp0D before reading it, so it is passed the Finch
        # scalar types.py constructs for its format, whatever its value.
        return jl.seval(jl_dtypes.ftype_to_jl_constructor_str(obj.ftype))
    if isinstance(obj, FiberTensor):
        if obj.pos != 0:
            raise ValueError("Only root-position FiberTensor objects can use Julia")
        return jl.Tensor(level_to_jl(obj.lvl, pin_fill))
    if isinstance(obj, BufferizedNDArray):
        fill = (
            jl_dtypes._zero_value(ftype(obj.fill_value)) if pin_fill else obj.fill_value
        )
        return _ndarray_to_jl_tensor(obj.to_numpy(), fill, copy=False)
    if isinstance(obj, NumPyWrapper):
        fill = (
            jl_dtypes._zero_value(ftype(obj.fill_value)) if pin_fill else obj.fill_value
        )
        return _ndarray_to_jl_tensor(obj._data, fill, copy=False)
    if isinstance(obj, Scalar):
        return scalar_to_jl(obj.val, pin_fill=pin_fill)
    if isinstance(obj, PatternTensor):
        return _pattern_tensor_to_jl(obj)
    if isinstance(obj, FillTensor):
        # A fill tensor is its fill everywhere, and its extents are passed to
        # kernels separately, so it is passed as a Finch scalar.
        fill = jl_dtypes._zero_value(obj.element_type) if pin_fill else obj.fill_value
        scalar_t = jl.seval(jl_dtypes.scalar_type_str(fill, obj.element_type))
        return scalar_t(_as_julia_scalar(obj.fill_value))
    if isinstance(obj, np.ndarray):
        fill = np.asarray(0, dtype=obj.dtype)[()]
        return _ndarray_to_jl_tensor(obj, fill, copy=False)
    if np.isscalar(obj):
        return scalar_to_jl(obj, pin_fill=pin_fill)
    raise ValueError(f"Unsupported Julia backend argument type: {type(obj)}")