Skip to content

[mlir][tosa] Remove Quantization Attribute #125479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def Tosa_AvgPool2dOp : Tosa_InferShapedTypeOp<"avg_pool2d"> {
Tosa_IntArrayAttr2:$stride,
Tosa_IntArrayAttr4:$pad,
TypeAttrOf<Tosa_AccType>:$acc_type,
OptionalAttr<Tosa_UnaryOpQuantizationAttr>:$quantization_info
OptionalAttr<I32Attr>:$input_zp,
OptionalAttr<I32Attr>:$output_zp
);

let results = (outs
Expand Down Expand Up @@ -237,7 +238,8 @@ def Tosa_FullyConnectedOp : Tosa_InferShapedTypeOp<"fully_connected"> {
Tosa_Tensor2D:$input,
TosaTensorRankOf<[Tosa_Weight], [2]>:$weight,
Tosa_Tensor1D:$bias,
OptionalAttr<Tosa_ConvOpQuantizationAttr>:$quantization_info
OptionalAttr<I32Attr>:$input_zp,
OptionalAttr<I32Attr>:$weight_zp
);

let results = (outs
Expand All @@ -263,7 +265,8 @@ def Tosa_MatMulOp : Tosa_InferShapedTypeOp<"matmul"> {
let arguments = (ins
Tosa_Tensor3D:$a,
Tosa_Tensor3D:$b,
OptionalAttr<Tosa_MatMulOpQuantizationAttr>:$quantization_info
OptionalAttr<I32Attr>:$a_zp,
OptionalAttr<I32Attr>:$b_zp
);

let results = (outs
Expand Down Expand Up @@ -1114,7 +1117,8 @@ def Tosa_NegateOp : Tosa_ElementwiseUnaryOp<"negate"> {

let arguments = (ins
Tosa_Tensor:$input1,
OptionalAttr<Tosa_UnaryOpQuantizationAttr>:$quantization_info
OptionalAttr<I32Attr>:$input1_zp,
OptionalAttr<I32Attr>:$output_zp
);

let results = (outs
Expand Down Expand Up @@ -1589,7 +1593,7 @@ def Tosa_PadOp : Tosa_InferShapedTypeOp<"pad"> {
Tosa_RankedTensor:$input1,
Tosa_Shape:$padding,
Optional<Tosa_ScalarTensor>:$pad_const,
OptionalAttr<Tosa_PadOpQuantizationAttr>:$quantization_info
OptionalAttr<I32Attr>:$input_zp
);

let results = (outs
Expand Down
106 changes: 54 additions & 52 deletions mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,63 +141,65 @@ static Value createLinalgBodyCalculationForElementwiseOp(
}

// tosa::NegateOp
if (isa<tosa::NegateOp>(op) && isa<FloatType>(elementTy))
return rewriter.create<arith::NegFOp>(loc, resultTypes, args);
if (isa<tosa::NegateOp>(op)) {
if (isa<FloatType>(elementTy))
return rewriter.create<arith::NegFOp>(loc, resultTypes, args);

if (isa<tosa::NegateOp>(op) && isa<IntegerType>(elementTy)) {
int64_t inZp = 0, outZp = 0;
if (isa<IntegerType>(elementTy)) {
auto inputZpAttr = cast<tosa::NegateOp>(op).getInput1Zp();
auto outputZpAttr = cast<tosa::NegateOp>(op).getOutputZp();

if (cast<tosa::NegateOp>(op).getQuantizationInfo()) {
auto quantizationInfo = cast<tosa::NegateOp>(op).getQuantizationInfo();
inZp = quantizationInfo.value().getInputZp();
outZp = quantizationInfo.value().getOutputZp();
}
const int64_t inZp = inputZpAttr ? *inputZpAttr : 0;
const int64_t outZp = outputZpAttr ? *outputZpAttr : 0;

int32_t inputBitWidth = elementTy.getIntOrFloatBitWidth();
if (!inZp && !outZp) {
auto constant = rewriter.create<arith::ConstantOp>(
loc, IntegerAttr::get(elementTy, 0));
return rewriter.create<arith::SubIOp>(loc, resultTypes, constant,
args[0]);
}
if (!inZp && !outZp) {
auto constant = rewriter.create<arith::ConstantOp>(
loc, IntegerAttr::get(elementTy, 0));
return rewriter.create<arith::SubIOp>(loc, resultTypes, constant,
args[0]);
}

// Compute the maximum value that can occur in the intermediate buffer.
int64_t zpAdd = inZp + outZp;
int64_t maxValue = APInt::getSignedMaxValue(inputBitWidth).getSExtValue() +
std::abs(zpAdd) + 1;

// Convert that maximum value into the maximum bitwidth needed to represent
// it. We assume 48-bit numbers may be supported further in the pipeline.
int intermediateBitWidth = 64;
if (maxValue <= APInt::getSignedMaxValue(16).getSExtValue()) {
intermediateBitWidth = 16;
} else if (maxValue <= APInt::getSignedMaxValue(32).getSExtValue()) {
intermediateBitWidth = 32;
} else if (maxValue <= APInt::getSignedMaxValue(48).getSExtValue()) {
intermediateBitWidth = 48;
}
// Compute the maximum value that can occur in the intermediate buffer.
const int32_t inputBitWidth = elementTy.getIntOrFloatBitWidth();
const int64_t zpAdd = inZp + outZp;
const int64_t maxValue =
APInt::getSignedMaxValue(inputBitWidth).getSExtValue() +
std::abs(zpAdd) + 1;

// Convert that maximum value into the maximum bitwidth needed to
// represent it. We assume 48-bit numbers may be supported further in
// the pipeline.
int intermediateBitWidth = 64;
if (maxValue <= APInt::getSignedMaxValue(16).getSExtValue()) {
intermediateBitWidth = 16;
} else if (maxValue <= APInt::getSignedMaxValue(32).getSExtValue()) {
intermediateBitWidth = 32;
} else if (maxValue <= APInt::getSignedMaxValue(48).getSExtValue()) {
intermediateBitWidth = 48;
}

Type intermediateType = rewriter.getIntegerType(intermediateBitWidth);
Value zpAddValue = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(intermediateType, zpAdd));

// The negation can be applied by doing:
// outputValue = inZp + outZp - inputValue
auto ext = rewriter.create<arith::ExtSIOp>(loc, intermediateType, args[0]);
auto sub = rewriter.create<arith::SubIOp>(loc, zpAddValue, ext);

// Clamp to the negation range.
Value min = rewriter.create<arith::ConstantIntOp>(
loc, APInt::getSignedMinValue(inputBitWidth).getSExtValue(),
intermediateType);
Value max = rewriter.create<arith::ConstantIntOp>(
loc, APInt::getSignedMaxValue(inputBitWidth).getSExtValue(),
intermediateType);
auto clamp =
clampIntHelper(loc, sub, min, max, rewriter, /*isUnsigned=*/false);

// Truncate to the final value.
return rewriter.create<arith::TruncIOp>(loc, elementTy, clamp);
Type intermediateType = rewriter.getIntegerType(intermediateBitWidth);
Value zpAddValue = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(intermediateType, zpAdd));

// The negation can be applied by doing:
// outputValue = inZp + outZp - inputValue
auto ext =
rewriter.create<arith::ExtSIOp>(loc, intermediateType, args[0]);
auto sub = rewriter.create<arith::SubIOp>(loc, zpAddValue, ext);

// Clamp to the negation range.
Value min = rewriter.create<arith::ConstantIntOp>(
loc, APInt::getSignedMinValue(inputBitWidth).getSExtValue(),
intermediateType);
Value max = rewriter.create<arith::ConstantIntOp>(
loc, APInt::getSignedMaxValue(inputBitWidth).getSExtValue(),
intermediateType);
auto clamp = clampIntHelper(loc, sub, min, max, rewriter, false);

// Truncate to the final value.
return rewriter.create<arith::TruncIOp>(loc, elementTy, clamp);
}
}

// tosa::BitwiseAndOp
Expand Down
34 changes: 13 additions & 21 deletions mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,18 +590,15 @@ class MatMulConverter : public OpConversionPattern<tosa::MatMulOp> {
.create<linalg::FillOp>(loc, ValueRange{zero},
ValueRange{emptyTensor})
.result();
if (!op.getQuantizationInfo()) {
if (!op.getAZp() && !op.getBZp()) {
rewriter.replaceOpWithNewOp<linalg::BatchMatmulOp>(
op, TypeRange{op.getType()},
ValueRange{adaptor.getA(), adaptor.getB()}, ValueRange{zeroTensor});
return success();
}

auto quantizationInfo = *op.getQuantizationInfo();
auto aZp = rewriter.create<arith::ConstantOp>(
loc, rewriter.getI32IntegerAttr(quantizationInfo.getAZp()));
auto bZp = rewriter.create<arith::ConstantOp>(
loc, rewriter.getI32IntegerAttr(quantizationInfo.getBZp()));
auto aZp = rewriter.create<arith::ConstantOp>(loc, op.getAZpAttr());
auto bZp = rewriter.create<arith::ConstantOp>(loc, op.getBZpAttr());
rewriter.replaceOpWithNewOp<linalg::QuantizedBatchMatmulOp>(
op, TypeRange{op.getType()},
ValueRange{adaptor.getA(), adaptor.getB(), aZp, bZp}, zeroTensor);
Expand Down Expand Up @@ -661,7 +658,7 @@ class FullyConnectedConverter
Value broadcastBias =
linalgBroadcastAndMaybeExtSI(rewriter, loc, bias, biasEmptyTensor);

if (!op.getQuantizationInfo()) {
if (!op.getInputZp() && !op.getWeightZp()) {
Value matmul = rewriter
.create<linalg::MatmulOp>(
loc, TypeRange{op.getType()},
Expand All @@ -672,11 +669,9 @@ class FullyConnectedConverter
return success();
}

auto quantizationInfo = *op.getQuantizationInfo();
auto inputZp = rewriter.create<arith::ConstantOp>(
loc, rewriter.getI32IntegerAttr(quantizationInfo.getInputZp()));
auto outputZp = rewriter.create<arith::ConstantOp>(
loc, rewriter.getI32IntegerAttr(quantizationInfo.getWeightZp()));
auto inputZp = rewriter.create<arith::ConstantOp>(loc, op.getInputZpAttr());
auto outputZp =
rewriter.create<arith::ConstantOp>(loc, op.getWeightZpAttr());
Value matmul =
rewriter
.create<linalg::QuantizedMatmulOp>(
Expand Down Expand Up @@ -958,10 +953,9 @@ class AvgPool2dConverter : public OpRewritePattern<tosa::AvgPool2dOp> {

// If we have quantization information we need to apply an offset
// for the input zp value.
if (op.getQuantizationInfo()) {
auto quantizationInfo = *op.getQuantizationInfo();
auto inputZp = rewriter.create<arith::ConstantOp>(
loc, b.getIntegerAttr(accETy, quantizationInfo.getInputZp()));
if (op.getInputZp()) {
auto inputZp =
rewriter.create<arith::ConstantOp>(loc, op.getInputZpAttr());
Value offset =
rewriter.create<arith::MulIOp>(loc, accETy, count, inputZp);
poolVal =
Expand Down Expand Up @@ -1013,11 +1007,9 @@ class AvgPool2dConverter : public OpRewritePattern<tosa::AvgPool2dOp> {

// If we have quantization information we need to apply output
// zeropoint.
if (op.getQuantizationInfo()) {
auto quantizationInfo = *op.getQuantizationInfo();
auto outputZp = rewriter.create<arith::ConstantOp>(
loc, b.getIntegerAttr(scaled.getType(),
quantizationInfo.getOutputZp()));
if (op.getOutputZp()) {
auto outputZp =
rewriter.create<arith::ConstantOp>(loc, op.getOutputZpAttr());
scaled = rewriter.create<arith::AddIOp>(loc, scaled, outputZp)
.getResult();
}
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ class PadConverter : public OpConversionPattern<tosa::PadOp> {
TypedAttr constantAttr;
if (isa<FloatType>(elementTy)) {
constantAttr = rewriter.getFloatAttr(elementTy, 0.0);
} else if (isa<IntegerType>(elementTy) && !padOp.getQuantizationInfo()) {
} else if (isa<IntegerType>(elementTy) && !padOp.getInputZpAttr()) {
constantAttr = rewriter.getIntegerAttr(elementTy, 0);
} else if (isa<IntegerType>(elementTy) && padOp.getQuantizationInfo()) {
int64_t value = padOp.getQuantizationInfo()->getInputZp();
} else if (isa<IntegerType>(elementTy) && padOp.getInputZpAttr()) {
int64_t value = padOp.getInputZpAttr().getInt();
constantAttr = rewriter.getIntegerAttr(elementTy, value);
}
if (constantAttr)
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ struct MaterializePadValue : public OpRewritePattern<tosa::PadOp> {
Attribute constantAttr;
if (llvm::isa<FloatType>(elementTy)) {
constantAttr = rewriter.getFloatAttr(elementTy, 0.0);
} else if (llvm::isa<IntegerType>(elementTy) && !op.getQuantizationInfo()) {
} else if (llvm::isa<IntegerType>(elementTy) && !op.getInputZpAttr()) {
constantAttr = rewriter.getIntegerAttr(elementTy, 0);
} else if (llvm::isa<IntegerType>(elementTy) && op.getQuantizationInfo()) {
auto value = op.getQuantizationInfo()->getInputZp();
} else if (llvm::isa<IntegerType>(elementTy) && op.getInputZpAttr()) {
int64_t value = op.getInputZpAttr().getInt();
constantAttr = rewriter.getIntegerAttr(elementTy, value);
}

Expand Down
60 changes: 43 additions & 17 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ static LogicalResult verifyConvOp(T op) {
}
}

bool inputIsQuant = !llvm::isa<FloatType>(inputEType);
bool weightIsQuant = !llvm::isa<FloatType>(weightEType);
bool inputIsFloat = llvm::isa<FloatType>(inputEType);
bool weightIsFloat = llvm::isa<FloatType>(weightEType);

// Either both must be quantized or both unquantized.
if (inputIsQuant != weightIsQuant) {
// Either both must be float or both non-float.
if (inputIsFloat != weightIsFloat) {
op.emitOpError(
"expect both input and weight to be float or not together, got ")
<< inputEType << " and " << weightEType;
Expand Down Expand Up @@ -527,7 +527,12 @@ static void buildTransConvOpWithQuantInfo(
auto quantAttr = ::buildConvOpQuantizationAttr(builder, input, weight);

if (quantAttr) {
result.addAttribute("quantization_info", quantAttr);
result.addAttribute("input_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getInputZp())));
result.addAttribute("weight_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getWeightZp())));
result.addTypes(
buildConvOpResultTypeInfo(builder, outputType, input, weight));
} else {
Expand Down Expand Up @@ -563,7 +568,10 @@ static void buildMatMulOpWithQuantInfo(OpBuilder &builder,
auto quantAttr = ::buildMatMulOpQuantizationAttr(builder, a, b);

if (quantAttr) {
result.addAttribute("quantization_info", quantAttr);
result.addAttribute("a_zp", builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getAZp())));
result.addAttribute("b_zp", builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getBZp())));

auto inputType = llvm::dyn_cast<ShapedType>(a.getType());
assert(inputType && "Input must be a shaped tensor type!");
Expand Down Expand Up @@ -603,8 +611,14 @@ buildAvgPool2dOpWithQuantInfo(OpBuilder &builder, OperationState &result,
result.addAttribute("pad", pad);
result.addAttribute("acc_type", accType);
auto quantAttr = buildUnaryOpQuantizationAttr(builder, input, outputType);
if (quantAttr)
result.addAttribute("quantization_info", quantAttr);
if (quantAttr) {
result.addAttribute("input_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getInputZp())));
result.addAttribute("output_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getOutputZp())));
}
result.types.push_back(outputType);
}

Expand All @@ -616,8 +630,15 @@ static void buildUnaryOpWithQuantInfo(OpBuilder &builder,
Value input) {
result.addOperands(input);
auto quantAttr = buildUnaryOpQuantizationAttr(builder, input, outputType);
if (quantAttr)
result.addAttribute("quantization_info", quantAttr);
if (quantAttr) {
// note: negateOp has attributes input1_zp and output_zp
result.addAttribute("input1_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getInputZp())));
result.addAttribute("output_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getOutputZp())));
}
result.types.push_back(outputType);
}

Expand All @@ -629,8 +650,11 @@ static void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result,
Value paddings) {
result.addOperands({input, paddings});
auto quantAttr = buildPadOpQuantizationAttr(builder, input);
if (quantAttr)
result.addAttribute("quantization_info", quantAttr);
if (quantAttr) {
result.addAttribute("input_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getInputZp())));
}
result.types.push_back(outputType);
}

Expand All @@ -643,8 +667,11 @@ static void buildExplicitValuePadOpWithQuantInfo(OpBuilder &builder,
Value padConst) {
result.addOperands({input, paddings, padConst});
auto quantAttr = buildPadOpQuantizationAttr(builder, input);
if (quantAttr)
result.addAttribute("quantization_info", quantAttr);
if (quantAttr) {
result.addAttribute("input_zp",
builder.getI32IntegerAttr(
static_cast<int32_t>(quantAttr.getInputZp())));
}
result.types.push_back(outputType);
}

Expand Down Expand Up @@ -898,9 +925,8 @@ LogicalResult FullyConnectedOp::verify() {

// Quantized type must have constructed the quantizationattr, and unquantized
// types should not have a quantizationattr.
if ((inputIsQuant && !getQuantizationInfo()) ||
(!inputIsQuant && getQuantizationInfo())) {
emitOpError("quantizationattr is required for quantized type, and not "
if ((inputIsQuant && !getInputZp()) || (!inputIsQuant && getInputZp())) {
emitOpError("input zero point is required for quantized type, and not "
"allowed for float type");
return failure();
}
Expand Down
Loading