-
Notifications
You must be signed in to change notification settings - Fork 363
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
Closed
full_like evaluator #2917
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__name__) | ||
|
||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.