• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//===-- TosaTypesBase.td - TOSA type definitions -----------*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the type definitions for the TOSA dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef TOSA_TYPES_BASE
14#define TOSA_TYPES_BASE
15
16include "mlir/IR/OpBase.td"
17
18//===----------------------------------------------------------------------===//
19// Tosa Type Definitions.
20//===----------------------------------------------------------------------===//
21
22// The base class of a quantized type.
23// Param tuple is: [bitwidth, zeropt, smantissa, sexp, low_end, high_end].
24// Where low and high ends are 0,255 when unsigned, -128,127 when signed, for
25// the 8-bit case.
26class Tosa_QuantizedType<string n, list<int> params, bit signed>
27  : Type<And<[CPred<"$_self.isa<mlir::quant::QuantizedType>()">,
28              CPred<"$_self.cast<mlir::quant::QuantizedType>()" #
29                    ".getStorageTypeIntegralWidth() == " # !head(params)>]>,
30    "Q" # !if (signed, "int", "uint") # !head(params) # " type"> {
31  string name = n;
32  string asTraitArgsStr =
33    StrJoinInt<params>.result # !if(signed, ", true", ", false");
34}
35
36//===----------------------------------------------------------------------===//
37// Non-Quantized Signed Integer Types.
38// Used to express accumulator results or compare results.
39//===----------------------------------------------------------------------===//
40
41def Tosa_Int32 : I<32>;
42def Tosa_Int48 : I<48>;
43def Tosa_Int64 : I<64>;
44
45def Tosa_SignedInt : AnyTypeOf<[Tosa_Int32,
46                                Tosa_Int48,
47                                Tosa_Int64]>;
48
49def Tosa_Bool : I<1>;
50
51// No unsigned unquantized int types.
52def Tosa_Int : AnyTypeOf<[Tosa_Bool,
53                          Tosa_SignedInt]>;
54
55def Tosa_Int32Or64 : AnyTypeOf<[Tosa_Int32,
56                   	        Tosa_Int64]>;
57
58//===----------------------------------------------------------------------===//
59// Quantized Integer Types.
60// Datatype for network feature map or weight content.
61//===----------------------------------------------------------------------===//
62//===----------------------------------------------------------------------===//
63// Name    Symmetry   Grouping                Sign
64//===----------------------------------------------------------------------===//
65// aint8 : asymmetric per tensor,             signed
66// uint8 : asymmetric per tensor ,            unsigned
67// int4  : symmetric  per channel,            signed
68// int8  : symmetric  per tensor/per channel, signed
69// int16 : symmetric  per tensor,             signed
70//===----------------------------------------------------------------------===//
71def Tosa_QuantizedInt	: AnyTypeOf<[Tosa_QuantizedType<"aint8", [8], 1>,
72                                     Tosa_QuantizedType<"uint8", [8], 0>,
73                                     Tosa_QuantizedType<"int4", [4, 0], 1>,
74                                     Tosa_QuantizedType<"int8", [8, 0], 1>,
75                                     Tosa_QuantizedType<"int16", [16, 0], 1>]>;
76
77//===----------------------------------------------------------------------===//
78// Floating-point types.
79//===----------------------------------------------------------------------===//
80def Tosa_Float : AnyTypeOf<[
81                            F32,
82			    F16,
83			    BF16]>;
84
85//===----------------------------------------------------------------------===//
86// Multi-category types.
87//===----------------------------------------------------------------------===//
88def Tosa_AnyNumber : AnyTypeOf<[Tosa_Int, Tosa_QuantizedInt, Tosa_Float],
89                               "number">;
90
91//===----------------------------------------------------------------------===//
92// Tensor types
93//===----------------------------------------------------------------------===//
94
95def Tosa_Int32Or64Tensor : TensorOf<[Tosa_Int32Or64]>;
96
97def Tosa_Tensor : TensorOf<[Tosa_AnyNumber]>;
98
99// Any tensor element type allowed in Tosa ops.
100def Tosa_ElementType : Type<Or<[Tosa_Int.predicate, Tosa_QuantizedInt.predicate,
101                                Tosa_Float.predicate]>, "tosa.dtype">;
102
103class Tosa_TensorOfOrNone<list<Type> allowedTypes, string description = ""> :
104  AnyTypeOf<[TensorOf<allowedTypes>, NoneType], description>;
105
106//===----------------------------------------------------------------------===//
107// Tensor types with constrained ranks.
108//===----------------------------------------------------------------------===//
109
110// Must be listed rank.
111def Tosa_Tensor1D : 1DTensorOf<[Tosa_AnyNumber]>;
112def Tosa_Tensor2D : 2DTensorOf<[Tosa_AnyNumber]>;
113def Tosa_Tensor4D : 4DTensorOf<[Tosa_AnyNumber]>;
114def Tosa_Tensor5D : TensorRankOf<[Tosa_AnyNumber], [5]>;
115def Tosa_Tensor6D : TensorRankOf<[Tosa_AnyNumber], [6]>;
116
117// Ranked tensors up to given rank.
118def Tosa_Tensor1Dto2D : TensorRankOf<[Tosa_AnyNumber], [1,2]>;
119def Tosa_Tensor1Dto4D : TensorRankOf<[Tosa_AnyNumber], [1,2,3,4]>;
120def Tosa_Tensor1Dto5D : TensorRankOf<[Tosa_AnyNumber], [1,2,3,4,5]>;
121def Tosa_Tensor1Dto6D : TensorRankOf<[Tosa_AnyNumber], [1,2,3,4,5,6]>;
122
123def Tosa_TensorUpto4D : TensorRankOf<[Tosa_AnyNumber], [0,1,2,3,4]>;
124def Tosa_TensorUpto6D : TensorRankOf<[Tosa_AnyNumber], [0,1,2,3,4,5,6]>;
125
126//===----------------------------------------------------------------------===//
127// Attribute predicates and classes.
128//===----------------------------------------------------------------------===//
129class ArrayMaxCt<int n> : AttrConstraint<
130    CPred<"$_self.cast<::mlir::ArrayAttr>().size() <= " # n>,
131    "with at least " # n # " elements">;
132
133def Tosa_IntArrayAttr2 : Confined<I64ArrayAttr, [ArrayCount<2>]>;
134def Tosa_IntArrayAttr3 : Confined<I64ArrayAttr, [ArrayCount<3>]>;
135def Tosa_IntArrayAttr4 : Confined<I64ArrayAttr, [ArrayCount<4>]>;
136def Tosa_IntArrayAttr5 : Confined<I64ArrayAttr, [ArrayCount<5>]>;
137def Tosa_IntArrayAttr6 : Confined<I64ArrayAttr, [ArrayCount<6>]>;
138
139def Tosa_IntArrayAttrUpto2 : Confined<I64ArrayAttr, [ArrayMaxCt<2>]>;
140def Tosa_IntArrayAttrUpto4 : Confined<I64ArrayAttr, [ArrayMaxCt<4>]>;
141def Tosa_IntArrayAttrUpto5 : Confined<I64ArrayAttr, [ArrayMaxCt<5>]>;
142
143//===----------------------------------------------------------------------===//
144// Iterable attributes.
145//===----------------------------------------------------------------------===//
146// Supported regimes for tosa.resize.
147def Tosa_ResizeTypeAttr : StringBasedAttr<
148    CPred<"$_self.cast<StringAttr>().getValue() == \"BILINEAR\"  || " #
149          "$_self.cast<StringAttr>().getValue() == \"NEAREST_NEIGHBOR\"">,
150    "Supported resize/upsampling strategies">;
151
152def Tosa_TensorTypeAttr : TypeAttrBase<"TensorType", "Tensor type attribute">;
153
154// Tensor to buffer types.
155def Tosa_Buffer : MemRefOf<[Tosa_AnyNumber]>;
156def Tosa_TupleBuffer : NestedTupleOf<[Tosa_Buffer]>;
157def Tosa_BufOrTuple : AnyTypeOf<[Tosa_Buffer, Tosa_TupleBuffer]>;
158
159#endif // TOSA_TYPES_BASE
160