Skip to content

Commit 003af69

Browse files
author
John Welsh
committed
Merge branch 'hyperfraise-3d'
2 parents a5daf0f + 9a11257 commit 003af69

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Master]
44

5+
- Added converter for ``torch.nn.functional.adaptive_avg_pool3d``
6+
- Added converter for ``torch.nn.functional.adaptive_max_pool3d``
7+
- Added converter for ``torch.maxpool3d`` and ``torch.nn.functional.max_pool3d``
58
- Added Quantization Aware Training (QAT) workflow to contrib
69
- Added converter for ``torch.roll``
710
- Added converter for ``torch.nn.functional.layer_norm``
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from torch2trt.torch2trt import *
2+
from torch2trt.module_test import add_module_test
3+
4+
5+
@tensorrt_converter(
6+
"torch.nn.AdaptiveAvgPool3d.forward", enabled=trt_version() >= "7.0"
7+
)
8+
def convert_AdaptiveAvgPool3d(ctx):
9+
module = ctx.method_args[0]
10+
input = ctx.method_args[1]
11+
output = ctx.method_return
12+
13+
input_trt = add_missing_trt_tensors(ctx.network, [input])[0]
14+
15+
output_size = module.output_size
16+
if not isinstance(output_size, tuple):
17+
output_size = (output_size,) * 3
18+
19+
stride = (
20+
input_trt.shape[-3] // output_size[-3],
21+
input_trt.shape[-2] // output_size[-2],
22+
input_trt.shape[-1] // output_size[-1],
23+
)
24+
25+
kernel_size = stride
26+
layer = ctx.network.add_pooling_nd(
27+
input=input_trt,
28+
type=trt.PoolingType.AVERAGE,
29+
window_size=kernel_size,
30+
)
31+
layer.stride_nd = stride
32+
33+
output._trt = layer.get_output(0)
34+
35+
36+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
37+
def test_AdaptiveAvgPool3d_1x1x1():
38+
return torch.nn.AdaptiveAvgPool3d((1, 1, 1))
39+
40+
41+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
42+
def test_AdaptiveAvgPool3d_2x2x2():
43+
return torch.nn.AdaptiveAvgPool3d((2, 2, 2))
44+
45+
46+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
47+
def test_AdaptiveAvgPool3d_3x3x3():
48+
return torch.nn.AdaptiveAvgPool3d((3, 3, 3))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from torch2trt.torch2trt import *
2+
from torch2trt.module_test import add_module_test
3+
4+
5+
@tensorrt_converter("torch.nn.BatchNorm3d.forward", enabled=trt_version() < "7.0")
6+
def convert_BatchNorm3d(ctx):
7+
module = ctx.method_args[0]
8+
input = ctx.method_args[1]
9+
input_trt = add_missing_trt_tensors(ctx.network, [input])[0]
10+
output = ctx.method_return
11+
12+
scale = module.weight.detach().cpu().numpy() / np.sqrt(
13+
module.running_var.detach().cpu().numpy() + module.eps
14+
)
15+
bias = (
16+
module.bias.detach().cpu().numpy()
17+
- module.running_mean.detach().cpu().numpy() * scale
18+
)
19+
power = np.ones_like(scale)
20+
21+
layer = ctx.network.add_scale(input_trt, trt.ScaleMode.CHANNEL, bias, scale, power)
22+
23+
output._trt = layer.get_output(0)

torch2trt/converters/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from .LogSoftmax import *
1818
from .activation import *
1919
from .adaptive_avg_pool2d import *
20+
from .adaptive_avg_pool3d import *
2021
from .adaptive_max_pool2d import *
22+
from .adaptive_max_pool3d import *
2123
from .add import *
2224
from .avg_pool import *
2325
from .batch_norm import *
@@ -37,6 +39,7 @@
3739
from .layer_norm import *
3840
from .max import *
3941
from .max_pool2d import *
42+
from .max_pool3d import *
4043
from .mean import *
4144
from .min import *
4245
from .mod import *
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from torch2trt.torch2trt import *
2+
from .AdaptiveAvgPool3d import *
3+
4+
5+
@tensorrt_converter("torch.nn.functional.adaptive_avg_pool3d")
6+
def convert_adaptive_avg_pool3d(ctx):
7+
ctx.method_args = (
8+
torch.nn.AdaptiveAvgPool3d(ctx.method_args[1]),
9+
ctx.method_args[0],
10+
)
11+
convert_AdaptiveAvgPool3d(ctx)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from torch2trt.torch2trt import *
2+
from torch2trt.module_test import add_module_test
3+
4+
5+
@tensorrt_converter("torch.nn.functional.adaptive_max_pool3d")
6+
def convert_adaptive_max_pool3d(ctx):
7+
input = ctx.method_args[0]
8+
output = ctx.method_return
9+
10+
output_size = ctx.method_args[1]
11+
if isinstance(output_size, int):
12+
output_size = (output_size,) * 3
13+
14+
stride = (
15+
input._trt.shape[-3] // output_size[-3],
16+
input._trt.shape[-2] // output_size[-2],
17+
input._trt.shape[-1] // output_size[-1],
18+
)
19+
20+
kernel_size = stride
21+
layer = ctx.network.add_pooling_nd(
22+
input=input._trt, type=trt.PoolingType.MAX, window_size=kernel_size
23+
)
24+
layer.stride_nd = stride
25+
26+
output._trt = layer.get_output(0)
27+
28+
29+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
30+
def test_adaptive_max_pool3d_1x1x1():
31+
return torch.nn.AdaptiveMaxPool3d((1, 1, 1))
32+
33+
34+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
35+
def test_adaptive_max_pool3d_2x2x2():
36+
return torch.nn.AdaptiveMaxPool3d((2, 2, 2))
37+
38+
39+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 16, 224, 224)])
40+
def test_adaptive_max_pool3d_3x3x3():
41+
return torch.nn.AdaptiveMaxPool3d((3, 3, 3))

torch2trt/converters/max_pool3d.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from torch2trt.torch2trt import *
2+
from torch2trt.module_test import add_module_test
3+
4+
5+
@tensorrt_converter("torch.nn.functional.max_pool3d")
6+
@tensorrt_converter("torch.max_pool3d")
7+
def convert_max_pool3d(ctx):
8+
# parse args
9+
input = get_arg(ctx, "input", pos=0, default=None)
10+
kernel_size = get_arg(ctx, "kernel_size", pos=1, default=None)
11+
stride = get_arg(ctx, "stride", pos=2, default=None)
12+
padding = get_arg(ctx, "padding", pos=3, default=0)
13+
dilation = get_arg(ctx, "dilation", pos=4, default=1)
14+
ceil_mode = get_arg(ctx, "ceil_mode", pos=5, default=False)
15+
16+
# get input trt tensor (or create constant if it doesn't exist)
17+
input_trt = add_missing_trt_tensors(ctx.network, [input])[0]
18+
19+
output = ctx.method_return
20+
21+
# get kernel size
22+
if not isinstance(kernel_size, tuple):
23+
kernel_size = (kernel_size,) * 3
24+
25+
# get stride
26+
if not isinstance(stride, tuple):
27+
stride = (stride,) * 3
28+
29+
# get padding
30+
if not isinstance(padding, tuple):
31+
padding = (padding,) * 3
32+
33+
layer = ctx.network.add_pooling_nd(
34+
input=input_trt, type=trt.PoolingType.MAX, window_size=kernel_size
35+
)
36+
37+
layer.stride_nd = stride
38+
layer.padding_nd = padding
39+
40+
if ceil_mode:
41+
layer.padding_mode = trt.PaddingMode.EXPLICIT_ROUND_UP
42+
43+
output._trt = layer.get_output(0)
44+
45+
46+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 4, 6, 7)])
47+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 5, 7, 8)])
48+
def test_MaxPool3d_without_ceil_mode():
49+
return torch.nn.MaxPool3d(kernel_size=3, stride=2, padding=1, ceil_mode=False)
50+
51+
52+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 4, 6, 7)])
53+
@add_module_test(torch.float32, torch.device("cuda"), [(1, 3, 5, 7, 8)])
54+
def test_MaxPool3d_with_ceil_mode():
55+
return torch.nn.MaxPool3d(kernel_size=3, stride=2, padding=1, ceil_mode=True)

0 commit comments

Comments
 (0)