Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PaddleV3] 添加 pytorchaten::pad 的算子映射并修复相关模型 #1073

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test_benchmark/PyTorch/ACG_UnitTest/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/PyTorch/ACG_UnitTest/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import paddle.fluid as fluid
import paddle
import numpy as np
import sys
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion test_benchmark/PyTorch/Mobilestereonet/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
5 changes: 1 addition & 4 deletions test_benchmark/PyTorch/Mobilestereonet/pd_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/PyTorch/Saicinpainting_LaMa/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import paddle.fluid as fluid
import paddle
import numpy as np
import sys
Expand All @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions test_benchmark/PyTorch/black.list
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ACG_UnitTest
BertForMaskedLM_dccuchile
BertModel_SpanBert
CamembertForQuestionAnswering
Expand All @@ -9,13 +8,10 @@ EasyOCR_recognizer
FCN_ResNet50
GRU
MiniFasNet
Mobilestereonet
MockingBird
Roberta
Saicinpainting_LaMa
SwinTransformer
XLMRobertaForTokenClassification
opadd
dataset
tools
output
1 change: 0 additions & 1 deletion test_benchmark/PyTorch/opadd/deploy_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/PyTorch/opadd/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import print_function
import paddle.fluid as fluid
import paddle
import sys
import os
Expand All @@ -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:
Expand Down
91 changes: 91 additions & 0 deletions x2paddle/op_mapper/pytorch2paddle/aten.py
Original file line number Diff line number Diff line change
Expand Up @@ -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