Skip to content

Commit

Permalink
Merge pull request #226 from Xilinx/bump_to_42a16fa9
Browse files Browse the repository at this point in the history
Merge with fixes of 42a16fa (2)
  • Loading branch information
mgehre-amd authored Aug 14, 2024
2 parents c852b7f + b71da97 commit 20759b3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions include/torch-mlir/Dialect/Torch/IR/GeneratedTorchOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -11052,6 +11052,31 @@ def Torch_AtenToDeviceOp : Torch_Op<"aten.to.device", [
}];
}

def Torch_Aten_CastFloatOp : Torch_Op<"aten._cast_Float", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::_cast_Float : (Tensor, bool) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self,
Torch_BoolType:$non_blocking
);
let results = (outs
AnyTorchOptionalTensorType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult Aten_CastFloatOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 2, 1);
}
void Aten_CastFloatOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 2, 1);
}
}];
let hasCanonicalizer = 1;
}

def Torch_AtenTypeAsOp : Torch_Op<"aten.type_as", [
AllowsTypeRefinement,
HasValueSemantics,
Expand Down
21 changes: 21 additions & 0 deletions lib/Dialect/Torch/IR/TorchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,27 @@ void AtenToOtherOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
});
}

//===----------------------------------------------------------------------===//
// Aten_CastFloatOp
//===----------------------------------------------------------------------===//

void Aten_CastFloatOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
// `aten.cast_float` -> `aten.to.dtype`
patterns.add(+[](Aten_CastFloatOp op, PatternRewriter &rewriter) {
auto self = op.getSelf();
auto loc = op.getLoc();
Value constNone = rewriter.create<ConstantNoneOp>(loc);
Value f32Type = rewriter.create<ConstantIntOp>(
loc, (int)torch_upstream::ScalarType::Float);
Value constFalse = rewriter.create<ConstantBoolOp>(loc, false);
rewriter.replaceOpWithNewOp<AtenToDtypeOp>(op, op.getType(), self, f32Type,
op.getNonBlocking(), constFalse,
constNone);
return success();
});
}

//===----------------------------------------------------------------------===//
// AtenViewOp
//===----------------------------------------------------------------------===//
Expand Down
8 changes: 8 additions & 0 deletions lib/Dialect/Torch/Transforms/AbstractInterpLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6850,6 +6850,10 @@ 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._cast_Float\"(%arg0: !torch.list<int>, %arg1: !torch.bool) -> !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.type_as\"(%arg0: !torch.list<int>, %arg1: !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"
Expand Down Expand Up @@ -13001,6 +13005,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
" %0:2 = torch.prim.TupleUnpack %arg1 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
" return %0#1 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten._cast_Float\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.bool) -> !torch.int {\n"
" %int6 = torch.constant.int 6\n"
" return %int6 : !torch.int\n"
" }\n"
" func.func @\"__torch_mlir_dtype_fn.aten.type_as\"(%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"
" return %0#1 : !torch.int\n"
Expand Down
2 changes: 2 additions & 0 deletions projects/pt1/e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@
"AtenSubFloatModule_basic",
"AtenToDeviceModule_basic",
"AtenToDtypeModule_basic",
"Aten_CastFloatModule_basic",
"AvgPool1dStaticModule_basic",
"AvgPool2dStaticModule_basic",
"AvgPool3dStaticModule_basic",
Expand Down Expand Up @@ -1010,6 +1011,7 @@
"AtenInstanceNormModule_basic",
"AtenToDeviceModule_basic",
"AtenToDtypeModule_basic",
"Aten_CastFloatModule_basic",
"BaddbmmBroadcast1DInputModule_basic",
"BaddbmmBroadcast2DInputModule_basic",
"BaddbmmDynamicModule_basic",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ def aten〇to〇device〡shape(self: List[int], device: device, dtype: int, non_
def aten〇to〇other〡shape(self: List[int], other: List[int], non_blocking: bool = False, copy: bool = False, memory_format: Optional[int] = None) -> List[int]:
return upstream_shape_functions.unary(self)

def aten〇_cast_Float〡shape(self: List[int], non_blocking: bool = False) -> List[int]:
return upstream_shape_functions.unary(self)

def aten〇type_as〡shape(self: List[int], other: List[int]) -> List[int]:
return upstream_shape_functions.unary(self)

Expand Down Expand Up @@ -4446,6 +4449,9 @@ def aten〇to〇other〡dtype(self_rank_dtype: Tuple[int, int], other_rank_dtype
other_rank, other_dtype = other_rank_dtype
return other_dtype

def aten〇_cast_Float〡dtype(self_rank_dtype: Tuple[int, int], non_blocking: bool = False) -> int:
return torch.float32

@check_dtype_function(_check_two_tensor_op())
def aten〇type_as〡dtype(self_rank_dtype: Tuple[int, int], other_rank_dtype: Tuple[int, int]) -> int:
other_rank, other_dtype = other_rank_dtype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ def emit_with_mutating_variants(key, **kwargs):
emit("aten::to.other : (Tensor, Tensor, bool, bool, int?) -> (Tensor)", has_canonicalizer=True)
emit("aten::to.prim_Device : (Tensor, Device?, int?, bool, bool) -> (Tensor)")
emit("aten::to.device : (Tensor, Device, int, bool, bool, int?) -> (Tensor)")
emit("aten::_cast_Float : (Tensor, bool) -> (Tensor)", has_canonicalizer=True)
emit("aten::type_as : (Tensor, Tensor) -> (Tensor)")
emit("aten::view : (Tensor, int[]) -> (Tensor)", has_folder=True)
emit("aten::_unsafe_view : (Tensor, int[]) -> (Tensor)")
Expand Down
20 changes: 20 additions & 0 deletions projects/pt1/python/torch_mlir_e2e_test/test_suite/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4450,6 +4450,26 @@ def AtenToDtypeModule_basic(module, tu: TestUtils):
# ==============================================================================


class Aten_CastFloatModule(torch.nn.Module):
def __init__(self):
super().__init__()

@export
@annotate_args([
None,
([2, 4], torch.int64, True),
])

def forward(self, val):
return torch.ops.aten._cast_Float(val)

@register_test_case(module_factory=lambda: Aten_CastFloatModule())
def Aten_CastFloatModule_basic(module, tu: TestUtils):
module.forward(tu.randint(2, 4))


# ==============================================================================

class UpSampleNearest2dBackward(torch.nn.Module):

def __init__(self):
Expand Down

0 comments on commit 20759b3

Please sign in to comment.