24#define GEN_PASS_DEF_NORMALIZEQUANTTYPES
25#include "mlir/Dialect/Quant/Transforms/Passes.h.inc"
35static bool isConvertibleToPerTensor(TensorType tensorType) {
36 return cast<UniformQuantizedSubChannelType>(tensorType.getElementType())
39 .getNumElements() == 1;
48static bool isConvertibleToPerAxis(TensorType tensorType) {
49 auto shape = cast<UniformQuantizedSubChannelType>(tensorType.getElementType())
53 return llvm::count_if(shape, [](
int64_t dim) {
return dim != 1; }) == 1;
60 static Type convertType(Type type) {
61 auto tensorType = dyn_cast<TensorType>(type);
67 dyn_cast<UniformQuantizedSubChannelType>(tensorType.getElementType());
68 if (!subChannelType) {
72 if (isConvertibleToPerTensor(tensorType)) {
74 subChannelType.getScales().getValues<APFloat>()[0].convertToDouble();
76 subChannelType.getZeroPoints().getValues<APInt>()[0].getSExtValue();
78 subChannelType.getFlags(), subChannelType.getStorageType(),
79 subChannelType.getExpressedType(), scale, zeroPoint,
80 subChannelType.getStorageTypeMin(),
81 subChannelType.getStorageTypeMax());
82 return tensorType.clone(perTensorType);
85 if (isConvertibleToPerAxis(tensorType)) {
86 auto shape = subChannelType.getScales().getType().getShape();
87 const auto *quantizedDimItr =
88 llvm::find_if(shape, [](
int64_t dim) {
return dim != 1; });
89 auto scales = llvm::to_vector(llvm::map_range(
90 subChannelType.getScales().getValues<APFloat>(),
91 [](
const APFloat &scale) { return scale.convertToDouble(); }));
92 auto zeroPoints = llvm::to_vector(llvm::map_range(
93 subChannelType.getZeroPoints().getValues<APInt>(),
94 [](
const APInt &zeroPoint) { return zeroPoint.getSExtValue(); }));
96 subChannelType.getFlags(), subChannelType.getStorageType(),
97 subChannelType.getExpressedType(), scales, zeroPoints,
98 quantizedDimItr - shape.begin(), subChannelType.getStorageTypeMin(),
99 subChannelType.getStorageTypeMax());
100 return tensorType.clone(perAxisType);
106 explicit NormalizedQuantTypesConverter() { addConversion(convertType); }
120 ConversionPatternRewriter &rewriter)
const final {
122 if (failed(typeConverter->convertTypes(op->getResultTypes(), resultTypes)))
126 op->getLoc(), op->getName(), resultTypes, operands, op->getAttrs(),
127 op->getPropertiesStorage(), op->getSuccessors(), op->getNumRegions());
128 for (
auto regions : llvm::zip(op->getRegions(), newOp->getRegions())) {
130 Region &parent = std::get<1>(regions);
131 rewriter.inlineRegionBefore(before, parent, parent.
end());
132 if (failed(rewriter.convertRegionTypes(&parent, *typeConverter)))
135 rewriter.insert(newOp);
136 rewriter.replaceOp(op, newOp->getResults());
142class NormalizeQuantTypes
149 NormalizedQuantTypesConverter typeConverter;
153 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
154 return typeConverter.isSignatureLegal(op.getFunctionType()) &&
155 typeConverter.isLegal(&op.getBody());
164 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
166 patterns.add<ConvertGenericOpwithSubChannelType>(typeConverter, context);
170 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.
const FrozenRewritePatternSet & patterns