Skip to content

Commit

Permalink
[MLIR][TORCH] Add TorchToTosa lowering for aten.where.self op
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosLewis authored and AmosLewis committed Oct 4, 2022
1 parent eda18e3 commit b0a1c48
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
"ArangeStartIntModule_basic",
"ArangeStartNegativeStepIntModule_basic",
"ArangeZeroElementOutputModule_basic",
"ElementwiseAtenWhereSelfModule_basic",
}

LTC_XFAIL_SET = {
Expand Down
25 changes: 25 additions & 0 deletions lib/Conversion/TorchToTosa/TorchToTosa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,30 @@ LogicalResult ConvertAtenOp<AtenBroadcastToOp>::matchAndRewrite(
"unimplemented: broadcasts other than same rank or zero ranked tensor.");
}


template <>
LogicalResult ConvertAtenOp<AtenWhereSelfOp>::matchAndRewrite(
AtenWhereSelfOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {

// Not a tensor type.
auto selfType = adaptor.self().getType().dyn_cast<TensorType>();
if (!selfType)
return rewriter.notifyMatchFailure(
op, "Only tensor types input are currently supported");
auto condType = adaptor.condition().getType().dyn_cast<TensorType>();
if (!condType)
return rewriter.notifyMatchFailure(
op, "Only tensor types condition are currently supported");

auto outType = getTypeConverter()->convertType(op.getType());
rewriter.replaceOpWithNewOp<tosa::SelectOp>(op, outType, adaptor.condition(),
adaptor.self(), adaptor.other());

return success();
}


template <>
LogicalResult ConvertAtenOp<AtenArangeStartStepOp>::matchAndRewrite(
AtenArangeStartStepOp op, OpAdaptor adaptor,
Expand Down Expand Up @@ -3703,6 +3727,7 @@ class ConvertTorchToTosa : public ConvertTorchToTosaBase<ConvertTorchToTosa> {
INSERT_ATENOP_PATTERN(AtenMaxDimOp);
INSERT_ATENOP_PATTERN(AtenSliceTensorOp);
INSERT_ATENOP_PATTERN(AtenBroadcastToOp);
INSERT_ATENOP_PATTERN(AtenWhereSelfOp);
INSERT_ATENOP_PATTERN(AtenArangeStartStepOp);
#undef INSERT_ATENOP_PATTERN

Expand Down
24 changes: 24 additions & 0 deletions python/torch_mlir_e2e_test/test_suite/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ def ElementwiseTernaryModule_basic(module, tu: TestUtils):
# ==============================================================================


class ElementwiseAtenWhereSelfModule(torch.nn.Module):

def __init__(self):
super().__init__()

@export
@annotate_args([
None,
([1, 1, 5, 5], torch.bool, True),
([1, 12, 5, 5], torch.float32, True),
([], torch.float32, True),
])
def forward(self, a, b, c):
return torch.ops.aten.where(a, b, c)


@register_test_case(module_factory=lambda: ElementwiseAtenWhereSelfModule())
def ElementwiseAtenWhereSelfModule_basic(module, tu: TestUtils):
module.forward(torch.zeros(1, 1, 5, 5, dtype=torch.bool), torch.rand(1, 12, 5, 5), torch.rand(()))


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


class ElementwiseWhereSelfModule(torch.nn.Module):

def __init__(self):
Expand Down

0 comments on commit b0a1c48

Please sign in to comment.