From ca95f0bc0be4691e81360314cfc965252067289b Mon Sep 17 00:00:00 2001 From: SunAhong1993 <48579383+SunAhong1993@users.noreply.github.com> Date: Wed, 12 May 2021 21:25:50 +0800 Subject: [PATCH] for release-1.1 (#569) * Update pull_request_template.md * Update pull_request_template.md * Update pull_request_template.md * Update README.md * fix the pad in tf (#567) * fix the convert.py args * fix teh pad and add log_softmax * Update pull_request_template.md * Update pull_request_template.md * Update pull_request_template.md Co-authored-by: Jason --- .github/pull_request_template.md | 15 +++++-- README.md | 4 +- .../op_mapper/dygraph/pytorch2paddle/aten.py | 45 +++++++++++++++++++ .../dygraph/tf2paddle/tf_op_mapper.py | 26 ++++++++--- 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 97cb5ee..31c08d9 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,5 +1,14 @@ # Create A Good Pull Request -- [] Python code style verification -- [] All models(TensorFLow/Caffe/ONNX/PyTorch) testing passed -- [] Details about your pull request, releated issues +> 下面的文字请保留在PR说明的最后面,并在提完PR后,根据实际情况勾选确认以下情况 + +Please check the follow step before merging this pull request + +- [ ] Python code style verification +- [ ] Review all the code diff by yourself +- [ ] All models(TensorFLow/Caffe/ONNX/PyTorch) testing passed +- [ ] Details about your pull request, releated issues + +If this PR add new model support, please update `model_zoo.md` and add model to out test model zoos(@wjj19950828) +- [ ] New Model Supported +- [ ] No New Model Supported diff --git a/README.md b/README.md index 16ae74c..53c68b3 100644 --- a/README.md +++ b/README.md @@ -116,9 +116,9 @@ X2Paddle提供了工具解决如下问题,详见[tools/README.md](tools/README 1. 新增支持转换的模型:[SwinTransformer](https://github.com/microsoft/Swin-Transformer/)、[BASNet](https://github.com/xuebinqin/BASNet)、[DBFace](https://github.com/dlunion/DBFace)、[EasyOCR](https://github.com/JaidedAI/EasyOCR)、[CifarNet](https://github.com/tensorflow/models/blob/master/research/slim/nets/cifarnet.py)等。 2. 支持Windows上使用本工具。 3. 新增TensorFlow op映射(4个):SplitV、ReverseV2、BatchToSpaceND、SpaceToBatchND。 -4. 新增PyTorch op映射(11个):aten::index、aten::roll、aten::adaptive_avg_pool1d、aten::reflection_pad2d、aten::reflection_pad1d、aten::instance_norm、aten::gru、aten::norm、aten::clamp_min、aten:prelu、aten:split_with_sizes。 +4. 新增PyTorch op映射(11个):aten::index、aten::roll、aten::adaptive_avg_pool1d、aten::reflection_pad2d、aten::reflection_pad1d、aten::instance_norm、aten::gru、aten::norm、aten::clamp_min、aten::prelu、aten:split_with_sizes。 5. 新增ONNX op映射(1个):DepthToSpace。 -6. 新增Caffe op映射(1个):op:MemoryData。 +6. 新增Caffe op映射(1个):MemoryData。 ## 贡献代码 diff --git a/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py b/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py index 43c6b2b..669465c 100644 --- a/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py +++ b/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py @@ -2962,6 +2962,51 @@ def aten_log(mapper, graph, node): return current_inputs, current_outputs +def aten_log_softmax(mapper, graph, node): + """ 构造log_softmax的PaddleLayer。 + TorchScript示例: + %4 = aten::log_softmax(%input, %2, %3) + 参数含义: + %4 (Tensor): 输出的Tensor。 + %input (Tensor): 输入的Tensor。 + %2 (int): 指定对输入进行运算的轴。 + %3 (int): 输入Tensor的数据类型。 + """ + scope_name = mapper.normalize_scope_name(node) + output_name = mapper._get_outputs_name(node)[0] + layer_outputs = [output_name] + layer_inputs = {} + layer_attrs = {} + inputs_name, inputs_node = mapper._get_inputs_name(node) + # 获取当前节点输出的list + current_outputs = [output_name] + current_inputs = [] + # 处理输入0,即%input + mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, + scope_name) + layer_inputs["x"] = inputs_name[0] + # 处理输入1,即%2,代表dtype + if inputs_name[1] in mapper.attrs: + layer_attrs["axis"] = mapper.attrs[inputs_name[1]] + else: + mapper._check_input(graph, inputs_node[1], inputs_name[1], + current_outputs, scope_name) + layer_inputs["axis"] = inputs_name[1] + # 处理输入2,即%3,代表dtype + if mapper.attrs[inputs_name[2]] is not None: + layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[2]]] + # 获取当前节点输入的list + current_inputs = list(layer_inputs.values()) + + graph.add_layer( + "paddle.nn.functional.log_softmax", + inputs=layer_inputs, + outputs=layer_outputs, + scope_name=scope_name, + **layer_attrs) + return current_inputs, current_outputs + + def aten_lstm(mapper, graph, node): """ 构造长短期记忆网络(LSTM)的PaddleLayer。 TorchScript示例: diff --git a/x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py b/x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py index a4f446f..606fd26 100644 --- a/x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py +++ b/x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py @@ -649,18 +649,30 @@ class TFOpMapper(OpMapper): paddings = self.graph.get_input_node(node, 1) assert paddings.layer_type == "Const", "Padding should be Const" paddings = paddings.value.flatten().tolist() + constant_values = 0 if len(node.layer.input) > 2: constant_values = self.graph.get_input_node(node, 2) assert constant_values.layer_type == "Const", "Padding should be Const" constant_values = constant_values.value - - self.paddle_graph.add_layer( - kernel="paddle.nn.functional.pad", - inputs={"x": input.name}, - outputs=[node.name], - pad=paddings, - value=constant_values) + + if len(paddings) == 8 and sum(paddings[:2]) == 0 \ + and sum(paddings[-2:]) == 0: + paddings = paddings[2: -2] + self.paddle_graph.add_layer( + kernel="paddle.nn.functional.pad", + inputs={"x": input.name}, + outputs=[node.name], + pad=paddings, + value=constant_values, + data_format=string('NHWC')) + else: + self.paddle_graph.add_layer( + kernel="paddle.nn.functional.pad", + inputs={"x": input.name}, + outputs=[node.name], + pad=paddings, + value=constant_values) def MirrorPad(self, node): self.Pad(node) -- GitLab