未验证 提交 e3ddca08 编写于 作者: J Jason 提交者: GitHub

Merge pull request #443 from SunAhong1993/paddle-2.0-new

fix the conv2dtranspose
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
import copy import copy
import numpy as np
from x2paddle.core.util import * from x2paddle.core.util import *
from x2paddle.core.program import PaddleGraph from x2paddle.core.program import PaddleGraph
...@@ -926,7 +927,7 @@ def aten_conv2d(mapper, graph, node): ...@@ -926,7 +927,7 @@ def aten_conv2d(mapper, graph, node):
%27 (Tensor): bias。 %27 (Tensor): bias。
%28 (int): 步长大小。 %28 (int): 步长大小。
%29 (int): 填充大小。 %29 (int): 填充大小。
%30 (int): 膨胀系数大小。 %30 (int): 空洞大小。
%26 (int): 卷积的组数。 %26 (int): 卷积的组数。
""" """
scope_name = mapper.normalize_scope_name(node) scope_name = mapper.normalize_scope_name(node)
...@@ -988,7 +989,7 @@ def aten__convolution(mapper, graph, node): ...@@ -988,7 +989,7 @@ def aten__convolution(mapper, graph, node):
%10 (Tensor): bias。 %10 (Tensor): bias。
%19 (list): 步长大小。 %19 (list): 步长大小。
%20 (list): 填充大小。 %20 (list): 填充大小。
%21 (list): 膨胀系数大小。 %21 (list): 空洞大小。
%13 (bool): 是否进行转置卷积。 %13 (bool): 是否进行转置卷积。
%22 (list): 输出形状上一侧额外添加的大小。 %22 (list): 输出形状上一侧额外添加的大小。
%12 (int): 卷积的组数。 %12 (int): 卷积的组数。
...@@ -1009,7 +1010,10 @@ def aten__convolution(mapper, graph, node): ...@@ -1009,7 +1010,10 @@ def aten__convolution(mapper, graph, node):
current_inputs = list(layer_inputs.values()) current_inputs = list(layer_inputs.values())
# 处理输入1,即%18 # 处理输入1,即%18
weights = mapper.pytorch_params[inputs_name[1]] weights = mapper.pytorch_params[inputs_name[1]]
mapper.paddle_params[op_name + ".weight"] = weights mapper.paddle_params[op_name + ".weight"] = weights #np.swapaxes(weights, 0, 1)
if mapper.attrs[inputs_name[6]]:
layer_attrs["out_channels"] = weights.shape[1]
else:
layer_attrs["out_channels"] = weights.shape[0] layer_attrs["out_channels"] = weights.shape[0]
layer_attrs["kernel_size"] = weights.shape[2:] layer_attrs["kernel_size"] = weights.shape[2:]
# 处理输入2,即%10 # 处理输入2,即%10
...@@ -1033,6 +1037,10 @@ def aten__convolution(mapper, graph, node): ...@@ -1033,6 +1037,10 @@ def aten__convolution(mapper, graph, node):
layer_attrs["output_padding"] = mapper.attrs[inputs_name[7]] layer_attrs["output_padding"] = mapper.attrs[inputs_name[7]]
# 处理输入8,即%12 # 处理输入8,即%12
layer_attrs["groups"] = mapper.attrs[inputs_name[8]] layer_attrs["groups"] = mapper.attrs[inputs_name[8]]
if mapper.attrs[inputs_name[6]]:
layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[inputs_name[
8]]
else:
layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[ layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[
8]] 8]]
if mapper.attrs[inputs_name[6]]: if mapper.attrs[inputs_name[6]]:
...@@ -1052,6 +1060,71 @@ def aten__convolution(mapper, graph, node): ...@@ -1052,6 +1060,71 @@ def aten__convolution(mapper, graph, node):
return current_inputs, current_outputs return current_inputs, current_outputs
def aten_conv_transpose2d(mapper, graph, node):
""" 构造conv_transpose2d的PaddleLayer。
TorchScript示例:
%input.10 : Tensor = aten::conv_transpose2d(%input.1, %18, %10, %19, %20, %21, %13, %22)
参数含义:
%input.10 (Tensor): 输出,卷积后的结果。
%input.8 (Tensor): 需要进行卷积的特征层。
%18 (Tensor): weights。
%10 (Tensor): bias。
%19 (list): 步长大小。
%20 (list): 填充大小。
%21 (int/tuple): 输出形状上一侧额外添加的大小。
%13 (int): 二维卷积层的组数。
%22 (int/tuple): 空洞大小。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("conv2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%18
weights = mapper.pytorch_params[inputs_name[1]]
mapper.paddle_params[op_name + ".weight"] = weights
layer_attrs["out_channels"] = weights.shape[1]
layer_attrs["kernel_size"] = weights.shape[2:]
# 处理输入2,即%10
if inputs_name[2] in mapper.pytorch_params:
bias = mapper.pytorch_params[inputs_name[2]]
if bias is not None:
mapper.paddle_params[op_name + ".bias"] = bias
else:
layer_attrs["bias_attr"] = False
else:
layer_attrs["bias_attr"] = False
# 处理输入3,即%19
layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
# 处理输入4,即%20
layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
# 处理输入5,即%21
layer_attrs["output_padding"] = mapper.attrs[inputs_name[5]]
# 处理输入6,即%13
layer_attrs["groups"] = mapper.attrs[inputs_name[6]]
# 处理输入7,即%22
layer_attrs["dilation"] = mapper.attrs[inputs_name[7]]
layer_attrs['in_channels'] = weights.shape[0] * mapper.attrs[inputs_name[
6]]
graph.add_layer(
"paddle.nn.Conv2DTranspose",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def aten_cos(mapper, graph, node): def aten_cos(mapper, graph, node):
""" 构造数学计算cos的PaddleLayer。 """ 构造数学计算cos的PaddleLayer。
......
...@@ -21,6 +21,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn", ...@@ -21,6 +21,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
"paddle.nn.Conv2D": "conv", "paddle.nn.Conv2D": "conv",
"paddle.nn.Embedding": "embedding", "paddle.nn.Embedding": "embedding",
"paddle.nn.Linear": "linear", "paddle.nn.Linear": "linear",
"paddle.nn.Conv2DTranspose": "conv",
"paddle.nn.ReLU": "relu", "paddle.nn.ReLU": "relu",
"paddle.nn.ReLU6": "relu", "paddle.nn.ReLU6": "relu",
"paddle.nn.Softmax": "softmax", "paddle.nn.Softmax": "softmax",
...@@ -35,7 +36,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn", ...@@ -35,7 +36,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
"paddle.nn.GELU": "gelu", "paddle.nn.GELU": "gelu",
"paddle.nn.Hardtanh": "tanh", "paddle.nn.Hardtanh": "tanh",
"paddle.nn.LeakyReLU": "leakly_relu"} "paddle.nn.LeakyReLU": "leakly_relu"}
NN_KERNEL_WITH_PARAMS = list(NN_KERNEL_NAME.keys())[:5] NN_KERNEL_WITH_PARAMS = list(NN_KERNEL_NAME.keys())[:6]
def rename_layers(layers, param_tree=None): def rename_layers(layers, param_tree=None):
""" 对子模块的输入输出等进行重命名。 """ 对子模块的输入输出等进行重命名。
......
...@@ -22,13 +22,7 @@ from x2paddle.core.util import * ...@@ -22,13 +22,7 @@ from x2paddle.core.util import *
class DygraphInterpolateBilinearFuser(FuseBase): class DygraphInterpolateBilinearFuser(FuseBase):
def __init__(self): def __init__(self):
super(DygraphInterpolateBilinearFuser, self).__init__(graph_type="dygraph") super(DygraphInterpolateBilinearFuser, self).__init__(graph_type="dygraph")
import torch self.pattenrs = list()
torch_version = torch.__version__
torch_version_part = torch_version.split(".")
if int(torch_version_part[0]) == 1 and int(torch_version_part[1]) > 5:
self.version_gt_150 = True
else:
self.version_gt_150 = False
def build_pattern(self): def build_pattern(self):
""" 描述需要替换的双线性插值图结构。 """ 描述需要替换的双线性插值图结构。
...@@ -72,27 +66,27 @@ class DygraphInterpolateBilinearFuser(FuseBase): ...@@ -72,27 +66,27 @@ class DygraphInterpolateBilinearFuser(FuseBase):
def gen_name(id): def gen_name(id):
return "x" + str(id) return "x" + str(id)
if self.version_gt_150: pattern = PaddleGraph(graph_type="dygraph")
self.pattern.add_layer( pattern.add_layer(
"prim.shape", "prim.shape",
inputs={"input": "interpolate-input-0"}, inputs={"input": "interpolate-input-0"},
outputs=[gen_name(9)]) outputs=[gen_name(9)])
self.pattern.add_layer( pattern.add_layer(
"prim.len", "prim.len",
inputs={"input": gen_name(9)}, inputs={"input": gen_name(9)},
outputs=[gen_name(9)]) outputs=[gen_name(9)])
self.pattern.add_layer( pattern.add_layer(
"prim.sub", "prim.sub",
inputs={"x": gen_name(9)}, inputs={"x": gen_name(9)},
outputs=[gen_name(10)], outputs=[gen_name(10)],
y=2) y=2)
self.pattern.add_layer( pattern.add_layer(
"prim.list", inputs={}, outputs=[gen_name(11)]) "prim.list", inputs={}, outputs=[gen_name(11)])
self.pattern.add_layer( pattern.add_layer(
"prim.loop", "prim.loop",
inputs={"input": gen_name(10)}, inputs={"input": gen_name(10)},
outputs=[gen_name(12.1), gen_name(12.2)]) outputs=[gen_name(12.1), gen_name(12.2)])
loop_layer = self.pattern.layers[list(self.pattern.layers.keys())[ loop_layer = pattern.layers[list(pattern.layers.keys())[
-1]] -1]]
pattern_block = PaddleGraph(loop_layer, graph_type="dygraph") pattern_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block.add_layer( pattern_block.add_layer(
...@@ -102,7 +96,7 @@ class DygraphInterpolateBilinearFuser(FuseBase): ...@@ -102,7 +96,7 @@ class DygraphInterpolateBilinearFuser(FuseBase):
element=None) element=None)
loop_layer.inputs["input-0"] = gen_name(11) loop_layer.inputs["input-0"] = gen_name(11)
loop_layer.add_block(pattern_block) loop_layer.add_block(pattern_block)
self.pattern.add_layer( pattern.add_layer(
"prim.tuple", "prim.tuple",
inputs={ inputs={
"input0": "interpolate-input-0", "input0": "interpolate-input-0",
...@@ -112,17 +106,17 @@ class DygraphInterpolateBilinearFuser(FuseBase): ...@@ -112,17 +106,17 @@ class DygraphInterpolateBilinearFuser(FuseBase):
input2=None, input2=None,
input3=None) input3=None)
self.pattern.add_layer( pattern.add_layer(
"prim.eq", "prim.eq",
inputs={"x": "interpolate-input-2"}, inputs={"x": "interpolate-input-2"},
outputs=[gen_name(10.1)], outputs=[gen_name(10.1)],
y=3) y=3)
self.pattern.add_layer( pattern.add_layer(
"prim.if", "prim.if",
inputs={"input": gen_name(10.1)}, inputs={"input": gen_name(10.1)},
outputs=[gen_name(14)]) outputs=[gen_name(14)])
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[ if_layer1 = pattern.layers[list(pattern.layers.keys())[
-1]] -1]]
pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph") pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block.add_layer( pattern_block.add_layer(
...@@ -259,14 +253,14 @@ class DygraphInterpolateBilinearFuser(FuseBase): ...@@ -259,14 +253,14 @@ class DygraphInterpolateBilinearFuser(FuseBase):
'input-9': 'interpolate-input-3', 'input-9': 'interpolate-input-3',
'input-10': 'interpolate-input-0' 'input-10': 'interpolate-input-0'
}) })
self.pattern.build(inputs={ pattern.build(inputs={
"input-0": "interpolate-input-0", "input-0": "interpolate-input-0",
"input-1": "interpolate-input-1", "input-1": "interpolate-input-1",
"input-2": "interpolate-input-2", "input-2": "interpolate-input-2",
"input-3": "interpolate-input-3", "input-3": "interpolate-input-3",
"input-4": "interpolate-input-4" "input-4": "interpolate-input-4"
}) })
self.patterns.append(pattern)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册