[Torch Dialect] canonicalize aten.sign to aten.sgn (#3112)

* `aten.sign` is a sub-set of `aten.sgn` (`aten.sgn` support complex
type).
pull/3116/head
Yuanqiang Liu 2024-04-08 20:05:42 +08:00 committed by GitHub
parent 43d54efd14
commit 2c56ef9252
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 152 additions and 66 deletions

View File

@ -391,51 +391,6 @@ def Torch_AtenSigmoid_Op : Torch_Op<"aten.sigmoid_", [
}]; }];
} }
def Torch_AtenSignOp : Torch_Op<"aten.sign", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::sign : (Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenSignOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 1, 1);
}
void AtenSignOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 1, 1);
}
}];
}
def Torch_AtenSign_Op : Torch_Op<"aten.sign_", [
IsTrailingUnderscoreInplaceVariant,
AllowsTypeRefinement
]> {
let summary = "Generated op for `aten::sign_ : (Tensor) -> (Tensor)`";
let arguments = (ins
Torch_NonValueTensorType:$self
);
let results = (outs
AnyTorchOptionalNonValueTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenSign_Op::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 1, 1);
}
void AtenSign_Op::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 1, 1);
}
}];
}
def Torch_AtenSinhOp : Torch_Op<"aten.sinh", [ def Torch_AtenSinhOp : Torch_Op<"aten.sinh", [
AllowsTypeRefinement, AllowsTypeRefinement,
HasValueSemantics, HasValueSemantics,
@ -4218,6 +4173,52 @@ def Torch_AtenRound_Op : Torch_Op<"aten.round_", [
}]; }];
} }
def Torch_AtenSignOp : Torch_Op<"aten.sign", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::sign : (Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenSignOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 1, 1);
}
void AtenSignOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 1, 1);
}
}];
let hasCanonicalizer = 1;
}
def Torch_AtenSign_Op : Torch_Op<"aten.sign_", [
IsTrailingUnderscoreInplaceVariant,
AllowsTypeRefinement
]> {
let summary = "Generated op for `aten::sign_ : (Tensor) -> (Tensor)`";
let arguments = (ins
Torch_NonValueTensorType:$self
);
let results = (outs
AnyTorchOptionalNonValueTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenSign_Op::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 1, 1);
}
void AtenSign_Op::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 1, 1);
}
}];
}
def Torch_AtenMaskedFillTensorOp : Torch_Op<"aten.masked_fill.Tensor", [ def Torch_AtenMaskedFillTensorOp : Torch_Op<"aten.masked_fill.Tensor", [
AllowsTypeRefinement, AllowsTypeRefinement,
HasValueSemantics, HasValueSemantics,

View File

@ -1793,6 +1793,17 @@ OpFoldResult AtenRoundOp::fold(FoldAdaptor adaptor) {
return {}; return {};
} }
//===----------------------------------------------------------------------===//
// AtenSignOp
//===----------------------------------------------------------------------===//
void AtenSignOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add(+[](AtenSignOp op, PatternRewriter &rewriter) {
rewriter.replaceOpWithNewOp<AtenSgnOp>(op, op.getType(), op.getSelf());
return success();
});
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// AtenMulScalarOp // AtenMulScalarOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -6442,6 +6442,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %0 = call @__torch__.torch.jit._shape_functions.unary(%arg0) : (!torch.list<int>) -> !torch.list<int>\n" " %0 = call @__torch__.torch.jit._shape_functions.unary(%arg0) : (!torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n" " return %0 : !torch.list<int>\n"
" }\n" " }\n"
" func.func @\"__torch_mlir_shape_fn.aten.sgn\"(%arg0: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.unary(%arg0) : (!torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.detach\"(%arg0: !torch.list<int>) -> !torch.list<int> {\n" " func.func @\"__torch_mlir_shape_fn.aten.detach\"(%arg0: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.unary(%arg0) : (!torch.list<int>) -> !torch.list<int>\n" " %0 = call @__torch__.torch.jit._shape_functions.unary(%arg0) : (!torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n" " return %0 : !torch.list<int>\n"
@ -10129,6 +10133,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n" " %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n" " return %0#1 : !torch.int\n"
" }\n" " }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.sgn\"(%arg0: !torch.tuple<int, int>) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.floor\"(%arg0: !torch.tuple<int, int>) -> !torch.int {\n" " func.func @\"__torch_mlir_dtype_fn.aten.floor\"(%arg0: !torch.tuple<int, int>) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n" " %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n" " return %0#1 : !torch.int\n"

View File

@ -6971,46 +6971,54 @@ public:
} // namespace } // namespace
namespace { namespace {
// Decompose `aten.sign` op into comparisons and aten.where. // Decompose `aten.sgn` op into comparisons and aten.where.
class DecomposeAtenSignOp : public OpRewritePattern<AtenSignOp> { class DecomposeAtenSgnOp : public OpRewritePattern<AtenSgnOp> {
public: public:
using OpRewritePattern::OpRewritePattern; using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(AtenSignOp op, LogicalResult matchAndRewrite(AtenSgnOp op,
PatternRewriter &rewriter) const override { PatternRewriter &rewriter) const override {
Location loc = op.getLoc(); Location loc = op.getLoc();
auto outType = op.getType().dyn_cast<BaseTensorType>(); auto outType = op.getType().cast<BaseTensorType>();
if (!outType) if (!outType.hasDtype()) {
return rewriter.notifyMatchFailure( return rewriter.notifyMatchFailure(op,
op, "Only tensor types input are currently supported"); "expected result type to have dtype");
}
// TODO: support complex type in future.
if (outType.getDtype().isa<mlir::ComplexType>()) {
return rewriter.notifyMatchFailure(op,
"doesn't support complex type now");
}
auto zero = auto zero =
rewriter.create<ConstantFloatOp>(loc, rewriter.getF64FloatAttr(0.0)); rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(0));
auto one = auto one =
rewriter.create<ConstantFloatOp>(loc, rewriter.getF64FloatAttr(1.0)); rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(1));
auto minusOne = auto minusOne =
rewriter.create<ConstantFloatOp>(loc, rewriter.getF64FloatAttr(-1.0)); rewriter.create<ConstantIntOp>(loc, rewriter.getI64IntegerAttr(-1));
auto compTy = outType.getWithSizesAndDtype(outType.getOptionalSizes(), auto compTy = outType.getWithSizesAndDtype(outType.getOptionalSizes(),
rewriter.getI1Type()); rewriter.getI1Type());
auto greater = auto greater =
rewriter.create<AtenGtScalarOp>(loc, compTy, op.getSelf(), zero); rewriter.create<AtenGtScalarOp>(loc, compTy, op.getSelf(), zero);
auto greaterEqual = auto less =
rewriter.create<AtenGeScalarOp>(loc, compTy, op.getSelf(), zero); rewriter.create<AtenLtScalarOp>(loc, compTy, op.getSelf(), zero);
// Pseudo code: // Pseudo code:
// if (in >= 0) // if (in > 0)
// if (in > 0)
// return 1 // return 1
// else // else if (in < 0)
// return 0
// else
// return -1 // return -1
// else
// return 0
// note: return 0 if nan/0.0/-0.0
// return 1 if inf
// return -1 if -inf
auto selectGreater = auto selectGreater =
rewriter.create<AtenWhereScalarOp>(loc, outType, greater, one, zero); rewriter.create<AtenWhereScalarOp>(loc, outType, greater, one, zero);
rewriter.replaceOpWithNewOp<AtenWhereScalarOtherOp>( rewriter.replaceOpWithNewOp<AtenWhereScalarSelfOp>(op, outType, less,
op, outType, greaterEqual, selectGreater, minusOne); minusOne, selectGreater);
return success(); return success();
} }
}; };
@ -7606,7 +7614,7 @@ public:
addPatternIfTargetOpIsIllegal<DecomposeAtenTopkOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenTopkOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenScalarTensor>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenScalarTensor>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenScatterValueOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenScatterValueOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenSignOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenSgnOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenTypeAsOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenTypeAsOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenTileOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenTileOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenReshapeAsOp>(patterns); addPatternIfTargetOpIsIllegal<DecomposeAtenReshapeAsOp>(patterns);

View File

@ -888,6 +888,8 @@ STABLEHLO_PASS_SET = {
"ViewTwoFiveThreeStaticModule_basic", "ViewTwoFiveThreeStaticModule_basic",
"ViewTwoToThreeStaticModule_basic", "ViewTwoToThreeStaticModule_basic",
"ElementwiseLog1pModule_basic", "ElementwiseLog1pModule_basic",
"ElementwiseSgnModule_basic",
"ElementwiseSignIntModule_basic",
} }
STABLEHLO_CRASHING_SET = { STABLEHLO_CRASHING_SET = {
@ -897,6 +899,8 @@ STABLEHLO_CRASHING_SET = {
# Write the TOSA set as a "passing" set as it is very early in development # Write the TOSA set as a "passing" set as it is very early in development
# and very few tests work yet. # and very few tests work yet.
TOSA_PASS_SET = { TOSA_PASS_SET = {
"ElementwiseSgnModule_basic",
"ElementwiseSignIntModule_basic",
"AdaptiveAvgPool2dNonUnitOutputSizeStaticModule_basic", "AdaptiveAvgPool2dNonUnitOutputSizeStaticModule_basic",
"AdaptiveAvgPool2dUnitOutputSizeStaticModule_basic", "AdaptiveAvgPool2dUnitOutputSizeStaticModule_basic",
"AddCDivModule_basic", "AddCDivModule_basic",
@ -1567,6 +1571,7 @@ ONNX_XFAIL_SET = {
"ViewSizeFromOtherTensor_basic", "ViewSizeFromOtherTensor_basic",
# Failure - onnx_export # Failure - onnx_export
"ElementwiseSgnModule_basic",
"AdaptiveAvgPool1dGeneralDynamic_basic", "AdaptiveAvgPool1dGeneralDynamic_basic",
"AdaptiveAvgPool1dNonUnitOutputSizeDynamicModule_basic", "AdaptiveAvgPool1dNonUnitOutputSizeDynamicModule_basic",
"AdaptiveAvgPool1dStaticLargerOutput_basic", "AdaptiveAvgPool1dStaticLargerOutput_basic",

View File

@ -200,6 +200,9 @@ def atenfloor〡shape(self: List[int]) -> List[int]:
def atensign〡shape(self: List[int]) -> List[int]: def atensign〡shape(self: List[int]) -> List[int]:
return upstream_shape_functions.unary(self) return upstream_shape_functions.unary(self)
def atensgn〡shape(self: List[int]) -> List[int]:
return upstream_shape_functions.unary(self)
def atendetach〡shape(self: List[int]) -> List[int]: def atendetach〡shape(self: List[int]) -> List[int]:
return upstream_shape_functions.unary(self) return upstream_shape_functions.unary(self)
@ -2346,6 +2349,11 @@ def atensign〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
self_rank, self_dtype = self_rank_dtype self_rank, self_dtype = self_rank_dtype
return self_dtype return self_dtype
@check_dtype_function(_check_tensors_with_the_same_dtype(num_of_tensors=1))
def atensgn〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
self_rank, self_dtype = self_rank_dtype
return self_dtype
@check_dtype_function(_check_tensors_with_the_same_dtype(num_of_tensors=1)) @check_dtype_function(_check_tensors_with_the_same_dtype(num_of_tensors=1))
def atenfloor〡dtype(self_rank_dtype: Tuple[int, int]) -> int: def atenfloor〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
self_rank, self_dtype = self_rank_dtype self_rank, self_dtype = self_rank_dtype

View File

@ -269,7 +269,6 @@ def emit_ops(emitter_td: TextEmitter, registry: Registry):
"aten::log : (Tensor) -> (Tensor)", "aten::log : (Tensor) -> (Tensor)",
"aten::selu : (Tensor) -> (Tensor)", "aten::selu : (Tensor) -> (Tensor)",
"aten::sigmoid : (Tensor) -> (Tensor)", "aten::sigmoid : (Tensor) -> (Tensor)",
"aten::sign : (Tensor) -> (Tensor)",
"aten::sinh : (Tensor) -> (Tensor)", "aten::sinh : (Tensor) -> (Tensor)",
"aten::sgn : (Tensor) -> (Tensor)", "aten::sgn : (Tensor) -> (Tensor)",
"aten::hardsigmoid : (Tensor) -> (Tensor)", "aten::hardsigmoid : (Tensor) -> (Tensor)",
@ -357,6 +356,7 @@ def emit_ops(emitter_td: TextEmitter, registry: Registry):
emit_with_mutating_variants("aten::floor : (Tensor) -> (Tensor)", has_folder=True) emit_with_mutating_variants("aten::floor : (Tensor) -> (Tensor)", has_folder=True)
emit_with_mutating_variants("aten::ceil : (Tensor) -> (Tensor)", has_folder=True) emit_with_mutating_variants("aten::ceil : (Tensor) -> (Tensor)", has_folder=True)
emit_with_mutating_variants("aten::round : (Tensor) -> (Tensor)", has_folder=True) emit_with_mutating_variants("aten::round : (Tensor) -> (Tensor)", has_folder=True)
emit_with_mutating_variants("aten::sign : (Tensor) -> (Tensor)", has_canonicalizer=True)
emit_with_mutating_variants("aten::masked_fill.Tensor : (Tensor, Tensor, Tensor) -> (Tensor)", has_canonicalizer=True) emit_with_mutating_variants("aten::masked_fill.Tensor : (Tensor, Tensor, Tensor) -> (Tensor)", has_canonicalizer=True)
emit_with_mutating_variants("aten::addcmul : (Tensor, Tensor, Tensor, Scalar) -> (Tensor)") emit_with_mutating_variants("aten::addcmul : (Tensor, Tensor, Tensor, Scalar) -> (Tensor)")

View File

@ -1986,6 +1986,51 @@ class ElementwiseSignModule(torch.nn.Module):
@register_test_case(module_factory=lambda: ElementwiseSignModule()) @register_test_case(module_factory=lambda: ElementwiseSignModule())
def ElementwiseSignModule_basic(module, tu: TestUtils): def ElementwiseSignModule_basic(module, tu: TestUtils):
module.forward(torch.tensor([[-2.0, 0.0, 1.1, 2.0],
[6.0, -0.0, torch.inf, -torch.inf]]))
# ==============================================================================
class ElementwiseSignIntModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args([
None,
([3, 4], torch.int64, True),
])
def forward(self, a):
return torch.ops.aten.sign(a)
@register_test_case(module_factory=lambda: ElementwiseSignIntModule())
def ElementwiseSignIntModule_basic(module, tu: TestUtils):
module.forward(tu.randint(3, 4, low=-100, high=100))
# ==============================================================================
class ElementwiseSgnModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args([
None,
([3, 4], torch.float32, True),
])
def forward(self, a):
return torch.ops.aten.sgn(a)
@register_test_case(module_factory=lambda: ElementwiseSgnModule())
def ElementwiseSgnModule_basic(module, tu: TestUtils):
module.forward(tu.rand(3, 4)) module.forward(tu.rand(3, 4))