Skip to content
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

added flip converter #822

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions torch2trt/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .expand import *
from .example_plugin import *
from .flatten import *
from .flip import *
from .floordiv import *
from .gelu import *
from .getitem import *
Expand Down
30 changes: 30 additions & 0 deletions torch2trt/converters/flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from torch2trt import torch2trt, tensorrt_converter, get_arg, trt, make_size_wrapper


@tensorrt_converter("torch.Tensor.flip")
@tensorrt_converter("torch.flip")
def convert_flip(ctx):

input = get_arg(ctx, 'input', 0, None)
dims = get_arg(ctx, 'dims', 1, None)
output = ctx.method_return

input_shape_trt = ctx.network.add_shape(input._trt).get_output(0)

offset = [0 for i in range(input.ndim)]
stride = [1 for i in range(input.ndim)]
shape = tuple(input.size())
for d in dims:
offset[d] = -1
stride[d] = -1

layer = ctx.network.add_slice(
input._trt,
offset,
shape,
stride
)
layer.set_input(2, input_shape_trt)
layer.mode = trt.SliceMode.WRAP

output._trt = layer.get_output(0)
62 changes: 62 additions & 0 deletions torch2trt/tests/test_flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import torch
import torch.nn as nn

from torch2trt import torch2trt


class FlipModule(nn.Module):

def __init__(self, dims):
super().__init__()
self.dims = dims

def forward(self, x):
return torch.flip(x, self.dims)


class FlipTensorModule(nn.Module):

def __init__(self, dims):
super().__init__()
self.dims = dims

def forward(self, x):
return x.flip(self.dims)



def test_torch_flip():

x = torch.randn(1, 2, 3).cuda()

model = FlipModule(dims=(1,)).cuda().eval()
model_trt = torch2trt(model, [x])

out = model(x)
out_trt = model_trt(x)

assert torch.allclose(out, out_trt, rtol=1e-4, atol=1e-4)

def test_torch_flip_multidim():

x = torch.randn(1, 2, 3).cuda()

model = FlipTensorModule(dims=(1, 2)).cuda().eval()
model_trt = torch2trt(model, [x])

out = model(x)
out_trt = model_trt(x)

assert torch.allclose(out, out_trt, rtol=1e-4, atol=1e-4)

def test_torch_flip_tensor():

x = torch.randn(1, 2, 3).cuda()

model = FlipTensorModule(dims=(1,)).cuda().eval()
model_trt = torch2trt(model, [x])

out = model(x)
out_trt = model_trt(x)

assert torch.allclose(out, out_trt, rtol=1e-4, atol=1e-4)