Skip to content

feat: support aten.trunc dynamo converter #2543

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 2 commits into from
Dec 28, 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
22 changes: 22 additions & 0 deletions py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2541,3 +2541,25 @@ def aten_ops_sort(
dim=args_bounds_check(args, 1, -1),
descending=args_bounds_check(args, 2, False),
)


@dynamo_tensorrt_converter(torch.ops.aten.trunc.default)
@enforce_tensor_types(
{
0: (TRTTensor,),
}
)
def aten_ops_trunc(
ctx: ConversionContext,
target: Target,
args: Tuple[Argument, ...],
kwargs: Dict[str, Argument],
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
return impl.unary.trunc(
ctx,
target,
SourceIR.ATEN,
name,
args[0],
)
29 changes: 28 additions & 1 deletion py/torch_tensorrt/dynamo/conversion/impl/unary/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from torch.fx.node import Target
from torch_tensorrt.dynamo._SourceIR import SourceIR
from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext
from torch_tensorrt.dynamo.conversion.converter_utils import cast_trt_tensor
from torch_tensorrt.dynamo.conversion.converter_utils import (
cast_trt_tensor,
get_trt_tensor,
)
from torch_tensorrt.dynamo.conversion.impl.unary.base import convert_unary
from torch_tensorrt.fx.types import TRTTensor

Expand Down Expand Up @@ -432,3 +435,27 @@ def erf(
return convert_unary(
ctx, target, source_ir, name, trt.UnaryOperation.ERF, input_val
)


def trunc(
ctx: ConversionContext,
target: Target,
source_ir: Optional[SourceIR],
name: str,
input_val: TRTTensor,
) -> TRTTensor:
if input_val.dtype not in (trt.float16, trt.float32):
return impl.cast.to_copy(
ctx,
target,
source_ir,
f"{name}_copy",
input_val,
input_val.dtype,
force_layer=True,
)

dividend = get_trt_tensor(ctx, 1, f"{name}_dividend")
return impl.elementwise.trunc_div(
ctx, target, source_ir, f"{name}_trunc", input_val, dividend
)
Comment on lines +459 to +461
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the documentation of torch.trunc, it seems that the behavior differs based on the data type. Specifically, for float inputs, the output is also a float, and for int inputs, the output is also an int. Does this line up with the behavior of trunc_div as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, could a line be added such as

if dtype not in (float16, float32):
   return input_val

This can help to avoid unnecessary layer insertion

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I added the code below since TRT engine inputs cannot be TRT engine outputs:

if input_val.dtype not in (trt.float16, trt.float32):
        return impl.cast.to_copy(
            ctx,
            target,
            source_ir,
            f"{name}_copy",
            input_val,
            input_val.dtype,
            force_layer=True,
        )

52 changes: 52 additions & 0 deletions tests/py/dynamo/conversion/test_trunc_aten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 TestTruncConverter(DispatchTestCase):
@parameterized.expand(
[
((10,),),
((1, 20),),
((2, 3, 4),),
((2, 3, 4, 5),),
]
)
def test_trunc_float(self, shape):
class Trunc(nn.Module):
def forward(self, input):
return torch.ops.aten.trunc.default(input)

inputs = [torch.randn(shape)]
self.run_test(
Trunc(),
inputs,
enable_passes=True,
)

@parameterized.expand(
[
((10,),),
((1, 20),),
((2, 3, 4),),
((2, 3, 4, 5),),
]
)
def test_trunc_int(self, shape):
class Trunc(nn.Module):
def forward(self, input):
return torch.ops.aten.trunc.default(input)

inputs = [torch.randint(-10, 10, shape, dtype=torch.int32)]
self.run_test(
Trunc(),
inputs,
enable_passes=True,
)


if __name__ == "__main__":
run_tests()