Skip to content

Commit

Permalink
[Torch Dialect][stablehlo] emit aten.rand op and add converter to sta…
Browse files Browse the repository at this point in the history
…blehlo (#2413)

* [Torch Dialect] emit aten.rand op and add converter to stablehlo

* add failed tests for torchdynamo backend

* add failed test for linalg backend
  • Loading branch information
Vremold authored Aug 27, 2023
1 parent 8f28d93 commit 4339c00
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
5 changes: 4 additions & 1 deletion e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
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 = {
Expand Down Expand Up @@ -62,6 +63,7 @@
# 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
Expand Down Expand Up @@ -868,6 +870,7 @@
"RandIntLowModule_basic",
"RandIntModule_basic",
"RandIntPinMemoryModule_basic",
"RandModule_basic",
"UniformStaticShapeModule_basic",
"UniformNoCorrelationModule_basic",
"TupleModule_basic",
Expand Down
27 changes: 27 additions & 0 deletions include/torch-mlir/Dialect/Torch/IR/GeneratedTorchOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions lib/Conversion/TorchToStablehlo/Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions lib/Dialect/Torch/Transforms/AbstractInterpLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def emit_with_mutating_variants(key, **kwargs):
# 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)")
Expand Down
22 changes: 22 additions & 0 deletions python/torch_mlir_e2e_test/test_suite/rng.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@

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

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):
Expand Down

0 comments on commit 4339c00

Please sign in to comment.