Skip to content

Linalg

linalg

LinalgFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/linalg.py
@register_impls
class LinalgFunctions(InterpreterFunctions):
    @impl(linalg.ops.GenericOp)
    def run_generic(
        self, interpreter: Interpreter, op: linalg.ops.GenericOp, args: tuple[Any, ...]
    ) -> PythonValues:
        if op.library_call is not None:
            raise NotImplementedError(
                "library_call not yet supported in linalg.generic interpreter"
            )
        return run_linalg_structured_op(interpreter, op, args)

    @impl_terminator(linalg.ops.YieldOp)
    def run_yield(
        self, interpreter: Interpreter, op: linalg.ops.YieldOp, args: tuple[Any, ...]
    ):
        return ReturnedValues(args), ()

    @impl(linalg.ops.AddOp)
    def run_add(
        self, interpreter: Interpreter, op: linalg.ops.AddOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.ExpOp)
    def run_exp(
        self, interpreter: Interpreter, op: linalg.ops.ExpOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.LogOp)
    def run_log(
        self, interpreter: Interpreter, op: linalg.ops.LogOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.SqrtOp)
    def run_sqrt(
        self, interpreter: Interpreter, op: linalg.ops.SqrtOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.FillOp)
    def run_fill(
        self, interpreter: Interpreter, op: linalg.ops.FillOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        operand, res = args[0], args[1]
        assert isa(operand, FloatAttr | IntegerAttr)
        assert isinstance(res, ShapedArray)
        res = cast(ShapedArray[float], res)
        if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
            raise NotImplementedError()
        for i in range(len(res.data)):
            res.data_ptr[i] = operand.value.data
        if len(op.results) > 0:
            return (res,)
        return ()

    @impl(linalg.ops.MulOp)
    def run_mul(
        self, interpreter: Interpreter, op: linalg.ops.MulOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.TransposeOp)
    def run_transpose(
        self,
        interpreter: Interpreter,
        op: linalg.ops.TransposeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.MatmulOp)
    def run_mat_mul(
        self, interpreter: Interpreter, op: linalg.ops.MatmulOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        return run_linalg_structured_op(interpreter, op, args)

    @impl(linalg.ops.PoolingNchwMaxOp)
    def run_pooling_nchw_max(
        self,
        interpreter: Interpreter,
        op: linalg.ops.PoolingNchwMaxOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        input, kernel_filter, res = args[0], args[1], args[2]
        assert isinstance(input, ShapedArray)
        assert isinstance(kernel_filter, ShapedArray)
        assert isinstance(res, ShapedArray)
        input = cast(ShapedArray[float], input)
        kernel_filter = cast(ShapedArray[float], kernel_filter)
        res = cast(ShapedArray[float], res)
        if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
            raise NotImplementedError()
        strides_type = op.strides.type
        assert isinstance(strides_type, TensorType)
        (strides_shape,) = strides_type.get_shape()
        strides = op.strides.get_values()
        if strides_shape != 2:
            raise NotImplementedError("Only 2d max pooling supported")

        m_height, m_width = input.shape[2:]
        ky, kx = kernel_filter.shape[0], kernel_filter.shape[1]

        # convert input into a numpy like array
        input_data = [
            [input.data[r * m_width + c] for c in range(m_width)]
            for r in range(m_height)
        ]

        output: list[float] = []
        for k in range(0, m_height - ky + 1, strides[0]):
            for l in range(0, m_width - kx + 1, strides[0]):
                block_max_value = float("-inf")
                for i in range(k, k + ky):
                    for j in range(l, l + kx):
                        block_max_value = max(block_max_value, input_data[i][j])
                output.append(block_max_value)
        for i in range(len(output)):
            res.data_ptr[i] = output[i]
        if len(op.results) > 0:
            return (res,)
        return ()

    @impl(linalg.ops.Conv2DNchwFchwOp)
    def run_conv_2d_nchw_fchw(
        self,
        interpreter: Interpreter,
        op: linalg.ops.Conv2DNchwFchwOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        input, kernel_filter, res = args[0], args[1], args[2]
        assert isinstance(input, ShapedArray)
        assert isinstance(kernel_filter, ShapedArray)
        assert isinstance(res, ShapedArray)
        input = cast(ShapedArray[float], input)
        kernel_filter = cast(ShapedArray[float], kernel_filter)
        res = cast(ShapedArray[float], res)
        if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
            raise NotImplementedError()
        m_height, m_width = input.shape[2:]
        ky, kx = kernel_filter.shape[2], kernel_filter.shape[3]
        strides = op.strides.get_values()
        # convert input into a numpy like array
        input_data = [
            [input.data[r * m_width + c] for c in range(m_width)]
            for r in range(m_height)
        ]
        # convert kernel into a numpy like array
        kernel_data = [
            [
                kernel_filter.data[r * kernel_filter.shape[3] + c]
                for c in range(kernel_filter.shape[3])
            ]
            for r in range(kernel_filter.shape[2])
        ]
        output: list[float] = []
        for k in range(0, m_height - ky + 1, strides[0]):
            for l in range(0, m_width - kx + 1, strides[0]):
                conv_value: float = 0.0
                for i in range(k, k + ky):
                    for j in range(l, l + kx):
                        conv_value += input_data[i][j] * kernel_data[i - k][j - l]
                output.append(conv_value)
        for i in range(len(output)):
            res.data_ptr[i] = output[i]
        if len(op.results) > 0:
            return (res,)
        return ()

run_generic(interpreter: Interpreter, op: linalg.ops.GenericOp, args: tuple[Any, ...]) -> PythonValues

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.GenericOp)
def run_generic(
    self, interpreter: Interpreter, op: linalg.ops.GenericOp, args: tuple[Any, ...]
) -> PythonValues:
    if op.library_call is not None:
        raise NotImplementedError(
            "library_call not yet supported in linalg.generic interpreter"
        )
    return run_linalg_structured_op(interpreter, op, args)

run_yield(interpreter: Interpreter, op: linalg.ops.YieldOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/linalg.py
@impl_terminator(linalg.ops.YieldOp)
def run_yield(
    self, interpreter: Interpreter, op: linalg.ops.YieldOp, args: tuple[Any, ...]
):
    return ReturnedValues(args), ()

run_add(interpreter: Interpreter, op: linalg.ops.AddOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.AddOp)
def run_add(
    self, interpreter: Interpreter, op: linalg.ops.AddOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_exp(interpreter: Interpreter, op: linalg.ops.ExpOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.ExpOp)
def run_exp(
    self, interpreter: Interpreter, op: linalg.ops.ExpOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_log(interpreter: Interpreter, op: linalg.ops.LogOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.LogOp)
def run_log(
    self, interpreter: Interpreter, op: linalg.ops.LogOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_sqrt(interpreter: Interpreter, op: linalg.ops.SqrtOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.SqrtOp)
def run_sqrt(
    self, interpreter: Interpreter, op: linalg.ops.SqrtOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_fill(interpreter: Interpreter, op: linalg.ops.FillOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.FillOp)
def run_fill(
    self, interpreter: Interpreter, op: linalg.ops.FillOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    operand, res = args[0], args[1]
    assert isa(operand, FloatAttr | IntegerAttr)
    assert isinstance(res, ShapedArray)
    res = cast(ShapedArray[float], res)
    if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
        raise NotImplementedError()
    for i in range(len(res.data)):
        res.data_ptr[i] = operand.value.data
    if len(op.results) > 0:
        return (res,)
    return ()

run_mul(interpreter: Interpreter, op: linalg.ops.MulOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.MulOp)
def run_mul(
    self, interpreter: Interpreter, op: linalg.ops.MulOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_transpose(interpreter: Interpreter, op: linalg.ops.TransposeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.TransposeOp)
def run_transpose(
    self,
    interpreter: Interpreter,
    op: linalg.ops.TransposeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_mat_mul(interpreter: Interpreter, op: linalg.ops.MatmulOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.MatmulOp)
def run_mat_mul(
    self, interpreter: Interpreter, op: linalg.ops.MatmulOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    return run_linalg_structured_op(interpreter, op, args)

run_pooling_nchw_max(interpreter: Interpreter, op: linalg.ops.PoolingNchwMaxOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.PoolingNchwMaxOp)
def run_pooling_nchw_max(
    self,
    interpreter: Interpreter,
    op: linalg.ops.PoolingNchwMaxOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    input, kernel_filter, res = args[0], args[1], args[2]
    assert isinstance(input, ShapedArray)
    assert isinstance(kernel_filter, ShapedArray)
    assert isinstance(res, ShapedArray)
    input = cast(ShapedArray[float], input)
    kernel_filter = cast(ShapedArray[float], kernel_filter)
    res = cast(ShapedArray[float], res)
    if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
        raise NotImplementedError()
    strides_type = op.strides.type
    assert isinstance(strides_type, TensorType)
    (strides_shape,) = strides_type.get_shape()
    strides = op.strides.get_values()
    if strides_shape != 2:
        raise NotImplementedError("Only 2d max pooling supported")

    m_height, m_width = input.shape[2:]
    ky, kx = kernel_filter.shape[0], kernel_filter.shape[1]

    # convert input into a numpy like array
    input_data = [
        [input.data[r * m_width + c] for c in range(m_width)]
        for r in range(m_height)
    ]

    output: list[float] = []
    for k in range(0, m_height - ky + 1, strides[0]):
        for l in range(0, m_width - kx + 1, strides[0]):
            block_max_value = float("-inf")
            for i in range(k, k + ky):
                for j in range(l, l + kx):
                    block_max_value = max(block_max_value, input_data[i][j])
            output.append(block_max_value)
    for i in range(len(output)):
        res.data_ptr[i] = output[i]
    if len(op.results) > 0:
        return (res,)
    return ()

run_conv_2d_nchw_fchw(interpreter: Interpreter, op: linalg.ops.Conv2DNchwFchwOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/linalg.py
@impl(linalg.ops.Conv2DNchwFchwOp)
def run_conv_2d_nchw_fchw(
    self,
    interpreter: Interpreter,
    op: linalg.ops.Conv2DNchwFchwOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    input, kernel_filter, res = args[0], args[1], args[2]
    assert isinstance(input, ShapedArray)
    assert isinstance(kernel_filter, ShapedArray)
    assert isinstance(res, ShapedArray)
    input = cast(ShapedArray[float], input)
    kernel_filter = cast(ShapedArray[float], kernel_filter)
    res = cast(ShapedArray[float], res)
    if not all(res.data_ptr[i] == 0.0 for i in range(len(res.data))):
        raise NotImplementedError()
    m_height, m_width = input.shape[2:]
    ky, kx = kernel_filter.shape[2], kernel_filter.shape[3]
    strides = op.strides.get_values()
    # convert input into a numpy like array
    input_data = [
        [input.data[r * m_width + c] for c in range(m_width)]
        for r in range(m_height)
    ]
    # convert kernel into a numpy like array
    kernel_data = [
        [
            kernel_filter.data[r * kernel_filter.shape[3] + c]
            for c in range(kernel_filter.shape[3])
        ]
        for r in range(kernel_filter.shape[2])
    ]
    output: list[float] = []
    for k in range(0, m_height - ky + 1, strides[0]):
        for l in range(0, m_width - kx + 1, strides[0]):
            conv_value: float = 0.0
            for i in range(k, k + ky):
                for j in range(l, l + kx):
                    conv_value += input_data[i][j] * kernel_data[i - k][j - l]
            output.append(conv_value)
    for i in range(len(output)):
        res.data_ptr[i] = output[i]
    if len(op.results) > 0:
        return (res,)
    return ()

run_linalg_structured_op(interpreter: Interpreter, op: linalg.abstract_ops.LinalgStructuredOperation, args: tuple[ShapedArray[float] | FloatAttr | IntegerAttr, ...])

Helper function for interpreting ops inheriting from LinalgStructuredOperation.

Source code in xdsl/interpreters/linalg.py
def run_linalg_structured_op(
    interpreter: Interpreter,
    op: linalg.abstract_ops.LinalgStructuredOperation,
    args: tuple[ShapedArray[float] | FloatAttr | IntegerAttr, ...],
):
    """
    Helper function for interpreting ops inheriting from
    [`LinalgStructuredOperation`][xdsl.dialects.linalg.abstract_ops.LinalgStructuredOperation].
    """
    body = op.body
    inputs_count = len(op.inputs)
    input_args = args[:inputs_count]
    output_args = args[inputs_count:]
    results = op.results
    indexing_maps = tuple(attr.data for attr in op.get_indexing_maps())

    if any(not isinstance(arg, ShapedArray) for arg in output_args):
        raise NotImplementedError("Only shaped out results are implemented")
    output_args = cast(tuple[ShapedArray[float], ...], output_args)
    if results:
        # If there are results, they must be tensors, initialised with the
        # `output_args`. If not, the results are stored in output_args directly.
        outputs = tuple(arg.copy() for arg in output_args)
    else:
        outputs = output_args

    loop_shaped_args = input_args + outputs

    # Static loop ranges use DYNAMIC_INDEX for dynamic dimensions, which makes
    # ``range`` silently skip every iteration. Infer the operand shape list
    # from the runtime shaped arguments instead, preserving the existing
    # shapes-to-loops mapping (and scalar operand handling).
    runtime_shapes = [
        dim
        for arg in loop_shaped_args
        if isinstance(arg, ShapedArray)
        for dim in arg.shape
    ]
    loop_ranges = op.get_shapes_to_loops_map().eval(runtime_shapes, [])

    output_indexing_maps = indexing_maps[inputs_count:]

    for indices in product(*(range(loop_range) for loop_range in loop_ranges)):
        loop_scalar_args = tuple(
            (
                scalar_attr(
                    i.load(indexing_map.eval(indices, ())),
                    block_arg.type,
                    interpreter.index_bitwidth,
                )
                if isinstance(i, ShapedArray)
                else i
            )
            for i, indexing_map, block_arg in zip(
                loop_shaped_args, indexing_maps, body.block.args, strict=True
            )
        )
        loop_results = interpreter.run_ssacfg_region(body, loop_scalar_args, "for_loop")
        for res, indexing_map in zip(loop_results, output_indexing_maps, strict=True):
            result_indices = indexing_map.eval(indices, ())
            outputs[0].store(result_indices, res.value.data)

    return outputs if results else ()