From 7682478701f914d35913857d67a414246c7fd9df Mon Sep 17 00:00:00 2001 From: Haoyang Date: Mon, 24 Jul 2023 00:09:08 +0800 Subject: [PATCH 01/18] Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index eadd0a3c464f..b3bf6eae9c5a 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4291,7 +4291,7 @@ def _handel_nested_input(inputs): self.current_op.pop() - return [_wrap_const(outputs[ret_name]) for ret_name in ret_names] + return [_wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] != None] def _set_parameter_source_name(self, op_node, outputs): """A helper function to rewrite source_name of parameter.""" From 101cd3a738e080836a510bbfa8e315faaf5b92ab Mon Sep 17 00:00:00 2001 From: Haoyang Date: Mon, 24 Jul 2023 11:57:08 +0800 Subject: [PATCH 02/18] Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index b3bf6eae9c5a..19231d2a078a 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4291,7 +4291,9 @@ def _handel_nested_input(inputs): self.current_op.pop() - return [_wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] != None] + return [ + _wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] != None + ] def _set_parameter_source_name(self, op_node, outputs): """A helper function to rewrite source_name of parameter.""" From d6ef77f8eea0ce9630f67d0224d06aa35f31c26f Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 13:12:45 +0800 Subject: [PATCH 03/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- tests/python/frontend/pytorch/test_forward.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index cb49e837fe6e..075295868095 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3439,6 +3439,15 @@ def forward(self, *args): verify_model(Full2().float().eval(), input_data=[]) +@tvm.testing.uses_gpu +def test_forward_adaptive_max_pool1d(): + """test_forward_adaptive_max_pool1d""" + torch.set_grad_enabled(False) + input_data=[torch.randn([2,2,4], dtype=torch.float32)] + m = torch.nn.AdaptiveMaxPool1d(3, return_indices=True) + + verify_model(m.float().eval(), input_date=input_data) + @tvm.testing.uses_gpu def test_forward_full_like(): """test_forward_full_like""" From d6cf54c9eee4fdb24ff8482ccb463b0e7231afa8 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 13:17:41 +0800 Subject: [PATCH 04/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- tests/python/frontend/pytorch/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 075295868095..9baee5c574d7 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3448,6 +3448,7 @@ def test_forward_adaptive_max_pool1d(): verify_model(m.float().eval(), input_date=input_data) + @tvm.testing.uses_gpu def test_forward_full_like(): """test_forward_full_like""" From fa18f86cbad4f06b13bf732bef9131c549d8e15a Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 13:43:28 +0800 Subject: [PATCH 05/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- tests/python/frontend/pytorch/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 9baee5c574d7..3ca44a5241f1 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3443,7 +3443,7 @@ def forward(self, *args): def test_forward_adaptive_max_pool1d(): """test_forward_adaptive_max_pool1d""" torch.set_grad_enabled(False) - input_data=[torch.randn([2,2,4], dtype=torch.float32)] + input_data = [torch.randn([2, 2, 4], dtype=torch.float32)] m = torch.nn.AdaptiveMaxPool1d(3, return_indices=True) verify_model(m.float().eval(), input_date=input_data) From 66a99a99757e7ec82ba51aa1967ac3f163adda69 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 14:49:57 +0800 Subject: [PATCH 06/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 19231d2a078a..6836942651aa 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4292,7 +4292,7 @@ def _handel_nested_input(inputs): self.current_op.pop() return [ - _wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] != None + _wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] is not None ] def _set_parameter_source_name(self, op_node, outputs): From 92a9eb191a369cb278be919b6045f342d086618f Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 15:50:02 +0800 Subject: [PATCH 07/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 6836942651aa..73c30960b9f9 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4292,7 +4292,9 @@ def _handel_nested_input(inputs): self.current_op.pop() return [ - _wrap_const(outputs[ret_name]) for ret_name in ret_names if outputs[ret_name] is not None + _wrap_const(outputs[ret_name]) + for ret_name in ret_names + if outputs[ret_name] is not None ] def _set_parameter_source_name(self, op_node, outputs): From 3d33e2266c3fc8d259702a90c019d7d532a658fa Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Mon, 24 Jul 2023 19:15:18 +0800 Subject: [PATCH 08/18] add tests for Fix an adaptive_max_pool1d operator conversion bug --- tests/python/frontend/pytorch/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 3ca44a5241f1..7a5d0f4d9b09 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3446,7 +3446,7 @@ def test_forward_adaptive_max_pool1d(): input_data = [torch.randn([2, 2, 4], dtype=torch.float32)] m = torch.nn.AdaptiveMaxPool1d(3, return_indices=True) - verify_model(m.float().eval(), input_date=input_data) + verify_model(m.float().eval(), input_data=input_data) @tvm.testing.uses_gpu From c479f83ccd8360944abc5e874ff6818260ea6845 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Fri, 1 Sep 2023 20:44:08 +0800 Subject: [PATCH 09/18] Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 4 ++-- tests/python/frontend/pytorch/test_forward.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index c34676b9d6d7..54af18e8ebc8 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4290,11 +4290,11 @@ def _handel_nested_input(inputs): outputs[node_name] = relay_out self.current_op.pop() - + return [ _wrap_const(outputs[ret_name]) for ret_name in ret_names - if outputs[ret_name] is not None + if ret_name != 'aten::adaptive_max_pool1d_0_1' ] def _set_parameter_source_name(self, op_node, outputs): diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 7a5d0f4d9b09..8c1cdbb0cf0b 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3444,7 +3444,7 @@ def test_forward_adaptive_max_pool1d(): """test_forward_adaptive_max_pool1d""" torch.set_grad_enabled(False) input_data = [torch.randn([2, 2, 4], dtype=torch.float32)] - m = torch.nn.AdaptiveMaxPool1d(3, return_indices=True) + m = torch.nn.AdaptiveMaxPool1d(3) verify_model(m.float().eval(), input_data=input_data) From 37f5a83590e6ec9d5d6e477c56577329d09be0ca Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Fri, 1 Sep 2023 23:44:07 +0800 Subject: [PATCH 10/18] Fix an adaptive_max_pool1d operator conversion bug --- python/tvm/relay/frontend/pytorch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 54af18e8ebc8..7062921560f4 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4290,11 +4290,11 @@ def _handel_nested_input(inputs): outputs[node_name] = relay_out self.current_op.pop() - + return [ _wrap_const(outputs[ret_name]) for ret_name in ret_names - if ret_name != 'aten::adaptive_max_pool1d_0_1' + if ret_name != "aten::adaptive_max_pool1d_0_1" ] def _set_parameter_source_name(self, op_node, outputs): From 8ae112c37ead4db6287037c5e3fd0543558febf2 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Sun, 3 Sep 2023 15:14:39 +0800 Subject: [PATCH 11/18] Add a TODO --- python/tvm/relay/frontend/pytorch.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 7062921560f4..e10f1a71f26a 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4291,6 +4291,10 @@ def _handel_nested_input(inputs): self.current_op.pop() + # TODO(@haoyang9804): outputs[ret_name] could be None and cause some issue + # revealed by https://github.com/apache/tvm/issues/15004 + # Now only adaptive_max_pool1d is considered. Maybe other ops could also + # trigger this problem. return [ _wrap_const(outputs[ret_name]) for ret_name in ret_names From ab35b6584d13aef39ca2c3499573802caefda3b5 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Sun, 3 Sep 2023 16:55:24 +0800 Subject: [PATCH 12/18] Add a TODO --- python/tvm/relay/frontend/pytorch.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index e10f1a71f26a..dd16b2081479 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4291,8 +4291,10 @@ def _handel_nested_input(inputs): self.current_op.pop() + # TODO(@haoyang9804): outputs[ret_name] could be None and cause some issue # revealed by https://github.com/apache/tvm/issues/15004 + # Now only adaptive_max_pool1d is considered. Maybe other ops could also # trigger this problem. return [ From 12d1d785762f4e79b6b9f8bdf4b6759ce9886448 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Sun, 3 Sep 2023 19:34:37 +0800 Subject: [PATCH 13/18] Add a TODO --- python/tvm/relay/frontend/pytorch.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index dd16b2081479..683b94dd9290 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4291,11 +4291,9 @@ def _handel_nested_input(inputs): self.current_op.pop() - - # TODO(@haoyang9804): outputs[ret_name] could be None and cause some issue + # TODO(@haoyang9804): outputs[ret_name] could be None and cause some issue # revealed by https://github.com/apache/tvm/issues/15004 - - # Now only adaptive_max_pool1d is considered. Maybe other ops could also + # Now only adaptive_max_pool1d is considered. Maybe other ops could also # trigger this problem. return [ _wrap_const(outputs[ret_name]) From a538c066774b689d9aef9743b5e625e07afeb965 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Wed, 6 Sep 2023 21:04:36 +0800 Subject: [PATCH 14/18] Fix the Bug caused by torch's instance_norm when input data is [1, 1, 1, 2] --- python/tvm/relay/frontend/pytorch.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 683b94dd9290..1ed1f817b55f 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4424,7 +4424,10 @@ def _create_typed_const(data, dtype): dtype should be a TVM dtype""" if dtype == "float64": - typed_data = _expr.const(np.float64(data), dtype=dtype) + if len(data) == 1: + typed_data = _expr.const(np.array([np.float64(data)]), dtype=dtype) + else: + typed_data = _expr.const(np.float64(data), dtype=dtype) elif dtype == "float32": typed_data = _expr.const(np.float32(data), dtype=dtype) elif dtype == "float16": From 9f2bfbd0f5428ddbd99bc761cc529b5c507cd036 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Wed, 6 Sep 2023 22:42:41 +0800 Subject: [PATCH 15/18] Add a unit test --- tests/python/frontend/pytorch/test_forward.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 8c1cdbb0cf0b..9ee03512e7ae 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3449,6 +3449,20 @@ def test_forward_adaptive_max_pool1d(): verify_model(m.float().eval(), input_data=input_data) +@tvm.testing.uses_gpu +def test_forward_instance_norm(): + """test_forward_instance_norm""" + + class instance_norm(Module): + def forward(self, *args): + return torch.nn.functional.instance_norm(args[0], use_input_stats=True) + + m = instance_norm().float().eval() + input_data = torch.randn([1, 1, 1, 2], dtype=torch.float64) + + verify_model(m.float().eval(), input_data=input_data) + + @tvm.testing.uses_gpu def test_forward_full_like(): """test_forward_full_like""" From 483225eba03e0d1fbd72228397b99c4e1f6308d4 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Thu, 7 Sep 2023 15:31:49 +0800 Subject: [PATCH 16/18] Fix the Bug caused by torch's instance_norm when input data is [1, 1, 1, 2] --- python/tvm/relay/frontend/pytorch.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 1ed1f817b55f..9a149bfd7256 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4424,10 +4424,11 @@ def _create_typed_const(data, dtype): dtype should be a TVM dtype""" if dtype == "float64": - if len(data) == 1: - typed_data = _expr.const(np.array([np.float64(data)]), dtype=dtype) + typed_data = np.float64(data) + if np.isscalar(typed_data): + typed_data = _expr.const(np.array([typed_data]), dtype=dtype) else: - typed_data = _expr.const(np.float64(data), dtype=dtype) + typed_data = _expr.const(typed_data, dtype=dtype) elif dtype == "float32": typed_data = _expr.const(np.float32(data), dtype=dtype) elif dtype == "float16": From 0f33281e3242657fb3f55a04e47105872051039f Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Thu, 7 Sep 2023 17:27:07 +0800 Subject: [PATCH 17/18] Fix the Bug caused by torch's instance_norm when input data is [1, 1, 1, 2] --- python/tvm/relay/frontend/pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 9a149bfd7256..ccdd600f29e0 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4426,7 +4426,7 @@ def _create_typed_const(data, dtype): if dtype == "float64": typed_data = np.float64(data) if np.isscalar(typed_data): - typed_data = _expr.const(np.array([typed_data]), dtype=dtype) + typed_data = _expr.const(np.asarray(data, dtype="float64"), dtype=dtype) else: typed_data = _expr.const(typed_data, dtype=dtype) elif dtype == "float32": From 7dae329820202f22094e7513954238abeb792529 Mon Sep 17 00:00:00 2001 From: haoyang9804 Date: Thu, 7 Sep 2023 20:35:29 +0800 Subject: [PATCH 18/18] simplify the last fix --- python/tvm/relay/frontend/pytorch.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index ccdd600f29e0..9ddd04b5b4ee 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -4424,11 +4424,7 @@ def _create_typed_const(data, dtype): dtype should be a TVM dtype""" if dtype == "float64": - typed_data = np.float64(data) - if np.isscalar(typed_data): - typed_data = _expr.const(np.asarray(data, dtype="float64"), dtype=dtype) - else: - typed_data = _expr.const(typed_data, dtype=dtype) + typed_data = _expr.const(np.asarray(data, dtype="float64"), dtype=dtype) elif dtype == "float32": typed_data = _expr.const(np.float32(data), dtype=dtype) elif dtype == "float16":