From a6bfb5705dbcccb24c203aef6199ff2c8064aa6d Mon Sep 17 00:00:00 2001 From: panyj Date: Tue, 3 Mar 2020 22:24:30 -0500 Subject: [PATCH 1/5] fix unordered dictionary problem for python version 3.5 --- python/tvm/relay/frontend/pytorch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index b256faa5d6f9..ca58b295cda2 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -918,7 +918,7 @@ def _get_constant(node): def _get_operator_nodes(nodes): """ Returns torch IR nodes that need conversion to Relay """ - ops = {} + ops = [] # Traverse nodes and add to graph for node in nodes: if node.outputsSize() > 1: @@ -927,7 +927,7 @@ def _get_operator_nodes(nodes): node_name = _get_output_name(node) if node.kind() != "prim::GetAttr": - ops[node_name] = node + ops.append((node_name,node)) return ops @@ -1015,7 +1015,7 @@ def parse_params(graph, state_dict): def parse_operators(operators, outputs, output_index_map, ret_name): """ Convert each Torch IR operators to Relay equivalent """ - for node_name, op_node in operators.items(): + for node_name, op_node in operators: operator = op_node.kind() inputs = _get_op_inputs(op_node, outputs, output_index_map) From 66a65d4f1abc7b89ad47c0ebb08d12d9538411d6 Mon Sep 17 00:00:00 2001 From: panyj Date: Tue, 3 Mar 2020 23:19:49 -0500 Subject: [PATCH 2/5] modify style --- 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 ca58b295cda2..19bccca34bd1 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -927,7 +927,7 @@ def _get_operator_nodes(nodes): node_name = _get_output_name(node) if node.kind() != "prim::GetAttr": - ops.append((node_name,node)) + ops.append((node_name, node)) return ops From 28a8e48954b85398f5c5794e7ed6deeff693fa8d Mon Sep 17 00:00:00 2001 From: panyj Date: Wed, 4 Mar 2020 14:47:59 -0500 Subject: [PATCH 3/5] default value of stride in torch.nn.functional.avg_pool is None --- 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 19bccca34bd1..688f2e65253e 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -458,7 +458,10 @@ def _impl(inputs, input_types): data = inputs[0] pool_size = _infer_shape(inputs[1]) - strides = _infer_shape(inputs[2]) + if inputs[2]: + strides = _infer_shape(inputs[2]) + else: + strides = pool_size padding = _infer_shape(inputs[3]) ceil_mode = int(inputs[4]) From 58f6e292908994222d08eb87eef43de4af0ce066 Mon Sep 17 00:00:00 2001 From: panyj Date: Wed, 4 Mar 2020 15:03:08 -0500 Subject: [PATCH 4/5] delete prev modifications --- python/tvm/relay/frontend/pytorch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 688f2e65253e..ca1d31cb9fe8 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -921,7 +921,7 @@ def _get_constant(node): def _get_operator_nodes(nodes): """ Returns torch IR nodes that need conversion to Relay """ - ops = [] + ops = {} # Traverse nodes and add to graph for node in nodes: if node.outputsSize() > 1: @@ -930,7 +930,7 @@ def _get_operator_nodes(nodes): node_name = _get_output_name(node) if node.kind() != "prim::GetAttr": - ops.append((node_name, node)) + ops[node_name] = node return ops @@ -1018,7 +1018,7 @@ def parse_params(graph, state_dict): def parse_operators(operators, outputs, output_index_map, ret_name): """ Convert each Torch IR operators to Relay equivalent """ - for node_name, op_node in operators: + for node_name, op_node in operators.items(): operator = op_node.kind() inputs = _get_op_inputs(op_node, outputs, output_index_map) From 2bdd1da68cd94962ca0b42faff3aa5528dcef9d8 Mon Sep 17 00:00:00 2001 From: panyj Date: Wed, 4 Mar 2020 15:46:29 -0500 Subject: [PATCH 5/5] add testcase for nn.functional.avg_pool2d --- tests/python/frontend/pytorch/test_forward.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index c2ff94de546f..e60c1fd88183 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -375,8 +375,13 @@ class AvgPool2D1(Module): def forward(self, *args): return torch.nn.AvgPool2d(kernel_size=[10, 10])(args[0]) + class AvgPool2D2(Module): + def forward(self, *args): + return torch.nn.functional.avg_pool2d(args[0], kernel_size=[10, 10]) + input_data = torch.rand(input_shape).float() verify_model(AvgPool2D1().float().eval(), input_data=input_data) + verify_model(AvgPool2D2().float().eval(), input_data=input_data) def test_forward_hardtanh(): torch.set_grad_enabled(False)