Skip to content
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
25 changes: 18 additions & 7 deletions lib/Conversion/TorchToStablehlo/Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,12 @@ class ConvertAtenCompareOp : public OpConversionPattern<AtenOpT> {
if (!lhsTy) {
return op.emitError("only Tensor types supported in StableHLO");
}
bool isRhsScalar = false;
if (!rhsTy) {
rhs = hlo::scalarToStablehloTensor(rewriter, op, adaptor.getOther(),
rhs.getType());
// use lhs's element type as compute type
rhs =
hlo::promoteType(rewriter, op.getLoc(), rhs, lhsTy.getElementType());
rhsTy = dyn_cast<RankedTensorType>(rhs.getType());
isRhsScalar = true;
}

auto outType = cast<RankedTensorType>(
Expand All @@ -537,16 +536,28 @@ class ConvertAtenCompareOp : public OpConversionPattern<AtenOpT> {
}

if (isa<mlir::IntegerType>(lhsElemTy) && isa<mlir::FloatType>(rhsElemTy)) {
lhs = hlo::promoteType(rewriter, op.getLoc(), lhs, rhsElemTy);
// torch.lt(x_int, 1.1) use fp32 as compute type
// torch.lt(x_int, y_float) use y's float type as compute type
Type promoteTo = isRhsScalar ? rewriter.getF32Type() : rhsElemTy;
lhs = hlo::promoteType(rewriter, op.getLoc(), lhs, promoteTo);
rhs = hlo::promoteType(rewriter, op.getLoc(), rhs, promoteTo);
} else if (isa<mlir::FloatType>(lhsElemTy) &&
isa<mlir::IntegerType>(rhsElemTy)) {
// always use lhs's float type as compute type
rhs = hlo::promoteType(rewriter, op.getLoc(), rhs, lhsElemTy);
} else {
if (lhsElemTy.getIntOrFloatBitWidth() >
rhsElemTy.getIntOrFloatBitWidth()) {
if (isRhsScalar) {
// torch.lt(x_float, 1.1) use x's float type as compute type
// torch.lt(x_int, 1) use x's int type as compute type
rhs = hlo::promoteType(rewriter, op.getLoc(), rhs, lhsElemTy);
} else {
lhs = hlo::promoteType(rewriter, op.getLoc(), lhs, rhsElemTy);
// torch.lt(x_float, y_float) use higher bitwidth as compute type
Type promoteTo = lhsElemTy.getIntOrFloatBitWidth() >
rhsElemTy.getIntOrFloatBitWidth()
? lhsElemTy
: rhsElemTy;
lhs = hlo::promoteType(rewriter, op.getLoc(), lhs, promoteTo);
rhs = hlo::promoteType(rewriter, op.getLoc(), rhs, promoteTo);
}
}
lhsElemTy = dyn_cast<RankedTensorType>(lhs.getType()).getElementType();
Expand Down
1 change: 0 additions & 1 deletion projects/pt1/e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@
"AtenPolarFloatModule_basic",
"DiagonalWithStaticShapeModule_basic",
"EinsumStaticDiagonalDimensionModule_basic",
"ElementwiseIntTensorLtFloatScalarModule_basic",
"ElementwiseRemainderScalarModule_Bool_NegativeDivisor_basic",
"ElementwiseRemainderScalarModule_Float_NegativeDividend_basic",
"ElementwiseRemainderScalarModule_Float_NegativeDivisor_basic",
Expand Down
Loading