From 5032c9a9e9d750c26ce283add118ab624d007d94 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 8 Jan 2021 10:15:57 +0000 Subject: [PATCH 1/6] Added Ops --- python/tvm/relay/frontend/pytorch.py | 9 +++++++ tests/python/frontend/pytorch/test_forward.py | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 8e69739544e5..fe11df83cbff 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -1295,6 +1295,13 @@ def clone(self, inputs, input_types): data = inputs[0] return _op.tensor.copy(data) + def copy_(self, inputs, input_types): + assert self.infer_shape(inputs[0]) == self.infer_shape( + inputs[1] + ), "Shapes of source and destination tensors must be the same" + + return _op.tensor.copy(_op.cast(inputs[1], input_types[0])) + def log_softmax(self, inputs, input_types): data = inputs[0] axis = int(inputs[1]) @@ -2117,6 +2124,7 @@ def create_convert_map(self): "aten::to": self.to, "aten::squeeze": self.squeeze, "aten::unsqueeze": self.unsqueeze, + "aten::unsqueeze_": self.unsqueeze, "aten::cat": self.concatenate, "aten::slice": self.slice, "aten::split": self.split, @@ -2162,6 +2170,7 @@ def create_convert_map(self): "aten::view": self.view, "aten::reshape": self.reshape, "aten::clone": self.clone, + "aten::copy_": self.copy_, "aten::log_softmax": self.log_softmax, "aten::sigmoid": self.sigmoid, "aten::softplus": self.softplus, diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index f76c697a2c81..bed79bab9f6a 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -447,8 +447,13 @@ class Unsqueeze1(Module): def forward(self, *args): return args[0].unsqueeze(2) + class Unsqueeze2(Module): + def forward(self, *args): + return args[0].unsqueeze_(2) + input_data = torch.rand(input_shape).float() verify_model(Unsqueeze1().float().eval(), input_data=input_data) + verify_model(Unsqueeze2().float().eval(), input_data=input_data) @tvm.testing.uses_gpu @@ -1178,6 +1183,19 @@ def forward(self, *args): verify_model(Clone1().float().eval(), input_data=input_data) +@tvm.testing.uses_gpu +def test_forward_copy_(): + torch.set_grad_enabled(False) + input_shape = [10] + + class Copy_(Module): + def forward(self, *args): + return torch.zeros_like(args[0]).copy_(args[0]) + + input_data = torch.rand(input_shape).float() + verify_model(Copy_().float().eval(), input_data=input_data) + + @tvm.testing.uses_gpu def test_forward_gather(): torch.set_grad_enabled(False) @@ -3445,6 +3463,12 @@ def test_hard_swish(): if __name__ == "__main__": + test_forward_copy_() + test_forward_unsqueeze() + + import sys + + sys.exit() # some structural tests test_forward_traced_function() test_forward_dtypes() From 5517c275cebc98e1e3a274b11802a730314c32f4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 8 Jan 2021 10:18:23 +0000 Subject: [PATCH 2/6] Regular --- tests/python/frontend/pytorch/test_forward.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index bed79bab9f6a..d9dcfcaaea36 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3463,12 +3463,6 @@ def test_hard_swish(): if __name__ == "__main__": - test_forward_copy_() - test_forward_unsqueeze() - - import sys - - sys.exit() # some structural tests test_forward_traced_function() test_forward_dtypes() @@ -3532,6 +3526,7 @@ def test_hard_swish(): test_forward_true_divide() test_forward_is_floating_point() test_forward_clone() + test_forward_copy_() test_forward_softplus() test_forward_softsign() test_forward_logsoftmax() From b4859078cbded73c0173654c9eb60e2e215fd46d Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 13 Jan 2021 06:46:29 +0000 Subject: [PATCH 3/6] Remove copy --- tests/python/frontend/pytorch/test_forward.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index d9dcfcaaea36..22b8d15634c9 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -449,7 +449,8 @@ def forward(self, *args): class Unsqueeze2(Module): def forward(self, *args): - return args[0].unsqueeze_(2) + y = args[0].unsqueeze_(2) + return args[0] input_data = torch.rand(input_shape).float() verify_model(Unsqueeze1().float().eval(), input_data=input_data) @@ -1183,19 +1184,6 @@ def forward(self, *args): verify_model(Clone1().float().eval(), input_data=input_data) -@tvm.testing.uses_gpu -def test_forward_copy_(): - torch.set_grad_enabled(False) - input_shape = [10] - - class Copy_(Module): - def forward(self, *args): - return torch.zeros_like(args[0]).copy_(args[0]) - - input_data = torch.rand(input_shape).float() - verify_model(Copy_().float().eval(), input_data=input_data) - - @tvm.testing.uses_gpu def test_forward_gather(): torch.set_grad_enabled(False) From 7ee4ee122677f4aff4c3077de33971cc3d5d35a1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 13 Jan 2021 07:12:24 +0000 Subject: [PATCH 4/6] Remove copy --- python/tvm/relay/frontend/pytorch.py | 8 -------- tests/python/frontend/pytorch/test_forward.py | 10 +++++++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index fe11df83cbff..b8b7be4c598c 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -1295,13 +1295,6 @@ def clone(self, inputs, input_types): data = inputs[0] return _op.tensor.copy(data) - def copy_(self, inputs, input_types): - assert self.infer_shape(inputs[0]) == self.infer_shape( - inputs[1] - ), "Shapes of source and destination tensors must be the same" - - return _op.tensor.copy(_op.cast(inputs[1], input_types[0])) - def log_softmax(self, inputs, input_types): data = inputs[0] axis = int(inputs[1]) @@ -2170,7 +2163,6 @@ def create_convert_map(self): "aten::view": self.view, "aten::reshape": self.reshape, "aten::clone": self.clone, - "aten::copy_": self.copy_, "aten::log_softmax": self.log_softmax, "aten::sigmoid": self.sigmoid, "aten::softplus": self.softplus, diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 22b8d15634c9..ac361bc6d174 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -449,8 +449,10 @@ def forward(self, *args): class Unsqueeze2(Module): def forward(self, *args): - y = args[0].unsqueeze_(2) - return args[0] + _ = args[0].unsqueeze_(2) + # Check whether operations after inplace unsqueeze works as expected + y = args[0].squeeze(2) + return torch.add(y, y) input_data = torch.rand(input_shape).float() verify_model(Unsqueeze1().float().eval(), input_data=input_data) @@ -3451,6 +3453,9 @@ def test_hard_swish(): if __name__ == "__main__": + test_forward_unsqueeze() + import sys; + sys.exit() # some structural tests test_forward_traced_function() test_forward_dtypes() @@ -3514,7 +3519,6 @@ def test_hard_swish(): test_forward_true_divide() test_forward_is_floating_point() test_forward_clone() - test_forward_copy_() test_forward_softplus() test_forward_softsign() test_forward_logsoftmax() From 55ac2e33c600b9409df953bbf4344b61811ea466 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 13 Jan 2021 07:14:50 +0000 Subject: [PATCH 5/6] Tests --- tests/python/frontend/pytorch/test_forward.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index ac361bc6d174..dbb97d296809 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3453,9 +3453,6 @@ def test_hard_swish(): if __name__ == "__main__": - test_forward_unsqueeze() - import sys; - sys.exit() # some structural tests test_forward_traced_function() test_forward_dtypes() From 87174bd2b89ad0b56857c6b64ae51efef810e7ec Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 13 Jan 2021 07:22:28 +0000 Subject: [PATCH 6/6] Black --- tests/python/frontend/pytorch/test_forward.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index dbb97d296809..7cdd450448ca 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -450,9 +450,9 @@ def forward(self, *args): class Unsqueeze2(Module): def forward(self, *args): _ = args[0].unsqueeze_(2) - # Check whether operations after inplace unsqueeze works as expected + # Check whether operations after inplace unsqueeze works as expected y = args[0].squeeze(2) - return torch.add(y, y) + return torch.add(y, y) input_data = torch.rand(input_shape).float() verify_model(Unsqueeze1().float().eval(), input_data=input_data)