Skip to content

Tosa

tosa

TInv = TypeVar('TInv', bound=TensorType) module-attribute

TOSA = Dialect('tosa', [ClampOp, ConstOp, RescaleOp, AddOp, SubOp, MulOp, SinOp, CosOp, ReciprocalOp, ReduceAllOp, ReduceAnyOp, ReduceMaxOp, ReduceMinOp, ReduceProductOp, ReduceSumOp, MatMulOp, MaxPool2DOp, AvgPool2DOp, ConcatOp, IfOp, YieldOp], [NanModeAttr, RoundingModeAttr]) module-attribute

RoundingMode

Bases: StrEnum

Source code in xdsl/dialects/tosa.py
92
93
94
95
class RoundingMode(StrEnum):
    SINGLE_ROUND = "SINGLE_ROUND"
    INEXACT_ROUND = "INEXACT_ROUND"
    DOUBLE_ROUND = "DOUBLE_ROUND"

SINGLE_ROUND = 'SINGLE_ROUND' class-attribute instance-attribute

INEXACT_ROUND = 'INEXACT_ROUND' class-attribute instance-attribute

DOUBLE_ROUND = 'DOUBLE_ROUND' class-attribute instance-attribute

RoundingModeAttr dataclass

Bases: EnumAttribute[RoundingMode]

Rounding mode for tosa.rescale See external documentation.

Source code in xdsl/dialects/tosa.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@irdl_attr_definition
class RoundingModeAttr(EnumAttribute[RoundingMode]):
    """
    Rounding mode for `tosa.rescale`
    See external [documentation](https://github.com/llvm/llvm-project/blob/fef02d48c08db859ef83f84232ed78bd9d1c323a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td#L470).
    """

    name = "tosa.rounding_mode"

    def print_parameter(self, printer: Printer) -> None:
        with printer.in_angle_brackets():
            printer.print_string(self.data)

    @classmethod
    def parse_parameter(cls, parser: AttrParser) -> RoundingMode:
        with parser.in_angle_brackets():
            return parser.parse_str_enum(RoundingMode)

name = 'tosa.rounding_mode' class-attribute instance-attribute

print_parameter(printer: Printer) -> None

Source code in xdsl/dialects/tosa.py
107
108
109
def print_parameter(self, printer: Printer) -> None:
    with printer.in_angle_brackets():
        printer.print_string(self.data)

parse_parameter(parser: AttrParser) -> RoundingMode classmethod

Source code in xdsl/dialects/tosa.py
111
112
113
114
@classmethod
def parse_parameter(cls, parser: AttrParser) -> RoundingMode:
    with parser.in_angle_brackets():
        return parser.parse_str_enum(RoundingMode)

NanMode

Bases: StrEnum

Source code in xdsl/dialects/tosa.py
117
118
119
class NanMode(StrEnum):
    PROPAGATE = "PROPAGATE"
    IGNORE = "IGNORE"

PROPAGATE = 'PROPAGATE' class-attribute instance-attribute

IGNORE = 'IGNORE' class-attribute instance-attribute

NanModeAttr dataclass

Bases: EnumAttribute[NanMode]

Supported NaN propagation strategies See external documentation.

Source code in xdsl/dialects/tosa.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@irdl_attr_definition
class NanModeAttr(EnumAttribute[NanMode]):
    """
    Supported NaN propagation strategies
    See external [documentation](https://github.com/llvm/llvm-project/blob/fef02d48c08db859ef83f84232ed78bd9d1c323a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td#L462).
    """

    name = "tosa.nan_mode"

    def print_parameter(self, printer: Printer) -> None:
        with printer.in_angle_brackets():
            printer.print_string(self.data)

    @classmethod
    def parse_parameter(cls, parser: AttrParser) -> NanMode:
        with parser.in_angle_brackets():
            return parser.parse_str_enum(NanMode)

name = 'tosa.nan_mode' class-attribute instance-attribute

print_parameter(printer: Printer) -> None

Source code in xdsl/dialects/tosa.py
131
132
133
def print_parameter(self, printer: Printer) -> None:
    with printer.in_angle_brackets():
        printer.print_string(self.data)

parse_parameter(parser: AttrParser) -> NanMode classmethod

Source code in xdsl/dialects/tosa.py
135
136
137
138
@classmethod
def parse_parameter(cls, parser: AttrParser) -> NanMode:
    with parser.in_angle_brackets():
        return parser.parse_str_enum(NanMode)

ClampOp dataclass

Bases: IRDLOperation

Computes clamp(features, min, max)

See external documentation

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class ClampOp(IRDLOperation):
    """
    Computes clamp(features, min, max)

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosaclamp-mlirtosaclampop)
    """

    name = "tosa.clamp"

    T: ClassVar[VarConstraint] = VarConstraint("T", AnyAttr())
    VALUE: ClassVar[AttrConstraint] = IntegerAttr.constr(
        type=SignlessIntegerConstraint & T
    ) | FloatAttr.constr(type=AnyFloatConstr & T)

    min_val = prop_def(VALUE)
    max_val = prop_def(VALUE)

    nan_mode = opt_prop_def(NanModeAttr, default_value=NanModeAttr(NanMode.PROPAGATE))

    input = operand_def(TensorType.constr(T))
    output = result_def(TensorType.constr(T))

    irdl_options = (ParsePropInAttrDict(),)

    assembly_format = "$input attr-dict `:` `(` type($input) `)` `->` type($output)"

name = 'tosa.clamp' class-attribute instance-attribute

T: VarConstraint = VarConstraint('T', AnyAttr()) class-attribute

VALUE: AttrConstraint = IntegerAttr.constr(type=(SignlessIntegerConstraint & T)) | FloatAttr.constr(type=(AnyFloatConstr & T)) class-attribute

min_val = prop_def(VALUE) class-attribute instance-attribute

max_val = prop_def(VALUE) class-attribute instance-attribute

nan_mode = opt_prop_def(NanModeAttr, default_value=(NanModeAttr(NanMode.PROPAGATE))) class-attribute instance-attribute

input = operand_def(TensorType.constr(T)) class-attribute instance-attribute

output = result_def(TensorType.constr(T)) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

assembly_format = '$input attr-dict `:` `(` type($input) `)` `->` type($output)' class-attribute instance-attribute

ConstOp

Bases: IRDLOperation

TOSA const operation.

See external documentation.

Source code in xdsl/dialects/tosa.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
@irdl_op_definition
class ConstOp(IRDLOperation):
    """
    TOSA const operation.

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosaconst-mlirtosaconstop).
    """

    name = "tosa.const"

    values = prop_def(DenseIntOrFPElementsAttr)

    output = result_def(TensorType)

    def __init__(self, values: DenseIntOrFPElementsAttr):
        super().__init__(
            properties={"values": values}, result_types=(values.get_type(),)
        )

name = 'tosa.const' class-attribute instance-attribute

values = prop_def(DenseIntOrFPElementsAttr) class-attribute instance-attribute

output = result_def(TensorType) class-attribute instance-attribute

__init__(values: DenseIntOrFPElementsAttr)

Source code in xdsl/dialects/tosa.py
183
184
185
186
def __init__(self, values: DenseIntOrFPElementsAttr):
    super().__init__(
        properties={"values": values}, result_types=(values.get_type(),)
    )

RescaleOp dataclass

Bases: IRDLOperation

Tosa Rescale Operator

See external documentation

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class RescaleOp(IRDLOperation):
    """
    Tosa Rescale Operator

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosarescale-mlirtosarescaleop)
    """

    name = "tosa.rescale"

    scale32 = prop_def(BoolAttr)
    rounding_mode = prop_def(
        RoundingModeAttr, default_value=RoundingModeAttr(RoundingMode.SINGLE_ROUND)
    )
    per_channel = prop_def(BoolAttr)
    input_unsigned = prop_def(BoolAttr)
    output_unsigned = prop_def(BoolAttr)

    input = operand_def(TensorType)
    multiplier = operand_def(TensorType)
    shift = operand_def(TensorType)
    input_zp = operand_def(TensorType)
    output_zp = operand_def(TensorType)

    output = result_def(TensorType)

    irdl_options = (ParsePropInAttrDict(),)

    def print(self, printer: Printer):
        # print operands
        printer.print_string(" ")
        printer.print_list(self.operands, lambda op: printer.print_ssa_value(op))

        # print attr-dict
        printer.print_string(" ")
        with printer.in_braces():

            def print_attr_entry(arg: tuple[str, Attribute]):
                k, v = arg
                match k:
                    case "rounding_mode":
                        printer.print_string("rounding_mode = ")
                        rounding_mode = cast(RoundingModeAttr, v)
                        printer.print_string(rounding_mode.data)
                    case _:
                        printer.print_identifier_or_string_literal(k)
                        printer.print_string(" = ")
                        printer.print_attribute(v)

            printer.print_list(self.properties.items(), print_attr_entry)

        # print types
        printer.print_string(" : ")
        with printer.in_parens():
            printer.print_list(
                self.operand_types, lambda ty: printer.print_attribute(ty)
            )

        printer.print_string(" -> ")
        printer.print_attribute(self.result_types[0])

    @classmethod
    def parse(cls, parser: Parser):
        # parse operands
        operands: list[SSAValue] = []

        def parse_arg():
            operands.append(parser.parse_operand())

        parser.parse_comma_separated_list(parser.Delimiter.NONE, parse_arg)

        # parse attr-dict
        properties: Mapping[str, Attribute | None] = {}

        def parse_attribute_entry():
            key = parser.parse_identifier()
            parser.parse_punctuation("=")
            match key:
                case "rounding_mode":
                    rounding_mode = parser.parse_identifier()
                    val = RoundingModeAttr(RoundingMode(rounding_mode))
                case _:
                    val = parser.parse_attribute()
            properties[key] = val

        parser.parse_comma_separated_list(
            parser.Delimiter.BRACES, parse_attribute_entry
        )

        # parse results
        operand_types: list[Attribute] = []

        def parse_operand_type():
            operand_types.append(parser.parse_attribute())

        parser.parse_punctuation(":")
        parser.parse_comma_separated_list(parser.Delimiter.PAREN, parse_operand_type)
        parser.parse_punctuation("->")

        result_type = parser.parse_attribute()

        return cls(
            operands=operands,
            result_types=[result_type],
            regions=[],
            properties=properties,
        )

name = 'tosa.rescale' class-attribute instance-attribute

scale32 = prop_def(BoolAttr) class-attribute instance-attribute

rounding_mode = prop_def(RoundingModeAttr, default_value=(RoundingModeAttr(RoundingMode.SINGLE_ROUND))) class-attribute instance-attribute

per_channel = prop_def(BoolAttr) class-attribute instance-attribute

input_unsigned = prop_def(BoolAttr) class-attribute instance-attribute

output_unsigned = prop_def(BoolAttr) class-attribute instance-attribute

input = operand_def(TensorType) class-attribute instance-attribute

multiplier = operand_def(TensorType) class-attribute instance-attribute

shift = operand_def(TensorType) class-attribute instance-attribute

input_zp = operand_def(TensorType) class-attribute instance-attribute

output_zp = operand_def(TensorType) class-attribute instance-attribute

output = result_def(TensorType) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

print(printer: Printer)

Source code in xdsl/dialects/tosa.py
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
def print(self, printer: Printer):
    # print operands
    printer.print_string(" ")
    printer.print_list(self.operands, lambda op: printer.print_ssa_value(op))

    # print attr-dict
    printer.print_string(" ")
    with printer.in_braces():

        def print_attr_entry(arg: tuple[str, Attribute]):
            k, v = arg
            match k:
                case "rounding_mode":
                    printer.print_string("rounding_mode = ")
                    rounding_mode = cast(RoundingModeAttr, v)
                    printer.print_string(rounding_mode.data)
                case _:
                    printer.print_identifier_or_string_literal(k)
                    printer.print_string(" = ")
                    printer.print_attribute(v)

        printer.print_list(self.properties.items(), print_attr_entry)

    # print types
    printer.print_string(" : ")
    with printer.in_parens():
        printer.print_list(
            self.operand_types, lambda ty: printer.print_attribute(ty)
        )

    printer.print_string(" -> ")
    printer.print_attribute(self.result_types[0])

parse(parser: Parser) classmethod

Source code in xdsl/dialects/tosa.py
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
@classmethod
def parse(cls, parser: Parser):
    # parse operands
    operands: list[SSAValue] = []

    def parse_arg():
        operands.append(parser.parse_operand())

    parser.parse_comma_separated_list(parser.Delimiter.NONE, parse_arg)

    # parse attr-dict
    properties: Mapping[str, Attribute | None] = {}

    def parse_attribute_entry():
        key = parser.parse_identifier()
        parser.parse_punctuation("=")
        match key:
            case "rounding_mode":
                rounding_mode = parser.parse_identifier()
                val = RoundingModeAttr(RoundingMode(rounding_mode))
            case _:
                val = parser.parse_attribute()
        properties[key] = val

    parser.parse_comma_separated_list(
        parser.Delimiter.BRACES, parse_attribute_entry
    )

    # parse results
    operand_types: list[Attribute] = []

    def parse_operand_type():
        operand_types.append(parser.parse_attribute())

    parser.parse_punctuation(":")
    parser.parse_comma_separated_list(parser.Delimiter.PAREN, parse_operand_type)
    parser.parse_punctuation("->")

    result_type = parser.parse_attribute()

    return cls(
        operands=operands,
        result_types=[result_type],
        regions=[],
        properties=properties,
    )

ElementwiseOperation dataclass

Bases: IRDLOperation, ABC

Abstract superclass for elementwise TOSA operations

Source code in xdsl/dialects/tosa.py
298
299
300
301
302
303
304
305
306
307
class ElementwiseOperation(IRDLOperation, ABC):
    """
    Abstract superclass for elementwise TOSA operations
    """

    assembly_format = "operands attr-dict `:` functional-type(operands, results)"

    traits = traits_def(
        Pure(),
    )

assembly_format = 'operands attr-dict `:` functional-type(operands, results)' class-attribute instance-attribute

traits = traits_def(Pure()) class-attribute instance-attribute

ElementwiseBinaryOperation dataclass

Bases: ElementwiseOperation

Abstract superclass for elementwise, binary TOSA operations.

Source code in xdsl/dialects/tosa.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
class ElementwiseBinaryOperation(ElementwiseOperation):
    """
    Abstract superclass for elementwise, binary TOSA operations.
    """

    T: ClassVar = VarConstraint("T", AnyAttr())

    input1 = operand_def(TensorType.constr(T))
    input2 = operand_def(TensorType.constr(T))
    output = result_def(TensorType.constr(T))

    def verify_(self) -> None:
        t1 = self.input1.type
        t2 = self.input2.type
        t_out = self.output.type

        if not are_tosa_broadcastable(t1, t2, t_out):
            raise VerifyException(
                f"'{type(self).name}' Operand and result tensor shapes are not compatible"
            )

T: ClassVar = VarConstraint('T', AnyAttr()) class-attribute instance-attribute

input1 = operand_def(TensorType.constr(T)) class-attribute instance-attribute

input2 = operand_def(TensorType.constr(T)) class-attribute instance-attribute

output = result_def(TensorType.constr(T)) class-attribute instance-attribute

verify_() -> None

Source code in xdsl/dialects/tosa.py
321
322
323
324
325
326
327
328
329
def verify_(self) -> None:
    t1 = self.input1.type
    t2 = self.input2.type
    t_out = self.output.type

    if not are_tosa_broadcastable(t1, t2, t_out):
        raise VerifyException(
            f"'{type(self).name}' Operand and result tensor shapes are not compatible"
        )

AddOp dataclass

Bases: ElementwiseBinaryOperation

Tosa elementwise add operation

See external documentation

Source code in xdsl/dialects/tosa.py
332
333
334
335
336
337
338
339
340
341
342
343
344
@irdl_op_definition
class AddOp(ElementwiseBinaryOperation):
    """
    Tosa elementwise add operation

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosaadd-mlirtosaaddop)
    """

    name = "tosa.add"

    traits = traits_def(
        Commutative(),
    )

name = 'tosa.add' class-attribute instance-attribute

traits = traits_def(Commutative()) class-attribute instance-attribute

SubOp dataclass

Bases: ElementwiseBinaryOperation

Tosa elementwise subtraction operation

See external documentation

Source code in xdsl/dialects/tosa.py
347
348
349
350
351
352
353
354
355
@irdl_op_definition
class SubOp(ElementwiseBinaryOperation):
    """
    Tosa elementwise subtraction operation

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosasub-mlirtosasubop)
    """

    name = "tosa.sub"

name = 'tosa.sub' class-attribute instance-attribute

MulOp dataclass

Bases: ElementwiseOperation

Tosa elementwise multiplication operation (Hadamard product)

See external documentation

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class MulOp(ElementwiseOperation):
    """
    Tosa elementwise multiplication operation (Hadamard product)

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosamul-mlirtosamulop)
    """

    name = "tosa.mul"

    traits = traits_def(
        Commutative(),
    )

    T: ClassVar = VarConstraint("T", AnyAttr())

    input1 = operand_def(TensorType.constr(T))
    input2 = operand_def(TensorType.constr(T))
    shift = operand_def(TensorType[I8])
    output = result_def(TensorType.constr(T))

    def verify_(self) -> None:
        t1 = self.input1.type
        t2 = self.input2.type
        t_out = self.output.type

        if not are_tosa_broadcastable(t1, t2, t_out):
            raise VerifyException(
                f"'{type(self).name}' Operand and result tensor shapes are not compatible"
            )

name = 'tosa.mul' class-attribute instance-attribute

traits = traits_def(Commutative()) class-attribute instance-attribute

T: ClassVar = VarConstraint('T', AnyAttr()) class-attribute instance-attribute

input1 = operand_def(TensorType.constr(T)) class-attribute instance-attribute

input2 = operand_def(TensorType.constr(T)) class-attribute instance-attribute

shift = operand_def(TensorType[I8]) class-attribute instance-attribute

output = result_def(TensorType.constr(T)) class-attribute instance-attribute

verify_() -> None

Source code in xdsl/dialects/tosa.py
379
380
381
382
383
384
385
386
387
def verify_(self) -> None:
    t1 = self.input1.type
    t2 = self.input2.type
    t_out = self.output.type

    if not are_tosa_broadcastable(t1, t2, t_out):
        raise VerifyException(
            f"'{type(self).name}' Operand and result tensor shapes are not compatible"
        )

ElementwiseUnaryOperation dataclass

Bases: ElementwiseOperation, Generic[TInv]

Abstract base class for elementwise unary operations on tensors of floating-point types

Source code in xdsl/dialects/tosa.py
393
394
395
396
397
398
399
class ElementwiseUnaryOperation(ElementwiseOperation, Generic[TInv]):
    """
    Abstract base class for elementwise unary operations on tensors of floating-point types
    """

    input1 = operand_def(TInv)
    result = result_def(TInv)

input1 = operand_def(TInv) class-attribute instance-attribute

result = result_def(TInv) class-attribute instance-attribute

SinOp dataclass

Bases: ElementwiseUnaryOperation[TensorType[AnyFloat]]

TOSA dialect operation computing sin(x) for each element in a tensor

See external documentation

Source code in xdsl/dialects/tosa.py
402
403
404
405
406
407
408
409
410
@irdl_op_definition
class SinOp(ElementwiseUnaryOperation[TensorType[AnyFloat]]):
    """
    TOSA dialect operation computing sin(x) for each element in a tensor

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosasin-mlirtosasinop)
    """

    name = "tosa.sin"

name = 'tosa.sin' class-attribute instance-attribute

CosOp dataclass

Bases: ElementwiseUnaryOperation[TensorType[AnyFloat]]

TOSA dialect operation computing cos(x) for each element in a tensor

See external documentation

Source code in xdsl/dialects/tosa.py
413
414
415
416
417
418
419
420
421
@irdl_op_definition
class CosOp(ElementwiseUnaryOperation[TensorType[AnyFloat]]):
    """
    TOSA dialect operation computing cos(x) for each element in a tensor

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosacos-mlirtosacosop)
    """

    name = "tosa.cos"

name = 'tosa.cos' class-attribute instance-attribute

ReciprocalOp dataclass

Bases: ElementwiseUnaryOperation[TensorType]

Elementwise reciprocal operation.

See external documentation.

Source code in xdsl/dialects/tosa.py
424
425
426
427
428
429
430
431
432
@irdl_op_definition
class ReciprocalOp(ElementwiseUnaryOperation[TensorType]):
    """
    Elementwise reciprocal operation.

    See [external documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosareciprocal-mlirtosareciprocalop).
    """

    name = "tosa.reciprocal"

name = 'tosa.reciprocal' class-attribute instance-attribute

MatMulOp dataclass

Bases: IRDLOperation

TOSA dialect operation for computing 2D matmuls. Expects 3D tensors as input with leading rank of 1 element, e.g.

tensor<1x14x19xf32> * tensor<1x19x28xf32> -> tensor<1x14x28xf32>

The operands a_zp and b_zp are the zero-point which are used for quantized operations, can be set to 0.0 for no effect.

See external documentation.

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class MatMulOp(IRDLOperation):
    """
    TOSA dialect operation for computing 2D matmuls. Expects 3D tensors as input with leading rank of 1 element, e.g.

    `tensor<1x14x19xf32> * tensor<1x19x28xf32> -> tensor<1x14x28xf32>`

    The operands `a_zp` and `b_zp` are the zero-point which are used for quantized operations, can be set to 0.0 for no effect.

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosamul-mlirtosamulop).
    """

    name = "tosa.matmul"

    T: ClassVar = VarConstraint("T", AnyAttr())

    a = operand_def(TensorType.constr(T))
    b = operand_def(TensorType.constr(T))

    a_zp = operand_def(TensorType.constr(T))
    b_zp = operand_def(TensorType.constr(T))

    output = result_def(TensorType.constr(T))

    assembly_format = "operands attr-dict `:` functional-type(operands, results)"

    traits = traits_def(
        Pure(),
    )

    def verify_(self) -> None:
        assert isinstance(self.a.type, ShapedType)
        assert isinstance(self.b.type, ShapedType)

        assert isinstance(self.a_zp.type, ShapedType)
        assert isinstance(self.b_zp.type, ShapedType)

        sa = self.a.type.get_shape()
        sb = self.b.type.get_shape()

        s_az = self.a_zp.type.get_shape()
        s_bz = self.b_zp.type.get_shape()

        if len(sa) != 3 or len(sb) != 3:
            raise VerifyException("'tosa.matmul' Expected operand tensors of rank 3")

        if sa[0] != 1 or sb[0] != 1:
            raise VerifyException(
                "'tosa.matmul' Expected leading dimension of input tensors to be 1"
            )

        # expect m x n ... n x k
        if sa[2] != sb[1]:
            raise VerifyException(
                "'tosa.matmul' Incompatible shapes for performing matrix multiplication"
            )

        # check that zero-points are unranked or scalar
        if len(s_az) not in [0, 1] or len(s_bz) not in [0, 1]:
            raise VerifyException(
                "'tosa.matmul' Expected zero-point operands to be unranked or scalar tensors"
            )

name = 'tosa.matmul' class-attribute instance-attribute

T: ClassVar = VarConstraint('T', AnyAttr()) class-attribute instance-attribute

a = operand_def(TensorType.constr(T)) class-attribute instance-attribute

b = operand_def(TensorType.constr(T)) class-attribute instance-attribute

a_zp = operand_def(TensorType.constr(T)) class-attribute instance-attribute

b_zp = operand_def(TensorType.constr(T)) class-attribute instance-attribute

output = result_def(TensorType.constr(T)) class-attribute instance-attribute

assembly_format = 'operands attr-dict `:` functional-type(operands, results)' class-attribute instance-attribute

traits = traits_def(Pure()) class-attribute instance-attribute

verify_() -> None

Source code in xdsl/dialects/tosa.py
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
def verify_(self) -> None:
    assert isinstance(self.a.type, ShapedType)
    assert isinstance(self.b.type, ShapedType)

    assert isinstance(self.a_zp.type, ShapedType)
    assert isinstance(self.b_zp.type, ShapedType)

    sa = self.a.type.get_shape()
    sb = self.b.type.get_shape()

    s_az = self.a_zp.type.get_shape()
    s_bz = self.b_zp.type.get_shape()

    if len(sa) != 3 or len(sb) != 3:
        raise VerifyException("'tosa.matmul' Expected operand tensors of rank 3")

    if sa[0] != 1 or sb[0] != 1:
        raise VerifyException(
            "'tosa.matmul' Expected leading dimension of input tensors to be 1"
        )

    # expect m x n ... n x k
    if sa[2] != sb[1]:
        raise VerifyException(
            "'tosa.matmul' Incompatible shapes for performing matrix multiplication"
        )

    # check that zero-points are unranked or scalar
    if len(s_az) not in [0, 1] or len(s_bz) not in [0, 1]:
        raise VerifyException(
            "'tosa.matmul' Expected zero-point operands to be unranked or scalar tensors"
        )

MaxPool2DOp dataclass

Bases: IRDLOperation

TOSA dialect operation for performing 2D max pooling on a tensor.

See external documentation.

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class MaxPool2DOp(IRDLOperation):
    """
    TOSA dialect operation for performing 2D max pooling on a tensor.

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosamax_pool2d-mlirtosamaxpool2dop).
    """

    name = "tosa.max_pool2d"

    T: ClassVar = VarConstraint("T", AnyAttr())
    input = operand_def(TensorType.constr(T))
    output = result_def(TensorType.constr(T))

    kernel = prop_def(DenseArrayBase[I64])
    stride = prop_def(DenseArrayBase[I64])
    pad = prop_def(DenseArrayBase[I64])
    nan_mode = opt_prop_def(NanModeAttr, default_value=NanModeAttr(NanMode.PROPAGATE))

    irdl_options = (ParsePropInAttrDict(),)

    assembly_format = "operands attr-dict `:` functional-type(operands, results)"

    def _verify_(self) -> None:
        assert isinstance(self.input.type, ShapedType)
        assert isinstance(self.output.type, ShapedType)

        input_shape = self.input.type.get_shape()
        output_shape = self.output.type.get_shape()

        if len(input_shape) != 4 or len(output_shape) != 4:
            raise VerifyException(
                "'tosa.max_pool2d' Expected input and output tensors to be rank 4"
            )

name = 'tosa.max_pool2d' class-attribute instance-attribute

T: ClassVar = VarConstraint('T', AnyAttr()) class-attribute instance-attribute

input = operand_def(TensorType.constr(T)) class-attribute instance-attribute

output = result_def(TensorType.constr(T)) class-attribute instance-attribute

kernel = prop_def(DenseArrayBase[I64]) class-attribute instance-attribute

stride = prop_def(DenseArrayBase[I64]) class-attribute instance-attribute

pad = prop_def(DenseArrayBase[I64]) class-attribute instance-attribute

nan_mode = opt_prop_def(NanModeAttr, default_value=(NanModeAttr(NanMode.PROPAGATE))) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

assembly_format = 'operands attr-dict `:` functional-type(operands, results)' class-attribute instance-attribute

AvgPool2DOp dataclass

Bases: IRDLOperation

TOSA dialect operation for performing 2D average pooling on a tensor.

See external documentation.

Source code in xdsl/dialects/tosa.py
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
@irdl_op_definition
class AvgPool2DOp(IRDLOperation):
    """
    TOSA dialect operation for performing 2D average pooling on a tensor.

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosaavg_pool2d-mlirtosaavgpool2dop).
    """

    name = "tosa.avg_pool2d"

    input = operand_def(TensorType)
    input_zp = operand_def(TensorType)
    output_zp = operand_def(TensorType)

    kernel = prop_def(DenseArrayBase)
    stride = prop_def(DenseArrayBase)
    pad = prop_def(DenseArrayBase)
    acc_type = prop_def(TypeAttribute)

    output = result_def(TensorType)

    irdl_options = (ParsePropInAttrDict(),)

    assembly_format = "operands attr-dict `:` functional-type(operands, results)"

name = 'tosa.avg_pool2d' class-attribute instance-attribute

input = operand_def(TensorType) class-attribute instance-attribute

input_zp = operand_def(TensorType) class-attribute instance-attribute

output_zp = operand_def(TensorType) class-attribute instance-attribute

kernel = prop_def(DenseArrayBase) class-attribute instance-attribute

stride = prop_def(DenseArrayBase) class-attribute instance-attribute

pad = prop_def(DenseArrayBase) class-attribute instance-attribute

acc_type = prop_def(TypeAttribute) class-attribute instance-attribute

output = result_def(TensorType) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

assembly_format = 'operands attr-dict `:` functional-type(operands, results)' class-attribute instance-attribute

ConcatOp

Bases: IRDLOperation

TOSA dialect operation for concatenating tensors.

See external documentation.

Source code in xdsl/dialects/tosa.py
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
@irdl_op_definition
class ConcatOp(IRDLOperation):
    """
    TOSA dialect operation for concatenating tensors.

    See external [documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosaconcat-mlirtosaconcatop).
    """

    name = "tosa.concat"

    tensors = var_operand_def(TensorType)
    axis = prop_def(IntegerAttr[I32])
    output = result_def(TensorType)

    def __init__(
        self, tensors: Sequence[SSAValue], axis: IntegerAttr, output_type: TensorType
    ):
        super().__init__(
            operands=[tensors],
            properties={"axis": axis},
            result_types=[output_type],
        )

    irdl_options = (ParsePropInAttrDict(),)

    assembly_format = "$tensors attr-dict `:` `(` type($tensors) `)` `->` type($output)"

name = 'tosa.concat' class-attribute instance-attribute

tensors = var_operand_def(TensorType) class-attribute instance-attribute

axis = prop_def(IntegerAttr[I32]) class-attribute instance-attribute

output = result_def(TensorType) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

assembly_format = '$tensors attr-dict `:` `(` type($tensors) `)` `->` type($output)' class-attribute instance-attribute

__init__(tensors: Sequence[SSAValue], axis: IntegerAttr, output_type: TensorType)

Source code in xdsl/dialects/tosa.py
575
576
577
578
579
580
581
582
def __init__(
    self, tensors: Sequence[SSAValue], axis: IntegerAttr, output_type: TensorType
):
    super().__init__(
        operands=[tensors],
        properties={"axis": axis},
        result_types=[output_type],
    )

YieldOp dataclass

Bases: IRDLOperation

TOSA operation for returning out of conditional and body of structured control flow

See external documentation

Source code in xdsl/dialects/tosa.py
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
@irdl_op_definition
class YieldOp(IRDLOperation):
    """
    TOSA operation for returning out of conditional and body of structured control flow

    See [external documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosayield-mlirtosayieldop)
    """

    name = "tosa.yield"

    inputs = var_operand_def(TensorType)

    traits = lazy_traits_def(
        lambda: (
            IsTerminator(),
            HasParent(IfOp),
            Pure(),
        )
    )

    assembly_format = "$inputs attr-dict `:` type($inputs)"

name = 'tosa.yield' class-attribute instance-attribute

inputs = var_operand_def(TensorType) class-attribute instance-attribute

traits = lazy_traits_def(lambda: (IsTerminator(), HasParent(IfOp), Pure())) class-attribute instance-attribute

assembly_format = '$inputs attr-dict `:` type($inputs)' class-attribute instance-attribute

IfOp dataclass

Bases: IRDLOperation

Conditional operation on tensors.

See external documentation.

Source code in xdsl/dialects/tosa.py
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
@irdl_op_definition
class IfOp(IRDLOperation):
    """
    Conditional operation on tensors.

    See [external documentation](https://mlir.llvm.org/docs/Dialects/TOSA/#tosacond_if-mlirtosaifop).
    """

    name = "tosa.cond_if"

    cond = operand_def(TensorType(i1, []))

    output = var_result_def(TensorType)

    true_region = region_def("single_block")
    false_region = region_def("single_block")

    traits = traits_def(
        RecursiveMemoryEffect(),
        SingleBlockImplicitTerminator(YieldOp),
    )

    assembly_format = "$cond `:` type($cond) `->` `(` type($output) `)` $true_region `else` $false_region attr-dict"

name = 'tosa.cond_if' class-attribute instance-attribute

cond = operand_def(TensorType(i1, [])) class-attribute instance-attribute

output = var_result_def(TensorType) class-attribute instance-attribute

true_region = region_def('single_block') class-attribute instance-attribute

false_region = region_def('single_block') class-attribute instance-attribute

traits = traits_def(RecursiveMemoryEffect(), SingleBlockImplicitTerminator(YieldOp)) class-attribute instance-attribute

assembly_format = '$cond `:` type($cond) `->` `(` type($output) `)` $true_region `else` $false_region attr-dict' class-attribute instance-attribute

ReductionOperation dataclass

Bases: IRDLOperation, ABC

Base class for all TOSA reduction operations

Source code in xdsl/dialects/tosa.py
642
643
644
645
646
647
648
649
650
651
652
653
654
class ReductionOperation(IRDLOperation, ABC):
    """
    Base class for all TOSA reduction operations
    """

    input = operand_def(TensorType)
    axis = prop_def(IntegerAttr[I32])

    output = result_def(TensorType)

    assembly_format = "$input attr-dict `:` functional-type(operands, results)"

    irdl_options = (ParsePropInAttrDict(),)

input = operand_def(TensorType) class-attribute instance-attribute

axis = prop_def(IntegerAttr[I32]) class-attribute instance-attribute

output = result_def(TensorType) class-attribute instance-attribute

assembly_format = '$input attr-dict `:` functional-type(operands, results)' class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

ReduceAllOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis with a logical AND operation

Source code in xdsl/dialects/tosa.py
657
658
659
660
661
662
663
@irdl_op_definition
class ReduceAllOp(ReductionOperation):
    """
    Reduce a tensor along the given axis with a logical AND operation
    """

    name = "tosa.reduce_all"

name = 'tosa.reduce_all' class-attribute instance-attribute

ReduceAnyOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis with a logical OR operation

Source code in xdsl/dialects/tosa.py
666
667
668
669
670
671
672
@irdl_op_definition
class ReduceAnyOp(ReductionOperation):
    """
    Reduce a tensor along the given axis with a logical OR operation
    """

    name = "tosa.reduce_any"

name = 'tosa.reduce_any' class-attribute instance-attribute

ReduceMaxOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis by taking the maximum value

Source code in xdsl/dialects/tosa.py
675
676
677
678
679
680
681
682
683
@irdl_op_definition
class ReduceMaxOp(ReductionOperation):
    """
    Reduce a tensor along the given axis by taking the maximum value
    """

    name = "tosa.reduce_max"

    nan_mode = opt_prop_def(NanModeAttr, default_value=NanModeAttr(NanMode.PROPAGATE))

name = 'tosa.reduce_max' class-attribute instance-attribute

nan_mode = opt_prop_def(NanModeAttr, default_value=(NanModeAttr(NanMode.PROPAGATE))) class-attribute instance-attribute

ReduceMinOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis by taking the minimum value

Source code in xdsl/dialects/tosa.py
686
687
688
689
690
691
692
693
694
@irdl_op_definition
class ReduceMinOp(ReductionOperation):
    """
    Reduce a tensor along the given axis by taking the minimum value
    """

    name = "tosa.reduce_min"

    nan_mode = opt_prop_def(NanModeAttr, default_value=NanModeAttr(NanMode.PROPAGATE))

name = 'tosa.reduce_min' class-attribute instance-attribute

nan_mode = opt_prop_def(NanModeAttr, default_value=(NanModeAttr(NanMode.PROPAGATE))) class-attribute instance-attribute

ReduceProductOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis by taking the product of all values

Source code in xdsl/dialects/tosa.py
697
698
699
700
701
702
703
@irdl_op_definition
class ReduceProductOp(ReductionOperation):
    """
    Reduce a tensor along the given axis by taking the product of all values
    """

    name = "tosa.reduce_product"

name = 'tosa.reduce_product' class-attribute instance-attribute

ReduceSumOp dataclass

Bases: ReductionOperation

Reduce a tensor along the given axis by taking the product of all values

Source code in xdsl/dialects/tosa.py
706
707
708
709
710
711
712
@irdl_op_definition
class ReduceSumOp(ReductionOperation):
    """
    Reduce a tensor along the given axis by taking the product of all values
    """

    name = "tosa.reduce_sum"

name = 'tosa.reduce_sum' class-attribute instance-attribute

are_tosa_broadcastable(lhs: Attribute, rhs: Attribute, out: Attribute)

Returns True if lhs and rhs have compatible shapes with broadcasting: the dimensions have to match, or one of them must be equal to 1, in which case the corresponding resulting dimension must be equal to the non-1 dimension.

e.g.: [1, 2, 3] & [4, 2, 1] -> [4, 2, 3]

Source code in xdsl/dialects/tosa.py
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
def are_tosa_broadcastable(lhs: Attribute, rhs: Attribute, out: Attribute):
    """
    Returns `True` if lhs and rhs have compatible shapes with broadcasting: the dimensions have to match, or one of them must be
    equal to 1, in which case the corresponding resulting dimension must be equal to the non-1 dimension.

    e.g.: `[1, 2, 3] & [4, 2, 1] -> [4, 2, 3]`
    """
    if (
        not isinstance(lhs, ShapedType)
        or not isinstance(rhs, ShapedType)
        or not isinstance(out, ShapedType)
    ):
        return False

    lhs_shape = lhs.get_shape()
    rhs_shape = rhs.get_shape()
    out_shape = out.get_shape()

    ranks_equal = len(lhs_shape) == len(rhs_shape) == len(out_shape)
    if not ranks_equal:
        return False

    # check that expected dimensions match output dimensions
    # and input dimensions are equal, or broadcast
    return all(
        l == r == o or (l == 1 and r == o) or (r == 1 and l == o)
        for l, r, o in zip(lhs_shape, rhs_shape, out_shape)
    )