diff --git a/test_benchmark/PyTorch/ACG_UnitTest/convert.py b/test_benchmark/PyTorch/ACG_UnitTest/convert.py index 2bcc07e9a..1a7caec52 100644 --- a/test_benchmark/PyTorch/ACG_UnitTest/convert.py +++ b/test_benchmark/PyTorch/ACG_UnitTest/convert.py @@ -14,7 +14,8 @@ def main(): num_blocks=2, time_window=3) - ckpt = torch.load("../dataset/ACG_UnitTest/model_best.pth") + ckpt = torch.load("../dataset/ACG_UnitTest/model_best.pth", + map_location=torch.device('cpu')) state_dict = ckpt['model'] model.load_state_dict(state_dict) diff --git a/test_benchmark/PyTorch/ACG_UnitTest/pd_infer.py b/test_benchmark/PyTorch/ACG_UnitTest/pd_infer.py index 6e26f94b6..1fcfbf1db 100644 --- a/test_benchmark/PyTorch/ACG_UnitTest/pd_infer.py +++ b/test_benchmark/PyTorch/ACG_UnitTest/pd_infer.py @@ -1,4 +1,3 @@ -import paddle.fluid as fluid import paddle import numpy as np import sys @@ -11,11 +10,8 @@ exe = paddle.static.Executor(paddle.CPUPlace()) # test dygraph - [prog, inputs, outputs - ] = fluid.io.load_inference_model(dirname="pd_model/inference_model/", - executor=exe, - model_filename="model.pdmodel", - params_filename="model.pdiparams") + [prog, inputs, outputs] = paddle.static.load_inference_model( + path_prefix="pd_model/inference_model/model", executor=exe) data = np.load('../dataset/ACG_UnitTest/input.npy') result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs) diff --git a/test_benchmark/PyTorch/Mobilestereonet/convert.py b/test_benchmark/PyTorch/Mobilestereonet/convert.py index 43a3556ef..2d6b78ed5 100644 --- a/test_benchmark/PyTorch/Mobilestereonet/convert.py +++ b/test_benchmark/PyTorch/Mobilestereonet/convert.py @@ -7,7 +7,8 @@ model = __models__['MSNet2D'](192) # state_dict = torch.load('./MSNet2D_SF_DS_KITTI2015.ckpt', map_location=torch.device('cpu')) state_dict = torch.load( - '../dataset/Mobilestereonet/MSNet2D_SF_DS_KITTI2015.ckpt') + '../dataset/Mobilestereonet/MSNet2D_SF_DS_KITTI2015.ckpt', + map_location=torch.device('cpu')) param_dict = state_dict['model'] new_param_dict = {} for k, v in param_dict.items(): diff --git a/test_benchmark/PyTorch/Mobilestereonet/pd_infer.py b/test_benchmark/PyTorch/Mobilestereonet/pd_infer.py index 8ba7699aa..bf511d334 100644 --- a/test_benchmark/PyTorch/Mobilestereonet/pd_infer.py +++ b/test_benchmark/PyTorch/Mobilestereonet/pd_infer.py @@ -11,10 +11,7 @@ # test dygraph [prog, inputs, outputs] = paddle.static.load_inference_model( - path_prefix="pd_model/inference_model", - executor=exe, - model_filename="model.pdmodel", - params_filename="model.pdiparams") + path_prefix="pd_model/inference_model/model", executor=exe) dummy_input_left = np.load("../dataset/Mobilestereonet/input_left.npy") dummy_input_right = np.load("../dataset/Mobilestereonet/input_right.npy") result = exe.run(prog, diff --git a/test_benchmark/PyTorch/Saicinpainting_LaMa/pd_infer.py b/test_benchmark/PyTorch/Saicinpainting_LaMa/pd_infer.py index 65b216c4a..0025d86e3 100644 --- a/test_benchmark/PyTorch/Saicinpainting_LaMa/pd_infer.py +++ b/test_benchmark/PyTorch/Saicinpainting_LaMa/pd_infer.py @@ -1,4 +1,3 @@ -import paddle.fluid as fluid import paddle import numpy as np import sys @@ -10,11 +9,8 @@ exe = paddle.static.Executor(paddle.CPUPlace()) # test dygraph - [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) data = np.load('../dataset/Saicinpainting_LaMa/input.npy') result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs) diff --git a/test_benchmark/PyTorch/black.list b/test_benchmark/PyTorch/black.list index 28f2493d5..53f23063b 100644 --- a/test_benchmark/PyTorch/black.list +++ b/test_benchmark/PyTorch/black.list @@ -1,4 +1,3 @@ -ACG_UnitTest BertForMaskedLM_dccuchile BertModel_SpanBert CamembertForQuestionAnswering @@ -9,13 +8,10 @@ EasyOCR_recognizer FCN_ResNet50 GRU MiniFasNet -Mobilestereonet MockingBird Roberta -Saicinpainting_LaMa SwinTransformer XLMRobertaForTokenClassification -opadd dataset tools output diff --git a/test_benchmark/PyTorch/opadd/deploy_infer.py b/test_benchmark/PyTorch/opadd/deploy_infer.py index 803d98557..eb8332301 100644 --- a/test_benchmark/PyTorch/opadd/deploy_infer.py +++ b/test_benchmark/PyTorch/opadd/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/opadd/pd_infer.py b/test_benchmark/PyTorch/opadd/pd_infer.py index 75a1c50fd..a7ec7c003 100644 --- a/test_benchmark/PyTorch/opadd/pd_infer.py +++ b/test_benchmark/PyTorch/opadd/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..075325292 100755 --- a/x2paddle/op_mapper/pytorch2paddle/aten.py +++ b/x2paddle/op_mapper/pytorch2paddle/aten.py @@ -6620,3 +6620,94 @@ def aten_topk(mapper, graph, node): **layer_attrs) return current_inputs, current_outputs + + +def aten_pad(mapper, graph, node): + """ + TorchScript Code: + %input.23 : Tensor = aten::pad(%input.21, %116, %114, %113) + Parameter meaning: + %input.21 (Tensor): Input Tensor + %116 (list): pad + %114 (str): pad mode + %113 (float): value + """ + scope_name = mapper.normalize_scope_name(node) + op_name = name_generator("pad", mapper.nn_name2id) + output_name = mapper._get_outputs_name(node)[0] + layer_inputs = {} + layer_attrs = {} + inputs_name, inputs_node = mapper._get_inputs_name(node) + # Output list + current_outputs = [output_name] + # process Input Tensor + mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, + scope_name) + + # process pad + padding_attr = None + if inputs_name[1] in mapper.attrs: + padding_attr = mapper.attrs[inputs_name[1]] + else: + mapper._check_input(graph, inputs_node[1], inputs_name[1], + current_outputs, scope_name) + layer_inputs["pad"] = inputs_name[1] + + # process `mode` + _pad_mode = mapper.attrs[inputs_name[2]] + layer_attrs["mode"] = _pad_mode + + # process value, try to conver to `float` + # with `None` which raise exception, make `value` be `0` as default. + _pad_value = mapper.attrs[inputs_name[3]] + _pad_value = _pad_value or 0 + try: + _pad_value = float(_pad_value) + except ValueError: + _pad_value = 0 + layer_attrs["value"] = _pad_value + + # process `data_format` + # TODO(megemini): the lastest version of `Paddle v3`, + # just make `data_format = string("None")` + # because `paddle.nn.functional.pad` can infer from input `x` + data_format = string("None") + if inputs_name[0] in mapper.attrs: + x_dim = len(mapper.attrs[inputs_name[0]]) + if x_dim == 3: + data_format = string("NCL") + elif x_dim == 4: + data_format = string("NCHW") + elif x_dim == 5: + data_format = string("NCDHW") + else: + if len(padding_attr) == 2: + data_format = string("NCL") + elif len(padding_attr) == 4: + data_format = string("NCHW") + elif len(padding_attr) == 6: + data_format = string("NCDHW") + layer_attrs["data_format"] = data_format + + # process `pad` + if padding_attr is not None: + layer_attrs["pad"] = padding_attr + if 'constant' in _pad_mode: + if len(padding_attr) == 2: + layer_attrs["pad"] = [0, 0, 0, 0, 0, 0] + padding_attr + elif len(padding_attr) == 4: + layer_attrs["pad"] = [0, 0, 0, 0] + padding_attr + elif len(padding_attr) == 6: + layer_attrs["pad"] = [0, 0] + padding_attr + + # input and kernel + layer_inputs["x"] = inputs_name[0] + kernel_name = "paddle.nn.functional.pad" + + graph.add_layer(kernel_name, + inputs=layer_inputs, + outputs=[output_name], + scope_name=scope_name, + **layer_attrs) + current_inputs = list(layer_inputs.values()) + return current_inputs, current_outputs