Skip to content

Commit ed20f09

Browse files
authored
Reorg for converters tanh (FX Converter Refactor [4/N]) <Target: converter_reorg_proto> (#1900)
1 parent f65340d commit ed20f09

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

py/torch_tensorrt/fx/converters/acc_ops_converters.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1169,15 +1169,12 @@ def acc_ops_tanh(
11691169
kwargs: Dict[str, Argument],
11701170
name: str,
11711171
) -> Union[TRTTensor, Sequence[TRTTensor]]:
1172-
input_val = kwargs["input"]
1173-
operation_type = trt.ActivationType.TANH
1174-
return activation.convert_activation(
1172+
return activation.tanh(
11751173
network,
11761174
target,
11771175
SourceIR.ACC,
11781176
name,
1179-
operation_type,
1180-
input_val,
1177+
kwargs["input"],
11811178
)
11821179

11831180

py/torch_tensorrt/fx/converters/aten_ops_converters.py

+18
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,24 @@ def aten_ops_reshape(
367367
return layer.get_output(0)
368368

369369

370+
@tensorrt_converter(torch.ops.aten.tanh.default)
371+
def aten_ops_tanh(
372+
network: TRTNetwork,
373+
target: Target,
374+
args: Tuple[Argument, ...],
375+
kwargs: Dict[str, Argument],
376+
name: str,
377+
) -> Union[TRTTensor, Sequence[TRTTensor]]:
378+
379+
return activation.tanh(
380+
network,
381+
target,
382+
SourceIR.ATEN,
383+
name,
384+
args[0],
385+
)
386+
387+
370388
@tensorrt_converter(torch.ops.aten.cat.default)
371389
def aten_ops_cat(
372390
network: TRTNetwork,

py/torch_tensorrt/fx/converters/impl/activation.py

+27
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,30 @@ def sigmoid_fn(x):
148148
input_val,
149149
dyn_range_fn=sigmoid_dyn_range_fn,
150150
)
151+
152+
153+
def tanh(
154+
network: TRTNetwork,
155+
target: Target,
156+
source_ir: Optional[SourceIR],
157+
name: str,
158+
input_val: TRTTensor,
159+
):
160+
operation_type = trt.ActivationType.TANH
161+
162+
def tanh_dyn_range_fn(dyn_range):
163+
def tanh_fn(x):
164+
# TODO: Can this just call torch.nn.functional.tanh?
165+
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
166+
167+
return tanh_fn(dyn_range[0]), tanh_fn(dyn_range[1])
168+
169+
return convert_activation(
170+
network,
171+
target,
172+
source_ir,
173+
name,
174+
operation_type,
175+
input_val,
176+
dyn_range_fn=tanh_dyn_range_fn,
177+
)

py/torch_tensorrt/fx/converters/nn_ops_converters.py

+15
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ def hardtanh(network, submod, args, kwargs, layer_name):
5151
name=layer_name,
5252
input_val=kwargs["input"],
5353
)
54+
55+
56+
@tensorrt_converter(torch.nn.functional.tanh)
57+
@tensorrt_converter(torch.nn.modules.activation.Tanh)
58+
def tanh(network, submod, args, kwargs, layer_name):
59+
# args/kwargs should have already been normalized to kwargs
60+
assert len(args) == 0
61+
62+
return activation.tanh(
63+
network=network,
64+
target="torch.nn.modules.activation.Tanh",
65+
source_ir=SourceIR.NN,
66+
name=layer_name,
67+
input_val=kwargs["input"],
68+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import torch
2+
import torch.nn as nn
3+
from torch.testing._internal.common_utils import run_tests
4+
from torch_tensorrt.fx.tools.common_fx2trt import DispatchTestCase, InputTensorSpec
5+
6+
7+
class TestTanhConverter(DispatchTestCase):
8+
def test_tanh(self):
9+
class TestModule(nn.Module):
10+
def forward(self, x):
11+
return nn.functional.tanh(x)
12+
13+
inputs = [torch.randn(1, 10)]
14+
self.run_test(TestModule(), inputs, expected_ops={torch.ops.aten.tanh.default})
15+
16+
def test_tanh_with_dynamic_shape(self):
17+
class TestModule(nn.Module):
18+
def forward(self, x):
19+
return nn.functional.tanh(x)
20+
21+
input_specs = [
22+
InputTensorSpec(
23+
shape=(-1, -1, -1),
24+
dtype=torch.float32,
25+
shape_ranges=[((1, 1, 1), (1, 2, 3), (3, 3, 3))],
26+
),
27+
]
28+
self.run_test_with_dynamic_shape(
29+
TestModule(), input_specs, expected_ops={torch.ops.aten.tanh.default}
30+
)
31+
32+
def test_tanh_with_dynamic_shape_four_dimensions(self):
33+
class TestModule(nn.Module):
34+
def forward(self, x):
35+
return nn.functional.tanh(x)
36+
37+
input_specs = [
38+
InputTensorSpec(
39+
shape=(-1, -1, -1, -1),
40+
dtype=torch.float32,
41+
shape_ranges=[((1, 1, 1, 5), (1, 2, 3, 5), (3, 3, 3, 5))],
42+
),
43+
]
44+
45+
self.run_test_with_dynamic_shape(
46+
TestModule(), input_specs, expected_ops={torch.ops.aten.tanh.default}
47+
)
48+
49+
50+
if __name__ == "__main__":
51+
run_tests()

0 commit comments

Comments
 (0)