[Stablehlo] support aten.isfinite (#3850)

byteir
yyp0 2024-11-07 16:52:39 +08:00 committed by Yuanqiang Liu
parent 6413e4a687
commit 83c27837f4
7 changed files with 86 additions and 0 deletions

View File

@ -4921,6 +4921,29 @@ def Torch_AtenFakeQuantizePerChannelAffineCachemaskOp : Torch_Op<"aten.fake_quan
}];
}
def Torch_AtenIsfiniteOp : Torch_Op<"aten.isfinite", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::isfinite : (Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenIsfiniteOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 1, 1);
}
void AtenIsfiniteOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 1, 1);
}
}];
}
def Torch_AtenMaximumOp : Torch_Op<"aten.maximum", [
AllowsTypeRefinement,
HasValueSemantics,

View File

@ -2075,6 +2075,30 @@ LogicalResult ConvertAtenOp<AtenTrilOp>::matchAndRewrite(
return success();
}
template <>
LogicalResult ConvertAtenOp<AtenIsfiniteOp>::matchAndRewrite(
AtenIsfiniteOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
Value self = adaptor.getSelf();
auto selfTy = cast<RankedTensorType>(self.getType());
if (!selfTy)
return rewriter.notifyMatchFailure(
op, "Only Tensor types are currently supported");
auto outType =
dyn_cast<RankedTensorType>(getTypeConverter()->convertType(op.getType()));
Type outElemTy = outType.getElementType();
if (!outElemTy.isInteger(1)) {
return rewriter.notifyMatchFailure(
op, "Only i1 output element type is supported");
}
rewriter.replaceOpWithNewOp<stablehlo::IsFiniteOp>(op.getOperation(), outType,
self);
return success();
}
void mlir::torch::torch_to_stablehlo::populateBasicOpPatternsAndLegality(
TypeConverter &typeConverter, RewritePatternSet &patterns,
ConversionTarget &target, const TorchToStablehloOptions &options) {
@ -2248,6 +2272,7 @@ void mlir::torch::torch_to_stablehlo::populateBasicOpPatternsAndLegality(
INSERT_ATENOP_PATTERN(AtenBitwiseRightShiftTensorOp);
INSERT_ATENOP_PATTERN(AtenTrilOp);
INSERT_ATENOP_PATTERN(AtenIsfiniteOp);
#undef INSERT_ATENOP_PATTERN
#define INSERT_BINARY_BROADCAST_PATTERN(AtenOp, StablehloOp) \

View File

@ -6495,6 +6495,9 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %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.isfinite\"(%arg0: !torch.list<int>) -> !torch.list<int> {\n"
" return %arg0 : !torch.list<int>\n"
" }\n"
" func.func @\"__torch_mlir_shape_fn.aten.cosine_similarity\"(%arg0: !torch.list<int>, %arg1: !torch.list<int>, %arg2: !torch.int, %arg3: !torch.float) -> !torch.list<int> {\n"
" %none = torch.constant.none\n"
" %int1 = torch.constant.int 1\n"
@ -11170,6 +11173,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %1 = call @__torch__._get_dtype_of_floating_point_op(%0#1) : (!torch.int) -> !torch.int\n"
" return %1 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.isfinite\"(%arg0: !torch.tuple<int, int>) -> !torch.int {\n"
" %int11 = torch.constant.int 11\n"
" return %int11 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.rad2deg\"(%arg0: !torch.tuple<int, int>) -> !torch.int {\n"
" %none = torch.constant.none\n"
" %str = torch.constant.str \"AssertionError: \"\n"

View File

@ -533,6 +533,7 @@ FX_IMPORTER_XFAIL_SET = {
"IndexPutImpl2DNoneIndexStaticModule_basic",
"IndexPutImpl3DFloatNonAccumulateModule_basic",
"IndexPutImplIndexWithNoneModule_basic",
"IsInfiniteModule_basic",
"InterpolateDynamicModule_sizes_nearest",
"IouOfModule_basic",
"MeshgridIndexingIJ_basic",

View File

@ -222,6 +222,9 @@ def atenexp2〡shape(self: List[int]) -> List[int]:
def atenexpm1〡shape(self: List[int]) -> List[int]:
return upstream_shape_functions.unary(self)
def atenisfinite〡shape(self: List[int]) -> List[int]:
return self
def atencosine_similarity〡shape(x1: List[int], x2: List[int], dim: int = 1, eps: float = 1e-08) -> List[int]:
broadcast = upstream_shape_functions.broadcast(x1, x2)
return broadcast[:dim] + broadcast[dim + 1:]
@ -2538,6 +2541,9 @@ def atenexpm1〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
self_rank, self_dtype = self_rank_dtype
return _get_dtype_of_floating_point_op(self_dtype)
def atenisfinite〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
return torch.bool
@check_dtype_function(_check_tensors_with_the_same_dtype(num_of_tensors=1, error_types={torch.complex64, torch.complex128}))
def atenrad2deg〡dtype(self_rank_dtype: Tuple[int, int]) -> int:
self_rank, self_dtype = self_rank_dtype

View File

@ -483,6 +483,7 @@ def emit_ops(emitter_td: TextEmitter, registry: Registry):
emit(
"aten::fake_quantize_per_channel_affine_cachemask : (Tensor, Tensor, Tensor, int, int, int) -> (Tensor, Tensor)"
)
emit("aten::isfinite : (Tensor) -> (Tensor)")
emit("aten::maximum : (Tensor, Tensor) -> (Tensor)")
emit("aten::minimum : (Tensor, Tensor) -> (Tensor)")
emit("aten::fmax : (Tensor, Tensor) -> (Tensor)")

View File

@ -4226,6 +4226,29 @@ def PowIntFloatModule_basic(module, tu: TestUtils):
# ==============================================================================
class IsInfiniteModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args(
[
None,
([-1], torch.float32, True),
]
)
def forward(self, x):
return torch.ops.aten.isfinite(x)
@register_test_case(module_factory=lambda: IsInfiniteModule())
def IsInfiniteModule_basic(module, tu: TestUtils):
module.forward(torch.tensor([-torch.inf, torch.inf, torch.nan, -2.3, 0.0, 1.5]))
# ==============================================================================
class BaddbmmDynamicModule(torch.nn.Module):
def __init__(self):
super().__init__()