Skip to content

full_like evaluator #2917

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

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 47 additions & 0 deletions py/torch_tensorrt/dynamo/conversion/ops_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from torch_tensorrt.dynamo.conversion.impl.elementwise import sub, trunc_div
from torch_tensorrt.fx.types import TRTTensor
from torch_tensorrt.fx.utils import Frameworks, unified_dtype_converter
Comment on lines 20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have our own types and switch from unified_dtype_converter to using _enums.dtype.to and _enums.dtype.from for type conversions. We shouldn't be using FX utils anymore.


_LOGGER: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,3 +166,49 @@ def aten_ops_randperm(
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
return np.random.permutation(args[0])


def full_validator(full_like_node: Node) -> bool:
device = full_like_node.kwargs.get("device", None)
if device is not None:
_LOGGER.debug(f"Currently we don't support specifying device, got {device}.")
return False
layout = full_like_node.kwargs.get("layout", None)
if layout is not None:
_LOGGER.debug(f"Currently we don't support specifying layout, got {layout}.")
return False
memory_format = full_like_node.kwargs.get("memory_format", None)
if memory_format is not None:
_LOGGER.debug(
f"Currently we don't support specifying memory_format, got {memory_format}."
)
return False
return True


@dynamo_tensorrt_converter(
torch.ops.aten.full_like.default, capability_validator=full_validator
)
def aten_ops_full_like(
ctx: ConversionContext,
target: Target,
args: Tuple[Argument, ...],
kwargs: Dict[str, Argument],
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
full_like_np_tensor = None
tensor_shape = args[0].shape
if kwargs.get("dtype") is not None:
full_like_np_tensor = np.full(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also add dynamic shape support ? what if the input shape has -1 in it ? Please refer to fill layer which can help you out here.

tensor_shape,
args[1],
dtype=unified_dtype_converter(kwargs.get("dtype"), Frameworks.NUMPY),
)
else:
# default returns np.float64. Verify the correctness of this
full_like_np_tensor = np.full(
tensor_shape,
args[1],
dtype=unified_dtype_converter(args[0].dtype, Frameworks.NUMPY),
)
return full_like_np_tensor
80 changes: 80 additions & 0 deletions tests/py/dynamo/conversion/test_full_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import numpy as np
import torch
import torch.nn as nn
import torch_tensorrt
from parameterized import parameterized
from torch.testing._internal.common_utils import run_tests

from .harness import DispatchTestCase

full_like_ops = [
(
"full_like_one_dimension",
torch.empty(1),
1,
None,
),
(
"full_like_two_dimension",
torch.empty(2, 3),
1,
None,
),
(
"full_like_three_dimension",
torch.empty(2, 3, 4),
1,
None,
),
(
"full_like_one_dimension_dtype",
torch.empty(1),
1,
torch.float32,
),
(
"full_like_two_dimension_dtype",
torch.empty(2, 3),
1,
torch.float32,
),
(
"full_like_four_dimension_dtype",
torch.empty(1, 2, 2, 1),
1,
torch.float32,
),
(
"full_like_five_dimension_dtype",
torch.empty(1, 2, 3, 2, 1),
1,
torch.float32,
),
]


class Testfull_likeConverter(DispatchTestCase):
@parameterized.expand(
[
(full_like_op[0], full_like_op[1], full_like_op[2], full_like_op[3])
for full_like_op in full_like_ops
]
)
def test_full_like(self, name, tensor, value, data_type):
class TestModule(nn.Module):
def __init__(self):
super().__init__()

def forward(self, tensor):
return torch.ops.aten.full_like.default(
tensor,
value,
dtype=data_type,
)

inputs = [tensor]
self.run_test(TestModule(), inputs)


if __name__ == "__main__":
run_tests()
Loading