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

Support aten::flip #8398

Merged
merged 2 commits into from
Jul 4, 2021
Merged
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
6 changes: 6 additions & 0 deletions python/tvm/relay/frontend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,11 @@ def nll_loss(self, inputs, input_types):
weights = _op.full(_expr.const(1), (num_class,), dtype=input_types[0])
return _op.nn.nll_loss(predictions, targets, weights, reduction, ignore_index)

def flip(self, inputs, input_types):
data = inputs[0]
axis = inputs[1]
return _op.transform.reverse(data, axis=axis[0])

# Operator mappings
def create_convert_map(self):
self.convert_map = {
Expand Down Expand Up @@ -2539,6 +2544,7 @@ def create_convert_map(self):
"aten::_unique2": self.unique,
"aten::nll_loss": self.nll_loss,
"aten::nll_loss2d": self.nll_loss,
"aten::flip": self.flip,
}

def update_convert_map(self, custom_map):
Expand Down
20 changes: 20 additions & 0 deletions tests/python/frontend/pytorch/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -3893,6 +3893,25 @@ def test_forward_nll_loss():
verify_model(torch.nn.NLLLoss(reduction="none").eval(), input_data=[predictions, targets])


@tvm.testing.uses_gpu
def test_forward_flip():
torch.set_grad_enabled(False)

class Flip(Module):
def __init__(self, axis=0):
super().__init__()
self.axis = axis

def forward(self, x):
return x.flip([self.axis])

input = torch.randn(2, 3, 4)
verify_model(Flip(axis=0), input_data=input)
verify_model(Flip(axis=1), input_data=input)
verify_model(Flip(axis=2), input_data=input)
verify_model(Flip(axis=-1), input_data=input)


if __name__ == "__main__":
# some structural tests
test_forward_traced_function()
Expand Down Expand Up @@ -4035,6 +4054,7 @@ def test_forward_nll_loss():
test_hard_swish()
test_hard_sigmoid()
test_forward_nll_loss()
test_forward_flip()

# Model tests
test_resnet18()
Expand Down