Skip to content

feat: support aten.sort dynamo converter #2514

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

Merged
merged 1 commit into from
Dec 12, 2023
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
24 changes: 24 additions & 0 deletions py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,3 +2505,27 @@ def upsample_bilinear2d(
resize_mode="bilinear",
align_corners=args_bounds_check(args, 2),
)


@dynamo_tensorrt_converter(torch.ops.aten.sort.default)
@enforce_tensor_types(
{
0: (TRTTensor,),
}
)
def aten_ops_sort(
ctx: ConversionContext,
target: Target,
args: Tuple[Argument, ...],
kwargs: Dict[str, Argument],
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
return impl.topk.sort(
ctx,
target,
SourceIR.ATEN,
name,
args[0],
dim=args_bounds_check(args, 1, -1),
descending=args_bounds_check(args, 2, False),
)
35 changes: 34 additions & 1 deletion py/torch_tensorrt/dynamo/conversion/impl/topk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Tuple, Union

import tensorrt as trt
from torch.fx.node import Target
Expand Down Expand Up @@ -101,3 +101,36 @@ def argmin(
return argmax_argmin(
ctx, target, source_ir, name, input, trt.TopKOperation.MIN, dim, keep_dim
)


def sort(
ctx: ConversionContext,
target: Target,
source_ir: Optional[SourceIR],
name: str,
input: TRTTensor,
dim: int,
descending: bool,
return_indices: bool = True,
) -> Union[TRTTensor, Tuple[TRTTensor, TRTTensor]]:
if descending:
topk_layer = ctx.net.add_topk(
input,
trt.TopKOperation.MAX,
input.shape[dim],
get_axes_for_reduce_op(get_positive_dim(dim, len(input.shape))),
)
else:
topk_layer = ctx.net.add_topk(
input,
trt.TopKOperation.MIN,
input.shape[dim],
get_axes_for_reduce_op(get_positive_dim(dim, len(input.shape))),
)

set_layer_name(topk_layer, target, name, source_ir)

if return_indices:
return topk_layer.get_output(0), topk_layer.get_output(1)
else:
return topk_layer.get_output(0)
34 changes: 34 additions & 0 deletions tests/py/dynamo/conversion/test_sort_aten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import torch
import torch.nn as nn
from parameterized import parameterized
from torch.testing._internal.common_utils import run_tests

from .harness import DispatchTestCase


class TestSortConverter(DispatchTestCase):
@parameterized.expand(
[
((3, 2, 4), 0, True),
((2, 3, 4, 5), 1, True),
((2, 3, 4, 5), 2, False),
((6, 7, 5, 4, 5), 4, False),
((1, 5, 2, 1), -1, True),
((1, 2, 5, 3), -2, False),
((6, 2, 1, 3), -4, True),
]
)
def test_sort(self, input_shape, dim, descending):
class Sort(nn.Module):
def forward(self, x):
return torch.ops.aten.sort.default(x, dim, descending)

inputs = [torch.randn(*input_shape)]
self.run_test(
Sort(),
inputs,
)


if __name__ == "__main__":
run_tests()