Skip to content

Commit 8af23ae

Browse files
psunnTai78641
authored andcommitted
[mlir][tosa] Change Rescale zero points to be inputs
*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. Change-Id: I2cf0106eb9f9ec88e16de5efc93b651053e5fc92 Signed-off-by: Peng Sun <[email protected]>
1 parent 3382119 commit 8af23ae

File tree

15 files changed

+263
-113
lines changed

15 files changed

+263
-113
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
@@ -218,15 +218,15 @@ profileComplianceMap = {
218218
{fp32T, fp16T}}}}},
219219
{"tosa.rescale",
220220
{{{Profile::pro_int},
221-
{{i8T, i8T},
222-
{i8T, i16T},
223-
{i8T, i32T},
224-
{i16T, i8T},
225-
{i16T, i16T},
226-
{i16T, i32T},
227-
{i32T, i8T},
228-
{i32T, i16T},
229-
{i32T, i32T}}}}},
221+
{{i8T, i8T, i8T, i8T},
222+
{i8T, i8T, i16T, i16T},
223+
{i8T, i8T, i32T, i32T},
224+
{i16T, i16T, i8T, i8T},
225+
{i16T, i16T, i16T, i16T},
226+
{i16T, i16T, i32T, i32T},
227+
{i32T, i32T, i8T, i8T},
228+
{i32T, i32T, i16T, i16T},
229+
{i32T, i32T, i32T, i32T}}}}},
230230
{"tosa.const",
231231
{{{Profile::pro_int}, {{boolT}, {i8T}, {i16T}, {i32T}}},
232232
{{Profile::pro_fp}, {{fp16T}, {fp32T}}}}},
@@ -386,7 +386,10 @@ extensionComplianceMap = {
386386
{fp16T, fp8e5m2T},
387387
{fp32T, fp8e5m2T}}}}},
388388
{"tosa.rescale",
389-
{{{Extension::int16}, {{i48T, i8T}, {i48T, i16T}, {i48T, i32T}}}}},
389+
{{{Extension::int16},
390+
{{i48T, i48T, i8T, i8T},
391+
{i48T, i48T, i16T, i16T},
392+
{i48T, i48T, i32T, i32T}}}}},
390393
{"tosa.const",
391394
{{{Extension::int4}, {{i4T}}},
392395
{{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
@@ -2344,8 +2344,8 @@ def Tosa_RescaleOp : Tosa_InferShapedTypeOp<"rescale"> {
23442344
Tosa_Tensor:$input,
23452345
Tosa_1DInt16Or32Tensor:$multiplier,
23462346
Tosa_1DInt8Tensor:$shift,
2347-
I32Attr:$input_zp,
2348-
I32Attr:$output_zp,
2347+
Tosa_ScalarIntOrFloatTensor:$input_zp,
2348+
Tosa_ScalarIntOrFloatTensor:$output_zp,
23492349
BoolAttr:$scale32,
23502350
BoolAttr:$double_round,
23512351
BoolAttr:$per_channel,
@@ -2362,6 +2362,13 @@ def Tosa_RescaleOp : Tosa_InferShapedTypeOp<"rescale"> {
23622362
Extension<[Tosa_EXT_INT16]>,
23632363
];
23642364

2365+
let extraClassDeclaration = [{
2366+
FailureOr<int64_t> getInputZeroPoint();
2367+
FailureOr<int64_t> getOutputZeroPoint();
2368+
LogicalResult verifyInputZeroPoint(int64_t zp);
2369+
LogicalResult verifyOutputZeroPoint(int64_t zp);
2370+
}];
2371+
23652372
let hasVerifier = 1;
23662373

23672374
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
}
@@ -1491,11 +1490,26 @@ class RescaleConverter : public OpRewritePattern<tosa::RescaleOp> {
14911490
// later.
14921491
int32_t inBitwidth = valueTy.getIntOrFloatBitWidth() > 32 ? 48 : 32;
14931492

1494-
auto inputZp = createConstFromIntAttribute<int32_t>(
1495-
op, "input_zp", nestedBuilder.getIntegerType(inBitwidth),
1493+
FailureOr<int64_t> maybeIZp = op.getInputZeroPoint();
1494+
if (failed(maybeIZp)) {
1495+
(void)rewriter.notifyMatchFailure(
1496+
op, "input zero point cannot be statically determined");
1497+
return;
1498+
}
1499+
1500+
auto inputZp = createConstOpFromZpVal<int32_t>(
1501+
op, *maybeIZp, nestedBuilder.getIntegerType(inBitwidth),
14961502
nestedBuilder);
1497-
auto outputZp = createConstFromIntAttribute<int32_t>(
1498-
op, "output_zp", nestedBuilder.getI32Type(), nestedBuilder);
1503+
1504+
FailureOr<int64_t> maybeOZp = op.getOutputZeroPoint();
1505+
if (failed(maybeOZp)) {
1506+
(void)rewriter.notifyMatchFailure(
1507+
op, "output zero point cannot be statically determined");
1508+
return;
1509+
};
1510+
1511+
auto outputZp = createConstOpFromZpVal<int32_t>(
1512+
op, *maybeOZp, nestedBuilder.getI32Type(), nestedBuilder);
14991513

15001514
Value multiplier = multiplierConstant ? multiplierConstant
15011515
: blockArgs[multiplierArg];

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

Lines changed: 62 additions & 34 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
//===----------------------------------------------------------------------===//
@@ -1708,6 +1727,33 @@ static LogicalResult verifyZeroPoint(T op, Value val, const int64_t &zp,
17081727
return success();
17091728
}
17101729

1730+
static LogicalResult verifyZeroPoint(tosa::RescaleOp op, Value zpVal,
1731+
const int64_t &zp,
1732+
const std::string &operand) {
1733+
bool isInputZp = (operand == "Input");
1734+
1735+
bool tensorUnsigned =
1736+
isInputZp ? op.getInputUnsigned() : op.getOutputUnsigned();
1737+
StringRef tensorName = isInputZp ? "input" : "output";
1738+
1739+
Type zpElemType = getElementTypeOrSelf(zpVal);
1740+
1741+
if (zp != 0) {
1742+
if (!zpElemType.isInteger(8) &&
1743+
!(zpElemType.isInteger(16) && tensorUnsigned)) {
1744+
return op.emitOpError()
1745+
<< "expect " << tensorName << "_zp of 0, got " << zp;
1746+
}
1747+
if (zpElemType.isInteger(16) && tensorUnsigned && zp != 32768) {
1748+
return op.emitOpError() << "expect " << tensorName
1749+
<< "_zp of 0 or 32768 for unsigned int16 "
1750+
<< tensorName << ", got " << zp;
1751+
}
1752+
}
1753+
1754+
return success();
1755+
}
1756+
17111757
#define ZERO_POINT_HELPER(OP, OPERAND_NAME) \
17121758
FailureOr<int64_t> tosa::OP::get##OPERAND_NAME##ZeroPoint() { \
17131759
return getZeroPoint(*this, get##OPERAND_NAME##Zp()); \
@@ -1728,6 +1774,8 @@ ZERO_POINT_HELPER(AvgPool2dOp, Input)
17281774
ZERO_POINT_HELPER(AvgPool2dOp, Output)
17291775
ZERO_POINT_HELPER(MatMulOp, A)
17301776
ZERO_POINT_HELPER(MatMulOp, B)
1777+
ZERO_POINT_HELPER(RescaleOp, Input)
1778+
ZERO_POINT_HELPER(RescaleOp, Output)
17311779
#undef ZERO_POINT_HELPER
17321780

17331781
LogicalResult tosa::TransposeOp::inferReturnTypeComponents(
@@ -2712,41 +2760,21 @@ LogicalResult RescaleOp::verify() {
27122760
return failure();
27132761
}
27142762

2715-
auto input_zp = getInputZpAttr().getInt();
2716-
if (input_zp != 0) {
2717-
// only int8/uint8 and uint16 input can have non-zero input_zp
2718-
if (!inputElementType.isInteger(8) &&
2719-
!(inputElementType.isInteger(16) && getInputUnsigned())) {
2720-
emitOpError("expect input_zp of 0, got ") << input_zp;
2721-
return failure();
2722-
}
2723-
// input_zp must be either 0 or 32768 for uint16 input
2724-
if (inputElementType.isInteger(16) && getInputUnsigned() &&
2725-
input_zp != 32768) {
2726-
emitOpError(
2727-
"expect input_zp of 0 or 32768 for unsigned int16 input, got ")
2728-
<< input_zp;
2729-
return failure();
2730-
}
2731-
}
2763+
if (verifyRescaleValueAndZpTypes(*this, getInput(), getInputZp(), "input")
2764+
.failed())
2765+
return failure();
27322766

2733-
auto output_zp = getOutputZpAttr().getInt();
2734-
if (output_zp != 0) {
2735-
// only int8/uint8 and uint16 output can have non-zero output_zp
2736-
if (!outputElementType.isInteger(8) &&
2737-
!(outputElementType.isInteger(16) && getOutputUnsigned())) {
2738-
emitOpError("expect output_zp of 0, got ") << output_zp;
2739-
return failure();
2740-
}
2741-
// output_zp must be either 0 or 32768 for uint16 output
2742-
if (outputElementType.isInteger(16) && getOutputUnsigned() &&
2743-
output_zp != 32768) {
2744-
emitOpError(
2745-
"expect output_zp of 0 or 32768 for unsigned int16 output, got ")
2746-
<< output_zp;
2747-
return failure();
2748-
}
2749-
}
2767+
if (verifyRescaleValueAndZpTypes(*this, getOutput(), getOutputZp(), "output")
2768+
.failed())
2769+
return failure();
2770+
2771+
FailureOr<int64_t> maybeIZp = getInputZeroPoint();
2772+
if (succeeded(maybeIZp) && verifyInputZeroPoint(*maybeIZp).failed())
2773+
return failure();
2774+
2775+
FailureOr<int64_t> maybeOZp = getOutputZeroPoint();
2776+
if (succeeded(maybeOZp) && verifyOutputZeroPoint(*maybeOZp).failed())
2777+
return failure();
27502778

27512779
auto multiplierType = llvm::dyn_cast<ShapedType>(getMultiplier().getType());
27522780
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ 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>
40+
3841
// expected-error@+1 {{failed to legalize operation 'tosa.rescale'}}
39-
%0 = tosa.rescale %arg0, %multiplier, %shift {double_round = false, 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>>
42+
%0 = tosa.rescale %arg0, %multiplier, %shift, %input_zp, %output_zp {scale32 = true, double_round = false, per_channel = false, 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>>
4043
return %0 : tensor<13x21x3x!quant.uniform<i8:f32, 0.015655439347028732:-1>>
4144
}
4245

0 commit comments

Comments
 (0)