Skip to content

Commit

Permalink
[TORCH][MLIR] Add E2E support for aten.empty_like op
Browse files Browse the repository at this point in the history
This commit adds decomposition of `aten.empty_like` into `aten.empty`
op.

Signed-Off-by: Gaurav Shukla <[email protected]>
  • Loading branch information
Shukla-Gaurav authored and Gaurav Shukla committed Dec 16, 2021
1 parent eddc09a commit bc9abbc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
42 changes: 39 additions & 3 deletions e2e_testing/torchscript/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def __init__(self):
None,
])
def forward(self):
return torch.abs(torch.empty((3, 4), dtype=torch.float32)) > -1.0
return torch.pow(torch.empty((3, 4), dtype=torch.float32), 0)

@register_test_case(module_factory=lambda: EmptyFloatModule())
def EmptyModule_float(module, tu: TestUtils):
Expand All @@ -679,15 +679,51 @@ def __init__(self):
None,
])
def forward(self):
return torch.abs(torch.empty((3, 4), dtype=torch.float32,
pin_memory=False)) > -1.0
return torch.pow(torch.empty((3, 4), dtype=torch.float32,
pin_memory=False), 0)

@register_test_case(module_factory=lambda: EmptyFalsePinMemoryModule())
def EmptyModule_falsePinMemory(module, tu: TestUtils):
module.forward()

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

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

@export
@annotate_args([
None,
([-1, -1], torch.int64, True),
])
def forward(self, a):
return 0 * torch.empty_like(a, dtype=torch.int64)

@register_test_case(module_factory=lambda: EmptyLikeIntModule())
def EmptyLikeModule_int(module, tu: TestUtils):
module.forward(torch.randint(10, (3, 5)))

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

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

@export
@annotate_args([
None,
([-1, -1], torch.float32, True),
])
def forward(self, a):
return torch.pow(torch.empty_like(a, dtype=torch.float32), 0)

@register_test_case(module_factory=lambda: EmptyLikeFloatModule())
def EmptyLikeModule_float(module, tu: TestUtils):
module.forward(tu.rand(4, 5))

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

class ContiguousModule(torch.nn.Module):
def __init__(self):
super().__init__()
Expand Down
29 changes: 29 additions & 0 deletions lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,33 @@ class DecomposeAtenLayerNormOp : public OpRewritePattern<AtenLayerNormOp> {
};
} // namespace

// Decompose `aten.empty_like` op into `aten.size` and `aten.empty` ops.
namespace {
class DecomposeAtenEmptyLikeOp : public OpRewritePattern<AtenEmptyLikeOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(AtenEmptyLikeOp op,
PatternRewriter &rewriter) const override {
auto sizeListType =
Torch::ListType::get(Torch::IntType::get(op.getContext()));
Value sizeList =
rewriter.create<AtenSizeOp>(op.getLoc(), sizeListType, op.self());

// TODO: Handle the case when `dtype` is NoneType.
Type dtype = op.dtype().getType();
if (dtype.isa<OptionalType>() || dtype.isa<Torch::NoneType>() ||
dtype.isa<mlir::NoneType>())
return rewriter.notifyMatchFailure(
op, "unimplemented: None dtype is not supported");

rewriter.replaceOpWithNewOp<AtenEmptyMemoryFormatOp>(
op, op.getType(), sizeList, op.dtype(), op.layout(), op.device(),
op.pin_memory(), op.memory_format());
return success();
}
};
} // namespace

namespace {
class DecomposeComplexOpsPass
: public DecomposeComplexOpsBase<DecomposeComplexOpsPass> {
Expand All @@ -521,6 +548,8 @@ class DecomposeComplexOpsPass
target.addIllegalOp<Aten_SoftmaxOp>();
patterns.add<DecomposeAtenLogSoftmaxIntOp>(context);
target.addIllegalOp<AtenLogSoftmaxIntOp>();
patterns.add<DecomposeAtenEmptyLikeOp>(context);
target.addIllegalOp<AtenEmptyLikeOp>();
patterns.add<DecomposeAtenExpandOp>(context);
target.addIllegalOp<AtenExpandOp>();
patterns.add<DecomposeAtenSizeOp>(context);
Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/Torch/Transforms/RefineTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class TypeAnalyzer : public ForwardDataFlowAnalysis<ValueKnowledge> {
AtenTanhOp, AtenBatchNormOp, AtenReluOp, AtenGeluOp,
AtenGeluBackwardOp, AtenBitwiseNotOp, AtenExpOp, AtenSinOp,
AtenCosOp, AtenSigmoidOp, DerefineOp, AtenToPrimDeviceOp, AtenCpuOp,
AtenContiguousOp, AtenFill_ScalarOp, AtenDetachOp,
AtenContiguousOp, AtenFill_ScalarOp, AtenDetachOp, AtenEmptyLikeOp,
AtenMaskedFill_ScalarOp, AtenCopy_Op, AtenCumsumOp, AtenLayerNormOp,
AtenClampOp, AtenLogOp, AtenNegOp, AtenSqrtOp, AtenFloorOp,
AtenLog2Op, Aten_SoftmaxBackwardDataOp, AtenRsqrtOp, AtenDropoutOp,
Expand Down

0 comments on commit bc9abbc

Please sign in to comment.