-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support amax dynamo converter (#2241)
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
matmul, | ||
normalization, | ||
permutation, | ||
reduce, | ||
select, | ||
shape, | ||
slice, | ||
|
This file contains 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,35 @@ | ||
from typing import Optional, Tuple, Union | ||
|
||
import tensorrt as trt | ||
from torch.fx.node import Target | ||
from torch_tensorrt.dynamo._SourceIR import SourceIR | ||
from torch_tensorrt.dynamo.conversion.converter_utils import ( | ||
cast_trt_tensor, | ||
get_axes_for_reduce_op, | ||
) | ||
from torch_tensorrt.fx.converters.converter_utils import set_layer_name | ||
from torch_tensorrt.fx.types import TRTNetwork, TRTTensor | ||
|
||
|
||
def amax( | ||
network: TRTNetwork, | ||
target: Target, | ||
source_ir: Optional[SourceIR], | ||
name: str, | ||
input_val: TRTTensor, | ||
dim: Union[int, Tuple[int]], | ||
keepdim: bool = False, | ||
) -> TRTTensor: | ||
if (isinstance(input_val, TRTTensor)) and ( | ||
input_val.dtype == trt.int8 or input_val.dtype == trt.int32 | ||
): | ||
input_val = cast_trt_tensor(network, input_val, trt.float32, name) | ||
|
||
layer = network.add_reduce( | ||
input_val, | ||
trt.ReduceOperation.MAX, | ||
axes=get_axes_for_reduce_op(dim), | ||
keep_dims=keepdim, | ||
) | ||
set_layer_name(layer, target, name, source_ir) | ||
return layer.get_output(0) |
This file contains 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,93 @@ | ||
import torch | ||
import torch.nn as nn | ||
from harness import DispatchTestCase | ||
from parameterized import parameterized | ||
from torch.testing._internal.common_utils import run_tests | ||
|
||
|
||
class TestAmaxConverter(DispatchTestCase): | ||
@parameterized.expand( | ||
[ | ||
((3, 2, 4), 1, True), | ||
((2, 3, 4, 5), 3, True), | ||
((2, 3, 4, 5), 2, False), | ||
((6, 7, 5, 4, 5), 4, False), | ||
] | ||
) | ||
def test_amax_dim_int_default(self, input_shape, dim, keep_dims): | ||
class Amax(nn.Module): | ||
def forward(self, x): | ||
return torch.amax(x, dim=dim, keepdim=keep_dims) | ||
|
||
inputs = [torch.randn(*input_shape)] | ||
self.run_test( | ||
Amax(), | ||
inputs, | ||
expected_ops={torch.ops.aten.amax.default}, | ||
) | ||
|
||
@parameterized.expand( | ||
[ | ||
((3, 2, 4), [1], True), | ||
((2, 1, 4, 5), [0, 3], True), | ||
((2, 3, 4, 5), [0, 1, 2, 3], False), | ||
((6, 7, 5, 4, 5), [1, 3, 4], False), | ||
] | ||
) | ||
def test_amax_dim_tuple_default(self, input_shape, dim, keep_dims): | ||
class Amax(nn.Module): | ||
def forward(self, x): | ||
return torch.amax(x, dim=dim, keepdim=keep_dims) | ||
|
||
inputs = [torch.randn(*input_shape)] | ||
self.run_test( | ||
Amax(), | ||
inputs, | ||
expected_ops={torch.ops.aten.amax.default}, | ||
) | ||
|
||
@parameterized.expand( | ||
[ | ||
((3, 2, 4), 1, True, torch.int, 0, 5), | ||
((2, 3, 4, 5), 3, True, torch.int, -10, 10), | ||
((2, 3, 4, 5), 2, False, torch.int32, -5, 0), | ||
((6, 7, 5, 4, 5), 4, False, torch.int32, -5, 5), | ||
] | ||
) | ||
def test_amax_dim_int_int(self, input_shape, dim, keep_dims, dtype, low, high): | ||
class Amax(nn.Module): | ||
def forward(self, x): | ||
return torch.amax(x, dim=dim, keepdim=keep_dims) | ||
|
||
inputs = [torch.randint(low, high, input_shape, dtype=dtype)] | ||
self.run_test( | ||
Amax(), | ||
inputs, | ||
expected_ops={torch.ops.aten.amax.default}, | ||
check_dtype=False, | ||
) | ||
|
||
@parameterized.expand( | ||
[ | ||
((3, 2, 4), [1], True, torch.int, 0, 5), | ||
((2, 1, 4, 5), [0, 3], True, torch.int, -10, 10), | ||
((2, 3, 4, 5), [0, 1, 2, 3], False, torch.int32, -5, 0), | ||
((6, 7, 5, 4, 5), [1, 3, 4], False, torch.int32, -5, 5), | ||
] | ||
) | ||
def test_amax_dim_tuple_int(self, input_shape, dim, keep_dims, dtype, low, high): | ||
class Amax(nn.Module): | ||
def forward(self, x): | ||
return torch.amax(x, dim=dim, keepdim=keep_dims) | ||
|
||
inputs = [torch.randint(low, high, input_shape, dtype=dtype)] | ||
self.run_test( | ||
Amax(), | ||
inputs, | ||
expected_ops={torch.ops.aten.amax.default}, | ||
check_dtype=False, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |