[torch dialect] emit aten.fmax/fmin and add decomposition patterns (#3510)

pull/3519/head
Jiawei Wu 2024-06-29 00:07:55 +08:00 committed by GitHub
parent 5a627c46b7
commit f75cbb4df9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 201 additions and 0 deletions

View File

@ -4671,6 +4671,54 @@ def Torch_AtenMinimumOp : Torch_Op<"aten.minimum", [
}];
}
def Torch_AtenFmaxOp : Torch_Op<"aten.fmax", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::fmax : (Tensor, Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self,
AnyTorchTensorType:$other
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenFmaxOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 2, 1);
}
void AtenFmaxOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 2, 1);
}
}];
}
def Torch_AtenFminOp : Torch_Op<"aten.fmin", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::fmin : (Tensor, Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self,
AnyTorchTensorType:$other
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenFminOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 2, 1);
}
void AtenFminOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 2, 1);
}
}];
}
def Torch_AtenMishOp : Torch_Op<"aten.mish", [
AllowsTypeRefinement,
HasValueSemantics,

View File

@ -8940,6 +8940,14 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %0 = call @__torch__.torch.jit._shape_functions.broadcast(%arg0, %arg1) : (!torch.list<int>, !torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.fmin\"(%arg0: !torch.list<int>, %arg1: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.broadcast(%arg0, %arg1) : (!torch.list<int>, !torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.fmax\"(%arg0: !torch.list<int>, %arg1: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.broadcast(%arg0, %arg1) : (!torch.list<int>, !torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.bitwise_or.Tensor\"(%arg0: !torch.list<int>, %arg1: !torch.list<int>) -> !torch.list<int> {\n"
" %0 = call @__torch__.torch.jit._shape_functions.broadcast(%arg0, %arg1) : (!torch.list<int>, !torch.list<int>) -> !torch.list<int>\n"
" return %0 : !torch.list<int>\n"
@ -12471,6 +12479,22 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %4 = call @__torch__.torch_mlir.jit_ir_importer.build_tools.library_generator.promote_dtypes(%2, %3) : (!torch.list<optional<int>>, !torch.list<int>) -> !torch.int\n"
" return %4 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.fmax\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.tuple<int, int>) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg1 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" %1:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" %2 = torch.prim.ListConstruct %1#0, %0#0 : (!torch.int, !torch.int) -> !torch.list<optional<int>>\n"
" %3 = torch.prim.ListConstruct %1#1, %0#1 : (!torch.int, !torch.int) -> !torch.list<int>\n"
" %4 = call @__torch__.torch_mlir.jit_ir_importer.build_tools.library_generator.promote_dtypes(%2, %3) : (!torch.list<optional<int>>, !torch.list<int>) -> !torch.int\n"
" return %4 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.fmin\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.tuple<int, int>) -> !torch.int {\n"
" %0:2 = torch.prim.TupleUnpack %arg1 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" %1:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" %2 = torch.prim.ListConstruct %1#0, %0#0 : (!torch.int, !torch.int) -> !torch.list<optional<int>>\n"
" %3 = torch.prim.ListConstruct %1#1, %0#1 : (!torch.int, !torch.int) -> !torch.list<int>\n"
" %4 = call @__torch__.torch_mlir.jit_ir_importer.build_tools.library_generator.promote_dtypes(%2, %3) : (!torch.list<optional<int>>, !torch.list<int>) -> !torch.int\n"
" return %4 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.mm\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.tuple<int, int>) -> !torch.int {\n"
" %false = torch.constant.bool false\n"
" %int5 = torch.constant.int 5\n"

View File

@ -8493,6 +8493,41 @@ public:
};
} // namespace
namespace {
// Decompose aten.fmax/fmin to aten.maximum/minimum + aten.where(nanMask)
template <typename AtenFOpT, typename AtenOpT>
class DecomposeAtenFMaxMinOp : public OpRewritePattern<AtenFOpT> {
public:
using OpRewritePattern<AtenFOpT>::OpRewritePattern;
LogicalResult matchAndRewrite(AtenFOpT op,
PatternRewriter &rewriter) const override {
Location loc = op.getLoc();
BaseTensorType outType = cast<BaseTensorType>(op.getType());
Type nanMaskType = outType.getWithSizesAndDtype(
!outType.hasSizes() ? std::optional<ArrayRef<int64_t>>()
: llvm::ArrayRef(outType.getSizes()),
rewriter.getI1Type());
Value self = op.getSelf();
Value other = op.getOther();
Value normalResult =
rewriter.create<AtenOpT>(loc, outType, self, other).getResult();
Value selfIsNan =
rewriter.create<Torch::AtenIsnanOp>(loc, nanMaskType, self).getResult();
Value otherIsNan =
rewriter.create<Torch::AtenIsnanOp>(loc, nanMaskType, other)
.getResult();
normalResult = rewriter.create<Torch::AtenWhereSelfOp>(
loc, outType, otherIsNan, self, normalResult);
rewriter.replaceOpWithNewOp<AtenWhereSelfOp>(op, outType, selfIsNan, other,
normalResult);
return success();
}
};
} // namespace
namespace {
class DecomposeComplexOpsPass
: public DecomposeComplexOpsBase<DecomposeComplexOpsPass> {
@ -8732,6 +8767,11 @@ public:
addPatternIfTargetOpIsIllegal<DecomposeAtenConv2dOp>(patterns);
addPatternIfTargetOpIsIllegal<DecomposeAtenConv3dOp>(patterns);
addPatternIfTargetOpIsIllegal<
DecomposeAtenFMaxMinOp<AtenFmaxOp, AtenMaximumOp>>(patterns);
addPatternIfTargetOpIsIllegal<
DecomposeAtenFMaxMinOp<AtenFminOp, AtenMinimumOp>>(patterns);
GreedyRewriteConfig config;
config.useTopDownTraversal = true;
config.maxIterations = GreedyRewriteConfig::kNoLimit;

View File

@ -544,6 +544,9 @@ static void markDecomposedOpsAsIllegal(MLIRContext *context,
target.addIllegalOp<AtenTriuOp>();
target.addIllegalOp<AtenTriuIndicesOp>();
target.addIllegalOp<AtenLinalgNormOp>();
target.addIllegalOp<AtenFminOp>();
target.addIllegalOp<AtenFmaxOp>();
for (auto &opName : backendLegalOpsSet) {
target.addLegalOp(
OperationName(kTorchOpPrefix + opName.first().str(), context));

View File

@ -1673,6 +1673,8 @@ TOSA_PASS_SET = {
"ElementwiseFlattenBroadcastModule_basic",
"ElementwiseFloorIntModule_basic",
"ElementwiseFloorModule_basic",
"ElementwiseFmaxModule_basic",
"ElementwiseFminModule_basic",
"ElementwiseGeFloatIntScalarModule_basic",
"ElementwiseGeFloatScalarModule_basic",
"ElementwiseGeIntScalarModule_basic",
@ -2215,6 +2217,8 @@ ONNX_XFAIL_SET = {
"ElementwiseAtenFloorDivideTensorNegativeModule_basic",
"ElementwiseLog10IntModule_basic",
"ElementwiseLog2IntModule_basic",
"ElementwiseFminModule_basic",
"ElementwiseFmaxModule_basic",
"FlipModuleStaticShape_basic",
"FlipNegativeIndexModule_basic",
"PixelShuffleModuleStaticRank4Float32_basic",

View File

@ -1403,6 +1403,12 @@ def atenminimum〡shape(self: List[int], other: List[int]) -> List[int]:
def atenmaximum〡shape(self: List[int], other: List[int]) -> List[int]:
return upstream_shape_functions.broadcast(self, other)
def atenfmin〡shape(self: List[int], other: List[int]) -> List[int]:
return upstream_shape_functions.broadcast(self, other)
def atenfmax〡shape(self: List[int], other: List[int]) -> List[int]:
return upstream_shape_functions.broadcast(self, other)
def atenbitwise_orTensor〡shape(self: List[int], other: List[int]) -> List[int]:
return upstream_shape_functions.broadcast(self, other)
@ -3655,6 +3661,22 @@ def atenminimum〡dtype(self_rank_dtype: Tuple[int, int], other_rank_dtype: T
dtypes = [self_dtype, other_dtype]
return promote_dtypes(ranks, dtypes)
@check_dtype_function(_check_two_tensor_op())
def atenfmax〡dtype(self_rank_dtype: Tuple[int, int], other_rank_dtype: Tuple[int, int]) -> int:
other_rank, other_dtype = other_rank_dtype
self_rank, self_dtype = self_rank_dtype
ranks: List[Optional[int]] = [self_rank, other_rank]
dtypes = [self_dtype, other_dtype]
return promote_dtypes(ranks, dtypes)
@check_dtype_function(_check_two_tensor_op())
def atenfmin〡dtype(self_rank_dtype: Tuple[int, int], other_rank_dtype: Tuple[int, int]) -> int:
other_rank, other_dtype = other_rank_dtype
self_rank, self_dtype = self_rank_dtype
ranks: List[Optional[int]] = [self_rank, other_rank]
dtypes = [self_dtype, other_dtype]
return promote_dtypes(ranks, dtypes)
@check_dtype_function(
_check_tensors_with_the_same_dtype(tensor_shapes=[(3, 4), (4, 3)]) +
# Different width

View File

@ -463,6 +463,8 @@ def emit_ops(emitter_td: TextEmitter, registry: Registry):
)
emit("aten::maximum : (Tensor, Tensor) -> (Tensor)")
emit("aten::minimum : (Tensor, Tensor) -> (Tensor)")
emit("aten::fmax : (Tensor, Tensor) -> (Tensor)")
emit("aten::fmin : (Tensor, Tensor) -> (Tensor)")
emit("aten::mish : (Tensor) -> (Tensor)")
emit("aten::xlogy.Tensor : (Tensor, Tensor) -> (Tensor)")
emit(

View File

@ -1440,6 +1440,64 @@ def ElementwiseMaximumIntModule_basic(module, tu: TestUtils):
# ==============================================================================
class ElementwiseFmaxModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args(
[
None,
([-1], torch.float32, True),
([-1], torch.float32, True),
]
)
def forward(self, x, y):
return torch.ops.aten.fmax(x, y)
@register_test_case(module_factory=lambda: ElementwiseFmaxModule())
def ElementwiseFmaxModule_basic(module, tu: TestUtils):
module.forward(tu.rand(4), tu.rand(4))
module.forward(tu.rand(4), torch.tensor([1.0, torch.nan, -0.5, -0.3]))
module.forward(
torch.tensor([0.8, torch.nan, torch.nan, -0.3]),
torch.tensor([1.0, torch.nan, -0.4, torch.nan]),
)
# ==============================================================================
class ElementwiseFminModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args(
[
None,
([-1], torch.float32, True),
([-1], torch.float32, True),
]
)
def forward(self, x, y):
return torch.ops.aten.fmin(x, y)
@register_test_case(module_factory=lambda: ElementwiseFminModule())
def ElementwiseFminModule_basic(module, tu: TestUtils):
module.forward(tu.rand(4), tu.rand(4))
module.forward(tu.rand(4), torch.tensor([1.0, torch.nan, -0.5, -0.3]))
module.forward(
torch.tensor([0.8, torch.nan, torch.nan, -0.3]),
torch.tensor([1.0, torch.nan, -0.4, torch.nan]),
)
# ==============================================================================
class ElementwiseMaxOtherModule(torch.nn.Module):
def __init__(self):
super().__init__()