• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//===-- TosaOpBase.td - TOSA dialect op builders -----------*- 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 common definitions for the TOSA dialect.
10//
11//===----------------------------------------------------------------------===//
12
13
14#ifndef TOSA_OP_BASE
15#define TOSA_OP_BASE
16
17//===----------------------------------------------------------------------===//
18// The TOSA Dialect.
19//===----------------------------------------------------------------------===//
20def Tosa_Dialect : Dialect {
21  let name = "tosa";
22
23  let description = [{
24    The Tensor Operator Set Architecture (TOSA) dialect.
25
26    This dialect implements the TOSA standard described at
27    https://developer.mlplatform.org/w/tosa/ .
28
29    Tensor Operator Set Architecture (TOSA) provides a set of whole-tensor
30    operations commonly employed by Deep Neural Networks. The intent is to
31    enable a variety of implementations running on a diverse range of
32    processors, with the results at the TOSA level consistent across those
33    implementations. Applications or frameworks which target TOSA can therefore
34    be deployed on a wide range of different processors, such as CPUs or GPUs,
35    with defined accuracy and compatibility constraints. Most operators from the
36    common ML frameworks should be expressible in TOSA. It is expected that
37    there will be tools to lower from the ML frameworks into TOSA.
38  }];
39
40  let cppNamespace = "mlir::tosa";
41  let hasConstantMaterializer = 1;
42}
43
44//===----------------------------------------------------------------------===//
45// TOSA Operator Quantization Attributes.
46//===----------------------------------------------------------------------===//
47
48// Quantization attributes used across TOSA operators. Quantization attributes
49// feed numerical precision parameters to the functional implementation of TOSA
50// operators.
51// The functional behavior is defined in the TOSA specification maintained at
52// https://developer.mlplatform.org/w/tosa/ . TOSA leverages MLIR's built in
53// quantization support: https://mlir.llvm.org/docs/Quantization/, and supports
54// uniform quantization. Depending on datatype, asymmetric and symmetric
55// quantization are supported. The types themselves are described in
56// TosaTypesBase.td .
57
58// This quantization attribute expresses numerical behavior of operators where
59// the operator has a numerical relationship between a single input and output.
60// For example: tosa.negate.
61def Tosa_UnaryOpQuantizationAttr : StructAttr<"UnaryOpQuantizationAttr",
62  Tosa_Dialect, [
63    StructFieldAttr<"input_zp",         I32Attr>,
64    StructFieldAttr<"output_zp",        I32Attr>
65    ]> {
66  let description = "Attribute for UnaryOp quantization information.";
67}
68
69// There is no explicit BinaryOpQuantizationAttr for 2-input/1-output ops. In
70// this case, a tosa.rescale is used to express the inputs to the same scale.
71// TODO: Upload WIP legalization document describing this construction by
72// example.
73
74// This quantization attribute holds input and weight zero point. Both the
75// ConvOp and MatMulOp QuantizationAttrs follow a common design semantic where
76// their ownquantization attribute only expresses the numerical behavior at
77// the inputs.
78// The scaling of their accumulator output is done using an explicit
79// tosa.rescale operator that scales the accumulator result to output scale.
80def Tosa_ConvOpQuantizationAttr : StructAttr<"ConvOpQuantizationAttr",
81  Tosa_Dialect, [
82    StructFieldAttr<"input_zp",         I32Attr>,
83    StructFieldAttr<"weight_zp",        I32Attr>
84    ]> {
85  let description = "Attribute for Conv type op quantization information.";
86}
87
88def Tosa_MatMulOpQuantizationAttr : StructAttr<"MatMulOpQuantizationAttr",
89  Tosa_Dialect, [
90    StructFieldAttr<"a_zp",             I32Attr>,
91    StructFieldAttr<"b_zp",             I32Attr>
92    ]> {
93  let description = "Attribute for MatMulOp quantization information.";
94}
95
96// This attribute holds input zero point correction applied to the padding
97// zeros to ensure numerical accuracy in the subsequent TOSA operations.
98// Its functional application is described in the tosa.pad() operator
99// description in the specification.
100def Tosa_PadOpQuantizationAttr : StructAttr<"PadOpQuantizationAttr",
101  Tosa_Dialect, [
102    StructFieldAttr<"input_zp",         I32Attr>
103    ]> {
104  let description = "Attribute for PadOp quantization information.";
105}
106
107//===----------------------------------------------------------------------===//
108// TOSA Operator Quantization Builders.
109//===----------------------------------------------------------------------===//
110
111// This builder is called on all convolution operators except for TransposeConv,
112// which has specialized output shape semantics. The builder also defines the
113// bitwidth of the output given the bit width of the input & weight content.
114def Tosa_ConvOpQuantInfoBuilder : OpBuilderDAG<
115  (ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias,
116       "ArrayAttr":$pad, "ArrayAttr":$stride, "ArrayAttr":$dilation),
117  [{
118    buildConvOpWithQuantInfo($_builder, $_state, outputType,
119                             input, weight, bias,
120                             pad, stride, dilation);
121  }]>;
122
123// Handles tosa.transpose_conv2d which has an outpad and output shape attribute.
124def Tosa_TransConvOpQuantInfoBuilder : OpBuilderDAG<
125  (ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias,
126       "ArrayAttr":$outpad, "ArrayAttr":$stride, "ArrayAttr":$dilation,
127       "ArrayAttr":$outputShape),
128  [{
129    buildTransConvOpWithQuantInfo($_builder, $_state, outputType,
130                                  input, weight, bias,
131                                  outpad, stride, dilation,
132                                  outputShape);
133  }]>;
134
135// The tosa.fully_connected op has its own builder as it does not have
136// strides/dilation/padding.
137def Tosa_FCOpQuantInfoBuilder : OpBuilderDAG<
138  (ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias),
139  [{
140    buildFCOpWithQuantInfo($_builder, $_state, outputType,
141                           input, weight, bias);
142  }]>;
143
144// The tosa.matmul op is also intended to be generated where a fully_connected
145// op must be constructed where the weight is not a constant. In this case,
146// the fully_connected op must be expressed using matmul.
147// TODO: Add link to the leglization document explaining this.
148def Tosa_MatMulOpQuantInfoBuilder : OpBuilderDAG<
149  (ins "Type":$outputType, "Value":$a, "Value":$b),
150  [{
151    buildMatMulOpWithQuantInfo($_builder, $_state, outputType,
152                               a, b);
153  }]>;
154
155// Both the tosa.avg_pool2d and unary ops use the same
156// UnaruOpQuantizationAttr but the avg_pool operator has its own builder as it
157// has additional parameters not part of the unary ops.
158def Tosa_AvgPool2dOpQuantInfoBuilder : OpBuilderDAG<
159  (ins "Type":$outputType, "Value":$input, "ArrayAttr":$kernel,
160       "ArrayAttr":$stride, "ArrayAttr":$pad),
161  [{
162    buildAvgPool2dOpWithQuantInfo($_builder, $_state, outputType,
163                                  input, kernel, stride, pad);
164  }]>;
165
166// This builder is called on single-parameter unary operators that have a scale
167// relationship between their input and output, expressed by the
168// UnaryOpQuantizationAttr.
169def Tosa_UnaryOpQuantInfoBuilder : OpBuilderDAG<
170  (ins "Type":$outputType, "Value":$input),
171  [{
172    buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input);
173  }]>;
174
175// This builder is called on the TOSA pad operator that needs to create its own
176// OptionalAttr quantization_attr parameter to scale the padding values
177// correctly.
178def Tosa_PadOpQuantInfoBuilder : OpBuilderDAG<
179  (ins "Type":$outputType, "Value":$input, "Value":$paddings),
180  [{
181    buildPadOpWithQuantInfo($_builder, $_state, outputType,
182                            input, paddings);
183  }]>;
184
185//===----------------------------------------------------------------------===//
186// TOSA Operator.
187//===----------------------------------------------------------------------===//
188
189class Tosa_Op<string mnemonic, list<OpTrait> traits = []> :
190    Op<Tosa_Dialect, mnemonic, !listconcat(traits, [TosaOpInterface])> {
191}
192
193#endif // TOSA_OP_BASE
194