Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[torch-frontend] support aten.sort with ascending=True #496

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,6 @@ class ConvertAtenSortOp : public OpConversionPattern<AtenSortOp> {
return rewriter.notifyMatchFailure(op, "unimplemented: "
"descending is not constant bool");
}
if (descending == false) {
return rewriter.notifyMatchFailure(op,
"unsupported: descending == false");
}
int64_t k = inputType.getDimSize(dim);
if (k == ShapedType::kDynamic) {
return rewriter.notifyMatchFailure(op, "sorted dim must be static");
Expand All @@ -638,7 +634,18 @@ class ConvertAtenSortOp : public OpConversionPattern<AtenSortOp> {

auto customCallOp = rewriter.create<stablehlo::CustomCallOp>(
op->getLoc(), resultTypes, bufferArgs, ArrayRef<NamedAttribute>(attrs));
rewriter.replaceOp(op, customCallOp->getResults());
Value values = customCallOp->getResults()[0];
Value indices = customCallOp->getResults()[1];
if (descending == false) {
values = rewriter.create<stablehlo::ReverseOp>(
op->getLoc(), values.getType(), values,
rewriter.getDenseI64ArrayAttr({dim}));
indices = rewriter.create<stablehlo::ReverseOp>(
op->getLoc(), indices.getType(), indices,
rewriter.getDenseI64ArrayAttr({dim}));
}
rewriter.replaceOp(op, {values, indices});

return success();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ def test_sort():
inputs = [tu.randn(3, 10)]
custom_test_helper(SortModule(), inputs, "byteir.top_k")

class SortAscendingModule(torch.nn.Module):
def forward(self, x):
return torch.sort(x, dim=-1, descending=False)

@pytest.mark.mhlo_tools
def test_sort_ascending():
inputs = [tu.randn(3, 10)]
custom_test_helper(SortAscendingModule(), inputs, "byteir.top_k")

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

class MaxDimModule(torch.nn.Module):
Expand Down
2 changes: 1 addition & 1 deletion frontends/torch-frontend/torch-frontend/python/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.2
1.3.3
Loading