From af2eef9912605f5bac6af8e93990f5b1014e4d33 Mon Sep 17 00:00:00 2001 From: megemini Date: Mon, 21 Oct 2024 21:19:59 +0800 Subject: [PATCH 1/4] [Fix] pytorch aten_linear with bias is --- test_benchmark/PyTorch/MiniFasNet/convert.py | 2 +- .../PyTorch/MiniFasNet/deploy_infer.py | 1 - test_benchmark/PyTorch/MiniFasNet/pd_infer.py | 8 +--- .../PyTorch/SwinTransformer/deploy_infer.py | 1 - .../PyTorch/SwinTransformer/pd_infer.py | 8 +--- x2paddle/op_mapper/pytorch2paddle/aten.py | 18 ++++++- .../pytorch2paddle/pytorch_op_mapper.py | 48 +++++++++++++------ 7 files changed, 55 insertions(+), 31 deletions(-) diff --git a/test_benchmark/PyTorch/MiniFasNet/convert.py b/test_benchmark/PyTorch/MiniFasNet/convert.py index f5a6505b3..f21d495d0 100644 --- a/test_benchmark/PyTorch/MiniFasNet/convert.py +++ b/test_benchmark/PyTorch/MiniFasNet/convert.py @@ -11,7 +11,7 @@ import torch import numpy as np from torchvision.models import AlexNet -from torchvision.models.utils import load_state_dict_from_url +# from torchvision.models.utils import load_state_dict_from_url from x2paddle.convert import pytorch2paddle # 构建输入,pytorch-to-paddle input_data = np.random.rand(1, 3, 80, 80).astype("float32") diff --git a/test_benchmark/PyTorch/MiniFasNet/deploy_infer.py b/test_benchmark/PyTorch/MiniFasNet/deploy_infer.py index bc0296bd6..c0124e92d 100644 --- a/test_benchmark/PyTorch/MiniFasNet/deploy_infer.py +++ b/test_benchmark/PyTorch/MiniFasNet/deploy_infer.py @@ -4,7 +4,6 @@ import numpy as np import paddle -import paddle.fluid as fluid from paddle.inference import Config from paddle.inference import create_predictor diff --git a/test_benchmark/PyTorch/MiniFasNet/pd_infer.py b/test_benchmark/PyTorch/MiniFasNet/pd_infer.py index b4d3ae155..007196872 100644 --- a/test_benchmark/PyTorch/MiniFasNet/pd_infer.py +++ b/test_benchmark/PyTorch/MiniFasNet/pd_infer.py @@ -1,5 +1,4 @@ from __future__ import print_function -import paddle.fluid as fluid import paddle import sys import os @@ -13,11 +12,8 @@ # trace paddle.enable_static() exe = paddle.static.Executor(paddle.CPUPlace()) - [prog, inputs, outputs] = fluid.io.load_inference_model( - dirname="pd_model_trace/inference_model/", - executor=exe, - model_filename="model.pdmodel", - params_filename="model.pdiparams") + [prog, inputs, outputs] = paddle.static.load_inference_model( + path_prefix="pd_model_trace/inference_model/model", executor=exe) result = exe.run(prog, feed={inputs[0]: img}, fetch_list=outputs) df = pytorch_output - result if np.max(np.fabs(df)) > 1e-03: diff --git a/test_benchmark/PyTorch/SwinTransformer/deploy_infer.py b/test_benchmark/PyTorch/SwinTransformer/deploy_infer.py index 6d35d2114..fccd25880 100644 --- a/test_benchmark/PyTorch/SwinTransformer/deploy_infer.py +++ b/test_benchmark/PyTorch/SwinTransformer/deploy_infer.py @@ -4,7 +4,6 @@ import numpy as np import paddle -import paddle.fluid as fluid from paddle.inference import Config from paddle.inference import create_predictor diff --git a/test_benchmark/PyTorch/SwinTransformer/pd_infer.py b/test_benchmark/PyTorch/SwinTransformer/pd_infer.py index 013c64d42..b60954182 100644 --- a/test_benchmark/PyTorch/SwinTransformer/pd_infer.py +++ b/test_benchmark/PyTorch/SwinTransformer/pd_infer.py @@ -1,5 +1,4 @@ from __future__ import print_function -import paddle.fluid as fluid import paddle import sys import os @@ -14,11 +13,8 @@ # trace paddle.enable_static() exe = paddle.static.Executor(paddle.CPUPlace()) - [prog, inputs, outputs] = fluid.io.load_inference_model( - dirname="pd_model_trace/inference_model/", - executor=exe, - model_filename="model.pdmodel", - params_filename="model.pdiparams") + [prog, inputs, outputs] = paddle.static.load_inference_model( + path_prefix="pd_model_trace/inference_model/model", executor=exe) result = exe.run(prog, feed={inputs[0]: input_data}, fetch_list=outputs) df = pytorch_output - result if numpy.max(numpy.fabs(df)) > 1e-04: diff --git a/x2paddle/op_mapper/pytorch2paddle/aten.py b/x2paddle/op_mapper/pytorch2paddle/aten.py index 9389ff0e0..f747ba557 100755 --- a/x2paddle/op_mapper/pytorch2paddle/aten.py +++ b/x2paddle/op_mapper/pytorch2paddle/aten.py @@ -3435,8 +3435,22 @@ def aten_linear(mapper, graph, node): scope_name=scope_name, **layer_attrs) if len(inputs_name) == 3: - mapper._check_input(graph, inputs_node[2], inputs_name[2], - current_outputs, scope_name) + # make `bias` dtype like `weight`, and shape is `1` for broadcast + if inputs_name[1] in mapper.pytorch_params: + param = mapper.pytorch_params[inputs_name[1]] + dtype = string(str(param.dtype)) + shape = (1, ) + mapper._check_input(graph, + inputs_node[2], + inputs_name[2], + current_outputs, + scope_name, + dtype=dtype, + shape=shape) + else: + mapper._check_input(graph, inputs_node[2], inputs_name[2], + current_outputs, scope_name) + graph.add_layer("paddle.add", inputs={ "x": output_name, diff --git a/x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py b/x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py index 0df10034f..19e06e411 100644 --- a/x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py +++ b/x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py @@ -173,7 +173,14 @@ def _get_outputs_name(self, node, attr_name=None): outputs_name.append(output_name) return outputs_name - def _check_input(self, graph, node, output_name, node_outputs, scope_name): + def _check_input(self, + graph, + node, + output_name, + node_outputs, + scope_name, + dtype=None, + shape=None): if node.kind() == "prim::GetAttr": param = self.pytorch_params[output_name] if isinstance(param, np.ndarray): @@ -231,19 +238,32 @@ def _check_input(self, graph, node, output_name, node_outputs, scope_name): value=string(param) if isinstance( param, str) else param) node_outputs.append(output_name) - elif node.kind( - ) == "prim::Constant" and output_name in self.pytorch_params: - param = self.pytorch_params[output_name] - self.paddle_params[output_name] = param - layer_id = graph.add_layer( - "self.create_parameter", - inputs={}, - outputs=[output_name], - scope_name=scope_name, - dtype=string(str(param.dtype)), - shape=param.shape, - default_initializer="paddle.nn.initializer.Constant(value=0.0)") - self.output2id[output_name] = layer_id + elif node.kind() == "prim::Constant": + if output_name in self.pytorch_params: + param = self.pytorch_params[output_name] + self.paddle_params[output_name] = param + layer_id = graph.add_layer( + "self.create_parameter", + inputs={}, + outputs=[output_name], + scope_name=scope_name, + dtype=string(str(param.dtype)), + shape=param.shape, + default_initializer= + "paddle.nn.initializer.Constant(value=0.0)") + self.output2id[output_name] = layer_id + else: + if dtype is not None and shape is not None: + layer_id = graph.add_layer( + "self.create_parameter", + inputs={}, + outputs=[output_name], + scope_name=scope_name, + dtype=dtype, + shape=shape, + default_initializer= + "paddle.nn.initializer.Constant(value=0.0)") + self.output2id[output_name] = layer_id def _get_inputs_name(self, node): inputs_name = [] From f9d9ac8f8c774d9ba3337657d72944a40f55b242 Mon Sep 17 00:00:00 2001 From: megemini Date: Mon, 21 Oct 2024 21:50:46 +0800 Subject: [PATCH 2/4] [Update] check input --- x2paddle/op_mapper/pytorch2paddle/aten.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x2paddle/op_mapper/pytorch2paddle/aten.py b/x2paddle/op_mapper/pytorch2paddle/aten.py index f747ba557..7e2c184cf 100755 --- a/x2paddle/op_mapper/pytorch2paddle/aten.py +++ b/x2paddle/op_mapper/pytorch2paddle/aten.py @@ -3436,7 +3436,8 @@ def aten_linear(mapper, graph, node): **layer_attrs) if len(inputs_name) == 3: # make `bias` dtype like `weight`, and shape is `1` for broadcast - if inputs_name[1] in mapper.pytorch_params: + if (inputs_name[2] not in mapper.pytorch_params + and inputs_name[1] in mapper.pytorch_params): param = mapper.pytorch_params[inputs_name[1]] dtype = string(str(param.dtype)) shape = (1, ) From 2f78ca6fa075262d165f26af72dff9a0fcfa1c06 Mon Sep 17 00:00:00 2001 From: megemini Date: Thu, 31 Oct 2024 19:56:53 +0800 Subject: [PATCH 3/4] [Update] black.list --- test_benchmark/PyTorch/black.list | 2 -- 1 file changed, 2 deletions(-) diff --git a/test_benchmark/PyTorch/black.list b/test_benchmark/PyTorch/black.list index 5e07b9d8a..ae55057e2 100644 --- a/test_benchmark/PyTorch/black.list +++ b/test_benchmark/PyTorch/black.list @@ -1,8 +1,6 @@ EasyOCR_detector EasyOCR_recognizer -MiniFasNet MockingBird -SwinTransformer dataset tools output From 78362f30f53ee20f9bef30afdde4c457022cc9a2 Mon Sep 17 00:00:00 2001 From: megemini Date: Thu, 31 Oct 2024 20:23:45 +0800 Subject: [PATCH 4/4] [Fix] map location to cpu --- test_benchmark/PyTorch/SwinTransformer/convert_trace.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test_benchmark/PyTorch/SwinTransformer/convert_trace.py b/test_benchmark/PyTorch/SwinTransformer/convert_trace.py index 231a047fd..c676414bf 100644 --- a/test_benchmark/PyTorch/SwinTransformer/convert_trace.py +++ b/test_benchmark/PyTorch/SwinTransformer/convert_trace.py @@ -16,7 +16,8 @@ model_name = "swin_tiny_patch4_window7_224" torch_module = SwinTransformer(**swin_model_cfg_map[model_name]) torch_state_dict = torch.load( - "../dataset/SwinTransformer/{}.pth".format(model_name))["model"] + "../dataset/SwinTransformer/{}.pth".format(model_name), + map_location=torch.device('cpu'))["model"] torch_module.load_state_dict(torch_state_dict) model_name = "pd_model_trace" # 设置为eval模式