//===- QuantUtils.cpp -----------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file contains TOSA numerical support functions and quantization // attribute builders. // //===----------------------------------------------------------------------===// #include "mlir/Dialect/Tosa/Utils/QuantUtils.h" usingnamespacemlir; usingnamespacemlir::tosa; /// From a scale value, generates multiplier and shift values where /// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that /// multiplier = mantissa*2^shift for 16-bit scaling. static void computeMultiplierAndShiftTosaScale16(double scale, int32_t &multiplier, int32_t &shift) { … } /// From a scale value, generates multiplier and shift values where /// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that /// multiplier = mantissa*2^shift for 32-bit scaling. static void computeMultiplierAndShiftTosaScale32(double scale, int32_t &multiplier, int32_t &shift) { … } /// Generates a quantized multiplier/shift from double. void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier, int32_t &shift, int32_t scaleWidth) { … } #define GET_UQTYPE(inputType) … #define GET_QTYPE(inputType) … /// Method to build ConvOpQuantizationAttr, called from /// ConvOpQuantInfoBuilder/TransConvOpQuantInfoBuilder: /// input_zp: input zeropoint /// weight_zp: weight zeropoint. ConvOpQuantizationAttr mlir::tosa::buildConvOpQuantizationAttr(OpBuilder &builder, Value input, Value weight) { … } /// Builds MatMulOpQuantizationAttr, called from /// MatMulOpQuantInfoBuilder: /// aZp: input a zeropoint /// bZp: input b zeropoint. MatMulOpQuantizationAttr mlir::tosa::buildMatMulOpQuantizationAttr(OpBuilder &builder, Value a, Value b) { … } /// Builds UnaryOpQuantizationAttr /// UnaryOpQuantInfoBuilder: /// inputZp: input zeropoint /// outputZp: output zeropoint. UnaryOpQuantizationAttr mlir::tosa::buildUnaryOpQuantizationAttr(OpBuilder &builder, Value input, Type outputRawType) { … } /// Builds PadOpQuantizationAttr, called from PadOpQuantInfoBuilder: /// inputZp: input zeropoint. PadOpQuantizationAttr mlir::tosa::buildPadOpQuantizationAttr(OpBuilder &builder, Value input) { … } /// Builds output type for a quantized ConvOp with the right bitwidth. /// This is called by the builder when dealing with quantized content. Type mlir::tosa::buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input, Value weight) { … } /// Builds Tosa quantization attributes from min/max values. Type mlir::tosa::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. TypeAttr mlir::tosa::buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDtype, Attribute minAttr, Attribute maxAttr, IntegerAttr quantBits, int filterQuantDim, bool isSigned, BoolAttr narrowRange) { … }