MLIR  21.0.0git
QuantUtils.h
Go to the documentation of this file.
1 //===-- QuantUtils.h - TOSA numerical support declarations ------*- C++ -*-===//
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 // Function declarations for TOSA numerical support functions and quantization
10 // attribute builders
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_TOSA_UTILS_QUANTUTILS_H
15 #define MLIR_DIALECT_TOSA_UTILS_QUANTUTILS_H
16 
18 
21 
22 namespace mlir {
23 namespace tosa {
24 
25 //===----------------------------------------------------------------------===//
26 // Utility functions to support quantization handling in Tosa.
27 //===----------------------------------------------------------------------===//
28 
29 /// From a scale value, computes multiplier and shift values
30 /// for 16 or 32-bit scale widths.
31 bool computeMultiplierAndShift(double scale, int32_t &multiplier,
32  int32_t &shift, int32_t scaleWidth);
33 
34 // Return a const value for array of IntType vec
35 template <typename IntType>
37  ArrayRef<IntType> vec) {
38  static_assert(
39  std::is_same<IntType, int8_t>::value ||
40  std::is_same<IntType, int16_t>::value ||
41  std::is_same<IntType, int32_t>::value,
42  "getConstTensorInt only supports int8_t, int16_t, and int32_t types.");
43 
44  int64_t count = vec.size();
45  assert(count > 0 && "Vector must not be empty");
46  auto element_type = builder.getIntegerType(sizeof(IntType) * 8);
47  mlir::RankedTensorType const_type =
48  RankedTensorType::get({count}, element_type);
49  mlir::DenseElementsAttr const_attr = DenseElementsAttr::get(const_type, vec);
50  auto const_op = builder.create<tosa::ConstOp>(loc, const_type, const_attr);
51  return const_op.getResult();
52 }
53 
54 //// Builds ConvOpQuantizationAttr from input and weight.
55 ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder,
56  Value input, Value weight);
57 
58 std::pair<Value, Value> createZPsAsConst(OpBuilder &builder, Value input,
59  Value weight);
60 
61 //// Builds MatMulOpQuantizationAttr for MatMul operations from A and B.
62 MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder,
63  Value a, Value b);
64 
65 //// Builds UnaryOpQuantizationAttr for unary operations from input values.
66 UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder,
67  Value input,
68  Type outputRawType);
69 
70 //// Builds PadOpQuantizationAttr for pad operations from input values.
71 PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder,
72  Value input);
73 
74 //// construct ConvOp output type with correct bitwidth based on input/weight
75 /// width.
76 Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input,
77  Value weight);
78 
79 /// Builds Tosa quantization attributes from min/max values.
80 Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr,
81  Attribute maxAttr, IntegerAttr quantBits,
82  int filterQuantDim, bool isSigned,
83  BoolAttr narrowRange);
84 
85 /// Builds Tosa quantization attributes from min/max values.
86 TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDType,
87  Attribute minAttr, Attribute maxAttr,
88  IntegerAttr quantBits, int filterQuantDim,
89  bool isSigned, BoolAttr narrowRange);
90 
91 } // namespace tosa
92 } // namespace mlir
93 
94 #endif // MLIR_DIALECT_TOSA_UTILS_QUANTUTILS_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:67
An attribute that represents a reference to a dense vector or tensor object.
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class helps build Operations.
Definition: Builders.h:205
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:407
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder, Value input, Value weight)
Method to build ConvOpQuantizationAttr, called from ConvOpQuantInfoBuilder/TransConvOpQuantInfoBuilde...
Definition: QuantUtils.cpp:198
TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr, Attribute maxAttr, IntegerAttr quantBits, int filterQuantDim, bool isSigned, BoolAttr narrowRange)
Builds Tosa quantization attributes from min/max values.
Definition: QuantUtils.cpp:389
Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input, Value weight)
construct ConvOp output type with correct bitwidth based on input/weight width.
Definition: QuantUtils.cpp:289
bool computeMultiplierAndShift(double scale, int32_t &multiplier, int32_t &shift, int32_t scaleWidth)
From a scale value, computes multiplier and shift values for 16 or 32-bit scale widths.
Definition: QuantUtils.cpp:95
Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr, Attribute maxAttr, IntegerAttr quantBits, int filterQuantDim, bool isSigned, BoolAttr narrowRange)
Builds Tosa quantization attributes from min/max values.
Definition: QuantUtils.cpp:321
PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder, Value input)
Builds PadOpQuantizationAttr, called from PadOpQuantInfoBuilder: inputZp: input zeropoint.
Definition: QuantUtils.cpp:269
std::pair< Value, Value > createZPsAsConst(OpBuilder &builder, Value input, Value weight)
Definition: QuantUtils.cpp:162
MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder, Value a, Value b)
Builds MatMulOpQuantizationAttr, called from MatMulOpQuantInfoBuilder: aZp: input a zeropoint bZp: in...
Definition: QuantUtils.cpp:214
Value getConstTensorInt(OpBuilder &builder, Location loc, ArrayRef< IntType > vec)
Definition: QuantUtils.h:36
UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder, Value input, Type outputRawType)
Builds UnaryOpQuantizationAttr UnaryOpQuantInfoBuilder: inputZp: input zeropoint outputZp: output zer...
Definition: QuantUtils.cpp:243
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...