Skip to content

Commit 913d077

Browse files
Tai78641psunn
andauthored
[mlir][tosa] Change Rescale zero points to be inputs (llvm#130340)
*Update RescaleOp to use zero-point as operands instead of attributes. *Check input_zp data type against the input and output_zp data type against the output. Signed-off-by: Peng Sun <[email protected]> Co-authored-by: Peng Sun <[email protected]>
1 parent 7341753 commit 913d077

16 files changed

+261
-117
lines changed

mlir/include/mlir/Dialect/Tosa/IR/TosaComplianceData.h.inc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ profileComplianceMap = {
222222
{fp32T, fp16T}}}}},
223223
{"tosa.rescale",
224224
{{{Profile::pro_int},
225-
{{i8T, i8T},
226-
{i8T, i16T},
227-
{i8T, i32T},
228-
{i16T, i8T},
229-
{i16T, i16T},
230-
{i16T, i32T},
231-
{i32T, i8T},
232-
{i32T, i16T},
233-
{i32T, i32T}}}}},
225+
{{i8T, i8T, i8T, i8T},
226+
{i8T, i8T, i16T, i16T},
227+
{i8T, i8T, i32T, i32T},
228+
{i16T, i16T, i8T, i8T},
229+
{i16T, i16T, i16T, i16T},
230+
{i16T, i16T, i32T, i32T},
231+
{i32T, i32T, i8T, i8T},
232+
{i32T, i32T, i16T, i16T},
233+
{i32T, i32T, i32T, i32T}}}}},
234234
{"tosa.const",
235235
{{{Profile::pro_int}, {{boolT}, {i8T}, {i16T}, {i32T}}},
236236
{{Profile::pro_fp}, {{fp16T}, {fp32T}}}}},
@@ -390,7 +390,10 @@ extensionComplianceMap = {
390390
{fp16T, fp8e5m2T},
391391
{fp32T, fp8e5m2T}}}}},
392392
{"tosa.rescale",
393-
{{{Extension::int16}, {{i48T, i8T}, {i48T, i16T}, {i48T, i32T}}}}},
393+
{{{Extension::int16},
394+
{{i48T, i48T, i8T, i8T},
395+
{i48T, i48T, i16T, i16T},
396+
{i48T, i48T, i32T, i32T}}}}},
394397
{"tosa.const",
395398
{{{Extension::int4}, {{i4T}}},
396399
{{Extension::int16}, {{i48T}}},

mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,8 +2357,8 @@ def Tosa_RescaleOp : Tosa_InferShapedTypeOp<"rescale"> {
23572357
Tosa_Tensor:$input,
23582358
Tosa_1DInt16Or32Tensor:$multiplier,
23592359
Tosa_1DInt8Tensor:$shift,
2360-
I32Attr:$input_zp,
2361-
I32Attr:$output_zp,
2360+
Tosa_ScalarIntOrFloatTensor:$input_zp,
2361+
Tosa_ScalarIntOrFloatTensor:$output_zp,
23622362
BoolAttr:$scale32,
23632363
Tosa_RoundingTypeAttr:$rounding_mode,
23642364
BoolAttr:$per_channel,
@@ -2375,6 +2375,13 @@ def Tosa_RescaleOp : Tosa_InferShapedTypeOp<"rescale"> {
23752375
Extension<[Tosa_EXT_INT16]>,
23762376
];
23772377

2378+
let extraClassDeclaration = [{
2379+
FailureOr<int64_t> getInputZeroPoint();
2380+
FailureOr<int64_t> getOutputZeroPoint();
2381+
LogicalResult verifyInputZeroPoint(int64_t zp);
2382+
LogicalResult verifyOutputZeroPoint(int64_t zp);
2383+
}];
2384+
23782385
let hasVerifier = 1;
23792386

23802387
let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)";

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ materializeBinaryNanCheckIfRequired(OpTy op, PatternRewriter &rewriter,
8484

8585
template <typename T>
8686
static arith::ConstantOp
87-
createConstFromIntAttribute(Operation *op, const std::string &attrName,
88-
Type requiredAttrType, OpBuilder &rewriter) {
89-
auto castedN = static_cast<T>(
90-
cast<IntegerAttr>(op->getAttr(attrName)).getValue().getSExtValue());
87+
createConstOpFromZpVal(Operation *op, const int64_t &zp, Type requiredAttrType,
88+
OpBuilder &rewriter) {
89+
auto castedN = static_cast<T>(zp);
9190
return rewriter.create<arith::ConstantOp>(
9291
op->getLoc(), IntegerAttr::get(requiredAttrType, castedN));
9392
}
@@ -1510,11 +1509,26 @@ class RescaleConverter : public OpRewritePattern<tosa::RescaleOp> {
15101509
// later.
15111510
int32_t inBitwidth = valueTy.getIntOrFloatBitWidth() > 32 ? 48 : 32;
15121511

1513-
auto inputZp = createConstFromIntAttribute<int32_t>(
1514-
op, "input_zp", nestedBuilder.getIntegerType(inBitwidth),
1512+
FailureOr<int64_t> maybeIZp = op.getInputZeroPoint();
1513+
if (failed(maybeIZp)) {
1514+
(void)rewriter.notifyMatchFailure(
1515+
op, "input zero point cannot be statically determined");
1516+
return;
1517+
}
1518+
1519+
auto inputZp = createConstOpFromZpVal<int32_t>(
1520+
op, *maybeIZp, nestedBuilder.getIntegerType(inBitwidth),
15151521
nestedBuilder);
1516-
auto outputZp = createConstFromIntAttribute<int32_t>(
1517-
op, "output_zp", nestedBuilder.getI32Type(), nestedBuilder);
1522+
1523+
FailureOr<int64_t> maybeOZp = op.getOutputZeroPoint();
1524+
if (failed(maybeOZp)) {
1525+
(void)rewriter.notifyMatchFailure(
1526+
op, "output zero point cannot be statically determined");
1527+
return;
1528+
};
1529+
1530+
auto outputZp = createConstOpFromZpVal<int32_t>(
1531+
op, *maybeOZp, nestedBuilder.getI32Type(), nestedBuilder);
15181532

15191533
Value multiplier = multiplierConstant ? multiplierConstant
15201534
: blockArgs[multiplierArg];

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ static Type getStorageElementTypeOrSelf(Type type) {
254254
return elementType;
255255
}
256256

257+
static LogicalResult verifyRescaleValueAndZpTypes(Operation *op, Value val,
258+
Value valZp, StringRef name) {
259+
Type eType = getStorageElementTypeOrSelf(val.getType());
260+
Type eZpType = getStorageElementTypeOrSelf(valZp.getType());
261+
262+
bool bothInts =
263+
mlir::isa<IntegerType>(eType) && mlir::isa<IntegerType>(eZpType);
264+
bool sameBitWidth =
265+
(eType.getIntOrFloatBitWidth() == eZpType.getIntOrFloatBitWidth());
266+
267+
if (!bothInts || !sameBitWidth) {
268+
return op->emitOpError()
269+
<< "expected " << name << " and " << name
270+
<< "_zp to both be integer of the same bitwidth, but got " << eType
271+
<< " vs. " << eZpType;
272+
}
273+
return success();
274+
}
275+
257276
//===----------------------------------------------------------------------===//
258277
// TOSA Operator Verifiers.
259278
//===----------------------------------------------------------------------===//
@@ -1729,6 +1748,33 @@ static LogicalResult verifyZeroPoint(T op, Value val, const int64_t &zp,
17291748
return success();
17301749
}
17311750

1751+
static LogicalResult verifyZeroPoint(tosa::RescaleOp op, Value zpVal,
1752+
const int64_t &zp,
1753+
const std::string &operand) {
1754+
bool isInputZp = (operand == "Input");
1755+
1756+
bool tensorUnsigned =
1757+
isInputZp ? op.getInputUnsigned() : op.getOutputUnsigned();
1758+
StringRef tensorName = isInputZp ? "input" : "output";
1759+
1760+
Type zpElemType = getElementTypeOrSelf(zpVal);
1761+
1762+
if (zp != 0) {
1763+
if (!zpElemType.isInteger(8) &&
1764+
!(zpElemType.isInteger(16) && tensorUnsigned)) {
1765+
return op.emitOpError()
1766+
<< "expect " << tensorName << "_zp of 0, got " << zp;
1767+
}
1768+
if (zpElemType.isInteger(16) && tensorUnsigned && zp != 32768) {
1769+
return op.emitOpError() << "expect " << tensorName
1770+
<< "_zp of 0 or 32768 for unsigned int16 "
1771+
<< tensorName << ", got " << zp;
1772+
}
1773+
}
1774+
1775+
return success();
1776+
}
1777+
17321778
#define ZERO_POINT_HELPER(OP, OPERAND_NAME) \
17331779
FailureOr<int64_t> tosa::OP::get##OPERAND_NAME##ZeroPoint() { \
17341780
return getZeroPoint(*this, get##OPERAND_NAME##Zp()); \
@@ -1751,7 +1797,8 @@ ZERO_POINT_HELPER(MatMulOp, A)
17511797
ZERO_POINT_HELPER(MatMulOp, B)
17521798
ZERO_POINT_HELPER(NegateOp, Input1)
17531799
ZERO_POINT_HELPER(NegateOp, Output)
1754-
1800+
ZERO_POINT_HELPER(RescaleOp, Input)
1801+
ZERO_POINT_HELPER(RescaleOp, Output)
17551802
#undef ZERO_POINT_HELPER
17561803

17571804
LogicalResult tosa::TransposeOp::inferReturnTypeComponents(
@@ -2784,41 +2831,21 @@ LogicalResult RescaleOp::verify() {
27842831
return failure();
27852832
}
27862833

2787-
auto input_zp = getInputZpAttr().getInt();
2788-
if (input_zp != 0) {
2789-
// only int8/uint8 and uint16 input can have non-zero input_zp
2790-
if (!inputElementType.isInteger(8) &&
2791-
!(inputElementType.isInteger(16) && getInputUnsigned())) {
2792-
emitOpError("expect input_zp of 0, got ") << input_zp;
2793-
return failure();
2794-
}
2795-
// input_zp must be either 0 or 32768 for uint16 input
2796-
if (inputElementType.isInteger(16) && getInputUnsigned() &&
2797-
input_zp != 32768) {
2798-
emitOpError(
2799-
"expect input_zp of 0 or 32768 for unsigned int16 input, got ")
2800-
<< input_zp;
2801-
return failure();
2802-
}
2803-
}
2834+
if (verifyRescaleValueAndZpTypes(*this, getInput(), getInputZp(), "input")
2835+
.failed())
2836+
return failure();
28042837

2805-
auto output_zp = getOutputZpAttr().getInt();
2806-
if (output_zp != 0) {
2807-
// only int8/uint8 and uint16 output can have non-zero output_zp
2808-
if (!outputElementType.isInteger(8) &&
2809-
!(outputElementType.isInteger(16) && getOutputUnsigned())) {
2810-
emitOpError("expect output_zp of 0, got ") << output_zp;
2811-
return failure();
2812-
}
2813-
// output_zp must be either 0 or 32768 for uint16 output
2814-
if (outputElementType.isInteger(16) && getOutputUnsigned() &&
2815-
output_zp != 32768) {
2816-
emitOpError(
2817-
"expect output_zp of 0 or 32768 for unsigned int16 output, got ")
2818-
<< output_zp;
2819-
return failure();
2820-
}
2821-
}
2838+
if (verifyRescaleValueAndZpTypes(*this, getOutput(), getOutputZp(), "output")
2839+
.failed())
2840+
return failure();
2841+
2842+
FailureOr<int64_t> maybeIZp = getInputZeroPoint();
2843+
if (succeeded(maybeIZp) && verifyInputZeroPoint(*maybeIZp).failed())
2844+
return failure();
2845+
2846+
FailureOr<int64_t> maybeOZp = getOutputZeroPoint();
2847+
if (succeeded(maybeOZp) && verifyOutputZeroPoint(*maybeOZp).failed())
2848+
return failure();
28222849

28232850
auto multiplierType = llvm::dyn_cast<ShapedType>(getMultiplier().getType());
28242851
if (!multiplierType) {

mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ void ProfileInfoDepot::populateProfileInfo(tosa::SelectOp op) {
175175
template <>
176176
void ProfileInfoDepot::populateProfileInfo(tosa::RescaleOp op) {
177177
addValue(op.getInput());
178+
addValue(op.getInputZp());
179+
addValue(op.getOutputZp());
178180
addValue(op.getOutput());
179181
}
180182

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalid.mlir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ func.func @unranked_add(%arg0 : tensor<10x10xf32> , %arg1 : tensor<10x10xf32>, %
3535
func.func @rescale_unsupported_type(%arg0: tensor<13x21x3x!quant.uniform<u8:f32, 0.015655439347028732:127>>) -> tensor<13x21x3x!quant.uniform<i8:f32, 0.015655439347028732:-1>> {
3636
%multiplier = "tosa.const"() {values = dense<1073741824> : tensor<1xi32> } : () -> tensor<1xi32>
3737
%shift = "tosa.const"() {values = dense<30> : tensor<1xi8> } : () -> tensor<1xi8>
38+
%input_zp = "tosa.const"() {values = dense<127> : tensor<1xi8>} : () -> tensor<1xi8>
39+
%output_zp = "tosa.const"() {values = dense<-1> : tensor<1xi8>} : () -> tensor<1xi8>
3840
// expected-error@+1 {{failed to legalize operation 'tosa.rescale'}}
39-
%0 = tosa.rescale %arg0, %multiplier, %shift {rounding_mode = "SINGLE_ROUND", input_zp = 127 : i32, output_zp = -1 : i32, per_channel = false, scale32 = true, input_unsigned = true, output_unsigned = false} : (tensor<13x21x3x!quant.uniform<u8:f32, 0.015655439347028732:127>>, tensor<1xi32>, tensor<1xi8>) -> tensor<13x21x3x!quant.uniform<i8:f32, 0.015655439347028732:-1>>
41+
%0 = tosa.rescale %arg0, %multiplier, %shift, %input_zp, %output_zp {rounding_mode = "SINGLE_ROUND", input_zp = 127 : i32, output_zp = -1 : i32, per_channel = false, scale32 = true, input_unsigned = true, output_unsigned = false} : (tensor<13x21x3x!quant.uniform<u8:f32, 0.015655439347028732:127>>, tensor<1xi32>, tensor<1xi8>, tensor<1xi8>, tensor<1xi8>) -> tensor<13x21x3x!quant.uniform<i8:f32, 0.015655439347028732:-1>>
4042
return %0 : tensor<13x21x3x!quant.uniform<i8:f32, 0.015655439347028732:-1>>
4143
}
4244

0 commit comments

Comments
 (0)