Skip to content

Commit 691c29b

Browse files
committed
Activation Converter reorg and adding tanh in aten operations
1 parent 1696cd2 commit 691c29b

File tree

5 files changed

+112
-5
lines changed

5 files changed

+112
-5
lines changed

py/torch_tensorrt/fx/converters/acc_ops_converters.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,15 +1166,12 @@ def acc_ops_tanh(
11661166
kwargs: Dict[str, Argument],
11671167
name: str,
11681168
) -> Union[TRTTensor, Sequence[TRTTensor]]:
1169-
input_val = kwargs["input"]
1170-
operation_type = trt.ActivationType.TANH
1171-
return activation.convert_activation(
1169+
return activation.tanh(
11721170
network,
11731171
target,
11741172
SourceIR.ACC,
11751173
name,
1176-
operation_type,
1177-
input_val,
1174+
kwargs["input"],
11781175
)
11791176

11801177

py/torch_tensorrt/fx/converters/aten_ops_converters.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,24 @@ def aten_ops_reshape(
353353
return layer.get_output(0)
354354

355355

356+
@tensorrt_converter(torch.ops.aten.tanh.default)
357+
def aten_ops_tanh(
358+
network: TRTNetwork,
359+
target: Target,
360+
args: Tuple[Argument, ...],
361+
kwargs: Dict[str, Argument],
362+
name: str,
363+
) -> Union[TRTTensor, Sequence[TRTTensor]]:
364+
365+
return activation.tanh(
366+
network,
367+
target,
368+
SourceIR.ATEN,
369+
name,
370+
args[0],
371+
)
372+
373+
356374
@tensorrt_converter(torch.ops.aten.cat.default)
357375
def aten_ops_cat(
358376
network: TRTNetwork,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,30 @@ def relu_dyn_range_fn(dyn_range):
9090
input_val,
9191
dyn_range_fn=relu_dyn_range_fn,
9292
)
93+
94+
95+
def tanh(
96+
network: TRTNetwork,
97+
target: Target,
98+
source_ir: Optional[SourceIR],
99+
name: str,
100+
input_val: TRTTensor,
101+
):
102+
operation_type = trt.ActivationType.TANH
103+
104+
def tanh_dyn_range_fn(dyn_range):
105+
def tanh_fn(x):
106+
# TODO: Can this just call torch.nn.functional.tanh?
107+
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
108+
109+
return tanh_fn(dyn_range[0]), tanh_fn(dyn_range[1])
110+
111+
return convert_activation(
112+
network,
113+
target,
114+
source_ir,
115+
name,
116+
operation_type,
117+
input_val,
118+
dyn_range_fn=tanh_dyn_range_fn,
119+
)

py/torch_tensorrt/fx/converters/nn_ops_converters.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ def relu(network, submod, args, kwargs, layer_name):
2222
name=layer_name,
2323
input_val=kwargs["input"],
2424
)
25+
26+
27+
@tensorrt_converter(torch.nn.modules.activation.Tanh)
28+
def tanh(network, submod, args, kwargs, layer_name):
29+
# args/kwargs should have already been normalized to kwargs
30+
assert len(args) == 0
31+
32+
activation.sigmoid(
33+
network=network,
34+
target="torch.nn.modules.activation.Tanh",
35+
source_ir=SourceIR.NN,
36+
name=layer_name,
37+
input_val=kwargs["input"],
38+
)
Lines changed: 51 additions & 0 deletions
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)