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 DIALECT_TOSA_UTILS_QUANT_UTILS_H 15 #define DIALECT_TOSA_UTILS_QUANT_UTILS_H 16 17 #include "mlir/Dialect/Tosa/IR/TosaOps.h" 18 19 #include "mlir/Dialect/Quant/FakeQuantSupport.h" 20 #include "mlir/Dialect/Quant/UniformSupport.h" 21 22 namespace mlir { 23 namespace tosa { 24 25 //===----------------------------------------------------------------------===// 26 // Utililty 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 void computeMultiplierAndShift(double scale, int32_t &multiplier, 32 int32_t &shift, int32_t scaleWidth); 33 34 //// Builds ConvOpQuantizationAttr from input and weight. 35 ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder, 36 Value input, Value weight); 37 38 //// Builds MatMulOpQuantizationAttr for MatMul operations from A and B. 39 MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder, 40 Value a, Value b); 41 42 //// Builds UnaryOpQuantizationAttr for unary operations from input values. 43 UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder, 44 Value input, 45 Type outputRawType); 46 47 //// Builds PadOpQuantizationAttr for pad operations from input values. 48 PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder, 49 Value input); 50 51 //// construct ConvOp output type with correct bitwidth based on input/weight 52 /// width. 53 Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input, 54 Value weight); 55 56 /// Builds Tosa quantization attributes from min/max values. 57 Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr, 58 Attribute maxAttr, IntegerAttr quantBits, 59 int filterQuantDim, bool isSigned, 60 BoolAttr narrowRange); 61 62 /// Builds Tosa quantization attributes from min/max values. 63 TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDType, 64 Attribute minAttr, Attribute maxAttr, 65 IntegerAttr quantBits, int filterQuantDim, 66 bool isSigned, BoolAttr narrowRange); 67 68 } // namespace tosa 69 } // namespace mlir 70 71 #endif // DIALECT_TOSA_UTILS_QUANT_UTILS_H 72