mirror of https://github.com/llvm/torch-mlir
[Torch Dialect][stablehlo] emit aten.rand op and add converter to stablehlo (#2413)
* [Torch Dialect] emit aten.rand op and add converter to stablehlo * add failed tests for torchdynamo backend * add failed test for linalg backendpull/2421/head
parent
8f28d933e1
commit
4339c00f1b
|
@ -16,7 +16,8 @@ from torch_mlir._version import torch_version_for_comparison, version
|
|||
LINALG_XFAIL_SET = COMMON_TORCH_MLIR_LOWERING_XFAILS | {
|
||||
# Lowering Torch Backend IR -> Linalg-on-Tensors Backend IR failed
|
||||
# 'linalg.depthwise_conv_2d_nchw_chw' op inferred input/output operand #1 has shape's dimension #0 to be 4, but found 8
|
||||
"Conv2dWithPaddingDilationStrideStaticModule_depthwise_multiplier"
|
||||
"Conv2dWithPaddingDilationStrideStaticModule_depthwise_multiplier",
|
||||
"RandModule_basic"
|
||||
}
|
||||
|
||||
TORCHDYNAMO_XFAIL_SET = {
|
||||
|
@ -62,6 +63,7 @@ TORCHDYNAMO_XFAIL_SET = {
|
|||
# error: failed to legalize operation 'torch.aten.view' that was explicitly marked illegal
|
||||
"ElementwiseFlattenBroadcastModule_basic",
|
||||
"FlattenRank0Module_basic",
|
||||
"RandModule_basic",
|
||||
"UniformModule_basic",
|
||||
"UniformStaticShapeModule_basic",
|
||||
# error: unsupported by backend contract: tensor with unknown rank
|
||||
|
@ -868,6 +870,7 @@ STABLEHLO_PASS_SET = {
|
|||
"RandIntLowModule_basic",
|
||||
"RandIntModule_basic",
|
||||
"RandIntPinMemoryModule_basic",
|
||||
"RandModule_basic",
|
||||
"UniformStaticShapeModule_basic",
|
||||
"UniformNoCorrelationModule_basic",
|
||||
"TupleModule_basic",
|
||||
|
|
|
@ -4154,6 +4154,33 @@ def Torch_AtenRandLikeOp : Torch_Op<"aten.rand_like", [
|
|||
}];
|
||||
}
|
||||
|
||||
def Torch_AtenRandOp : Torch_Op<"aten.rand", [
|
||||
AllowsTypeRefinement,
|
||||
HasValueSemantics,
|
||||
ReadOnly
|
||||
]> {
|
||||
let summary = "Generated op for `aten::rand : (int[], int?, int?, Device?, bool?) -> (Tensor)`";
|
||||
let arguments = (ins
|
||||
AnyTorchListOfTorchIntType:$size,
|
||||
AnyTorchOptionalIntType:$dtype,
|
||||
AnyTorchOptionalIntType:$layout,
|
||||
AnyTorchOptionalDeviceType:$device,
|
||||
AnyTorchOptionalBoolType:$pin_memory
|
||||
);
|
||||
let results = (outs
|
||||
AnyTorchTensorType:$result
|
||||
);
|
||||
let hasCustomAssemblyFormat = 1;
|
||||
let extraClassDefinition = [{
|
||||
ParseResult AtenRandOp::parse(OpAsmParser &parser, OperationState &result) {
|
||||
return parseDefaultTorchOp(parser, result, 5, 1);
|
||||
}
|
||||
void AtenRandOp::print(OpAsmPrinter &printer) {
|
||||
printDefaultTorchOp(printer, *this, 5, 1);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
def Torch_AtenBernoulliOp : Torch_Op<"aten.bernoulli", [
|
||||
AllowsTypeRefinement,
|
||||
HasValueSemantics,
|
||||
|
|
|
@ -1553,6 +1553,36 @@ LogicalResult ConvertAtenOp<AtenUniformOp>::matchAndRewrite(
|
|||
return success();
|
||||
}
|
||||
|
||||
template <>
|
||||
LogicalResult ConvertAtenOp<AtenRandOp>::matchAndRewrite(
|
||||
AtenRandOp op, OpAdaptor adaptor,
|
||||
ConversionPatternRewriter &rewriter) const {
|
||||
Location loc = op.getLoc();
|
||||
SmallVector<int64_t> size;
|
||||
if (!matchPattern(adaptor.getSize(), m_TorchListOfConstantInts(size))) {
|
||||
return rewriter.notifyMatchFailure(op,
|
||||
"only constant integer size supported");
|
||||
}
|
||||
auto shapeTensor = rewriter.create<stablehlo::ConstantOp>(
|
||||
loc, rewriter.getI64TensorAttr(size));
|
||||
auto outTy = getTypeConverter()->convertType(op.getType());
|
||||
auto outElemTy = outTy.cast<RankedTensorType>().getElementType();
|
||||
|
||||
if (!outElemTy.isa<FloatType>()) {
|
||||
return rewriter.notifyMatchFailure(op, "only float type supported");
|
||||
}
|
||||
|
||||
Value from = rewriter.create<arith::ConstantOp>(
|
||||
loc, rewriter.getFloatAttr(outElemTy, 0.0));
|
||||
from = hlo::scalarToStablehloTensor(rewriter, op, from, outElemTy);
|
||||
Value to = rewriter.create<arith::ConstantOp>(
|
||||
loc, rewriter.getFloatAttr(outElemTy, 1.0));
|
||||
to = hlo::scalarToStablehloTensor(rewriter, op, to, outElemTy);
|
||||
rewriter.replaceOpWithNewOp<stablehlo::RngOp>(
|
||||
op, outTy, from, to, shapeTensor, stablehlo::RngDistribution::UNIFORM);
|
||||
return success();
|
||||
}
|
||||
|
||||
// Converts `aten.empty.memory_format` to `tensor.empty` op.
|
||||
template <>
|
||||
LogicalResult ConvertAtenOp<AtenEmptyMemoryFormatOp>::matchAndRewrite(
|
||||
|
@ -1844,6 +1874,7 @@ void mlir::torch::torch_to_stablehlo::populateBasicOpPatternsAndLegality(
|
|||
INSERT_ATENOP_PATTERN(AtenWhereSelfOp);
|
||||
INSERT_ATENOP_PATTERN(AtenPowTensorTensorOp);
|
||||
INSERT_ATENOP_PATTERN(AtenUniformOp);
|
||||
INSERT_ATENOP_PATTERN(AtenRandOp);
|
||||
INSERT_ATENOP_PATTERN(AtenEmptyMemoryFormatOp);
|
||||
INSERT_ATENOP_PATTERN(AtenFillScalarOp);
|
||||
INSERT_ATENOP_PATTERN(AtenFlipOp);
|
||||
|
|
|
@ -7251,6 +7251,9 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
|
|||
" func.func @\"__torch_mlir_shape_fn.aten.uniform\"(%arg0: !torch.list<int>, %arg1: !torch.float, %arg2: !torch.float, %arg3: !torch.any) -> !torch.list<int> {\n"
|
||||
" return %arg0 : !torch.list<int>\n"
|
||||
" }\n"
|
||||
" func.func @\"__torch_mlir_shape_fn.aten.rand\"(%arg0: !torch.list<int>, %arg1: !torch.optional<int>, %arg2: !torch.optional<int>, %arg3: !torch.optional<Device>, %arg4: !torch.optional<bool>) -> !torch.list<int> {\n"
|
||||
" return %arg0 : !torch.list<int>\n"
|
||||
" }\n"
|
||||
" func.func @\"__torch_mlir_shape_fn.aten.bernoulli.float\"(%arg0: !torch.list<int>, %arg1: !torch.float, %arg2: !torch.any) -> !torch.list<int> {\n"
|
||||
" return %arg0 : !torch.list<int>\n"
|
||||
" }\n"
|
||||
|
@ -8840,6 +8843,18 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
|
|||
" %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.rand\"(%arg0: !torch.list<int>, %arg1: !torch.optional<int>, %arg2: !torch.optional<int>, %arg3: !torch.optional<Device>, %arg4: !torch.optional<bool>) -> !torch.int {\n"
|
||||
" %int6 = torch.constant.int 6\n"
|
||||
" %none = torch.constant.none\n"
|
||||
" %0 = torch.aten.__is__ %arg1, %none : !torch.optional<int>, !torch.none -> !torch.bool\n"
|
||||
" %1 = torch.prim.If %0 -> (!torch.int) {\n"
|
||||
" torch.prim.If.yield %int6 : !torch.int\n"
|
||||
" } else {\n"
|
||||
" %2 = torch.prim.unchecked_cast %arg1 : !torch.optional<int> -> !torch.int\n"
|
||||
" torch.prim.If.yield %2 : !torch.int\n"
|
||||
" }\n"
|
||||
" return %1 : !torch.int\n"
|
||||
" }\n"
|
||||
" func.func @\"__torch_mlir_dtype_fn.aten._unsafe_view\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.list<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"
|
||||
|
|
|
@ -671,6 +671,9 @@ def aten〇copy〡shape(self: List[int], src: List[int], non_blocking: bool = Fa
|
|||
def aten〇uniform〡shape(self: List[int], from_: float = 0., to: float = 1., generator: Any = None) -> List[int]:
|
||||
return self
|
||||
|
||||
def aten〇rand〡shape(size: List[int], dtype: Optional[int] = None, layout: Optional[int] = None, device: Optional[device] = None, pin_memory: Optional[bool] = None) -> List[int]:
|
||||
return size
|
||||
|
||||
@not_present_in_registry
|
||||
def aten〇bernoulli〇float〡shape(self: List[int], p: float = 0.5, generator: Any = None) -> List[int]:
|
||||
return self
|
||||
|
@ -1965,6 +1968,12 @@ def aten〇uniform〡dtype(self_rank_dtype: Tuple[int, int], from_: float = 0.,
|
|||
self_rank, self_dtype = self_rank_dtype
|
||||
return self_dtype
|
||||
|
||||
@check_dtype_function([Invocation([1]),
|
||||
Invocation([1], dtype=torch.float16),
|
||||
Invocation([1], dtype=torch.complex64)])
|
||||
def aten〇rand〡dtype(size: List[int], dtype: Optional[int] = None, layout: Optional[int] = None, device: Optional[device] = None, pin_memory: Optional[bool] = None) -> int:
|
||||
return torch.float32 if dtype is None else dtype
|
||||
|
||||
@check_dtype_function(_check_tensors_with_the_same_dtype(num_of_tensors=1, size=[1]))
|
||||
def aten〇_unsafe_view〡dtype(self_rank_dtype: Tuple[int, int], size: List[int]) -> int:
|
||||
self_rank, self_dtype = self_rank_dtype
|
||||
|
|
|
@ -347,6 +347,7 @@ def emit_ops(emitter_td: TextEmitter, registry: Registry):
|
|||
# Random number generation
|
||||
emit_with_mutating_variants("aten::uniform : (Tensor, float, float, Generator?) -> (Tensor)")
|
||||
emit("aten::rand_like : (Tensor, int?, int?, Device?, bool?, int?) -> (Tensor)")
|
||||
emit("aten::rand : (int[], int?, int?, Device?, bool?) -> (Tensor)")
|
||||
emit("aten::bernoulli : (Tensor, Generator?) -> (Tensor)")
|
||||
emit("aten::bernoulli_.float : (Tensor, float, Generator?) -> (Tensor)")
|
||||
emit("aten::bernoulli.p : (Tensor, float, Generator?) -> (Tensor)")
|
||||
|
|
|
@ -6,6 +6,28 @@ from torch_mlir_e2e_test.annotations import annotate_args, export
|
|||
|
||||
# ==============================================================================
|
||||
|
||||
class RandModule(torch.nn.Module):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
@export
|
||||
@annotate_args([
|
||||
None,
|
||||
([1024, 512], torch.float, True)
|
||||
])
|
||||
def forward(self, x):
|
||||
size = x.size()
|
||||
a = torch.rand(size)
|
||||
return torch.std(a), torch.mean(a)
|
||||
|
||||
|
||||
@register_test_case(module_factory=lambda: RandModule())
|
||||
def RandModule_basic(module, tu: TestUtils):
|
||||
module.forward(tu.rand(1024, 512))
|
||||
|
||||
# ==============================================================================
|
||||
|
||||
class UniformModule(torch.nn.Module):
|
||||
|
||||
def __init__(self):
|
||||
|
|
Loading…
Reference in New Issue