20#include "llvm/ADT/SmallVectorExtras.h"
25#define GEN_PASS_DEF_NORMALIZEQUANTTYPES
26#include "mlir/Dialect/Quant/Transforms/Passes.h.inc"
36static bool isConvertibleToPerTensor(TensorType tensorType) {
37 return cast<UniformQuantizedSubChannelType>(tensorType.getElementType())
40 .getNumElements() == 1;
49static bool isConvertibleToPerAxis(TensorType tensorType) {
50 auto shape = cast<UniformQuantizedSubChannelType>(tensorType.getElementType())
54 return llvm::count_if(shape, [](
int64_t dim) {
return dim != 1; }) == 1;
61 static Type convertType(Type type) {
62 auto tensorType = dyn_cast<TensorType>(type);
68 dyn_cast<UniformQuantizedSubChannelType>(tensorType.getElementType());
69 if (!subChannelType) {
73 if (isConvertibleToPerTensor(tensorType)) {
75 subChannelType.getScales().getValues<APFloat>()[0].convertToDouble();
77 subChannelType.getZeroPoints().getValues<APInt>()[0].getSExtValue();
79 subChannelType.getFlags(), subChannelType.getStorageType(),
80 subChannelType.getExpressedType(), scale, zeroPoint,
81 subChannelType.getStorageTypeMin(),
82 subChannelType.getStorageTypeMax());
83 return tensorType.clone(perTensorType);
86 if (isConvertibleToPerAxis(tensorType)) {
87 auto shape = subChannelType.getScales().getType().getShape();
88 const auto *quantizedDimItr =
89 llvm::find_if(shape, [](
int64_t dim) {
return dim != 1; });
90 auto scales = llvm::map_to_vector(
91 subChannelType.getScales().getValues<APFloat>(),
92 [](
const APFloat &scale) { return scale.convertToDouble(); });
93 auto zeroPoints = llvm::map_to_vector(
94 subChannelType.getZeroPoints().getValues<APInt>(),
95 [](
const APInt &zeroPoint) { return zeroPoint.getSExtValue(); });
97 subChannelType.getFlags(), subChannelType.getStorageType(),
98 subChannelType.getExpressedType(), scales, zeroPoints,
99 quantizedDimItr - shape.begin(), subChannelType.getStorageTypeMin(),
100 subChannelType.getStorageTypeMax());
101 return tensorType.clone(perAxisType);
107 explicit NormalizedQuantTypesConverter() { addConversion(convertType); }
113class ConvertGenericOpwithSubChannelType :
public ConversionPattern {
117 : ConversionPattern(typeConverter, MatchAnyOpTypeTag{}, 0, context) {}
121 ConversionPatternRewriter &rewriter)
const final {
123 if (failed(typeConverter->convertTypes(op->getResultTypes(), resultTypes)))
127 op->getLoc(), op->getName(), resultTypes, operands, op->getAttrs(),
128 op->getPropertiesStorage(), op->getSuccessors(), op->getNumRegions());
129 for (
auto regions : llvm::zip(op->getRegions(), newOp->getRegions())) {
130 Region &before = std::get<0>(regions);
131 Region &parent = std::get<1>(regions);
132 rewriter.inlineRegionBefore(before, parent, parent.
end());
133 if (failed(rewriter.convertRegionTypes(&parent, *typeConverter)))
136 rewriter.insert(newOp);
137 rewriter.replaceOp(op, newOp->getResults());
143class NormalizeQuantTypes
150 NormalizedQuantTypesConverter typeConverter;
154 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
155 return typeConverter.isSignatureLegal(op.getFunctionType()) &&
156 typeConverter.isLegal(&op.getBody());
165 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
166 patterns, typeConverter);
167 patterns.add<ConvertGenericOpwithSubChannelType>(typeConverter, context);
171 applyFullConversion(getOperation(),
target, std::move(patterns))))
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, OpaqueProperties properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
operand_type_range getOperandTypes()
result_type_range getResultTypes()
virtual void runOnOperation()=0
The polymorphic API that runs the pass over the currently held operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Include the generated interface declarations.