提交 12dc7fb1 编写于 作者: S SunAhong1993

for sript

上级 e1d7b069
......@@ -209,7 +209,7 @@ def onnx2paddle(model_path, save_dir, paddle_type, params_merge=False):
mapper.save_inference_model(save_dir, params_merge)
def pytorch2paddle(module, save_dir, jit_type, input_examples):
def pytorch2paddle(module, save_dir, jit_type, input_examples=None):
# check pytorch installation and version
try:
import torch
......@@ -232,7 +232,7 @@ def pytorch2paddle(module, save_dir, jit_type, input_examples):
if jit_type == "trace":
model = TraceDecoder(module, input_examples)
else:
model = ScriptDecoder(module)
model = ScriptDecoder(module, input_examples)
mapper = PyTorchOpMapper(model)
mapper.paddle_graph.build()
print("Model optimizing ...")
......@@ -324,10 +324,6 @@ def main():
if args.params_merge:
params_merge = True
onnx2paddle(args.model, args.save_dir, args.paddle_type, params_merge)
elif args.framework == "pytorch":
assert args.model is not None, "--model should be defined while translating pytorch model"
pytorch2paddle(args.model, args.save_dir, args.jit_type, args.input_files)
elif args.framework == "paddle2onnx":
assert args.model is not None, "--model should be defined while translating paddle model to onnx"
paddle2onnx(args.model, args.save_dir, opset_version=args.onnx_opset)
......
......@@ -281,12 +281,13 @@ class PaddleGraph(object):
else:
self.gen_dygraph_code(save_dir)
self.dump_dygraph_parameter(save_dir)
# 动转静
if len(self.inputs_info) > 0:
input_shapes = list()
input_types = list()
for input_name in self.inputs:
input_shapes.append(self.inputs_info[input_name][0])
input_types.append(self.inputs_info[input_name][1])
# 如果input_files非空,则导出推理模型;其值类似[[None, 3, 224, 224]]
self.dygraph2static(save_dir, input_shapes, input_types)
def gen_static_code(self, code_dir):
......@@ -424,9 +425,7 @@ class PaddleGraph(object):
if self.edges_out.get(layer_id, 0) == 0:
for i, output_name in enumerate(layer.outputs):
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel) or \
(layer.kernel == "paddle.to_tensor" and layer.attrs["data"].startswith("params["))or \
"paddle.fluid.dygraph" in layer.kernel:
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel):
if i == 0:
continue
if output_name not in self.outputs:
......@@ -512,6 +511,8 @@ class PaddleGraph(object):
return_code = "return {}".format(", ".join(self.outputs))
self.forward_func.extend(gen_codes([return_code], indent=2))
for code_line in self.forward_func:
if "assert [1, 1] == 1 or [1, 1] == [1, 1], 'The [1, 1] must be [1, [1, 1]]!'" in code_line:
continue
f.write(code_line)
for code_line in self.run_func:
f.write(code_line)
......@@ -593,7 +594,7 @@ class PaddleGraph(object):
line = line.strip(", ")
line += ")"
if layer.kernel == "self.create_parameter":
self.init_func.extend(gen_codes(["self." + line], indent=indent))
self.init_func.extend(gen_codes(["self." + line], indent=2))
self.forward_func.extend(gen_codes(["{} = self.{}".format(layer.outputs[0],
layer.outputs[0])], indent=indent))
else:
......
......@@ -41,10 +41,10 @@ class ScriptDecoder(Decoder):
script_path (str): ScriptModule保存路径。
model_path (str): PyTorchModule保存路径。
"""
def __init__(self, module):
def __init__(self, module, input_examples=None):
self.script = torch.jit.script(module)
self.graph = self._optimize_graph(self.script.inlined_graph)
self.input_examples = None
self.input_examples = input_examples
class TraceDecoder(Decoder):
""" PyTorchModule后使用trace方式转换为ScriptModule。
......@@ -66,3 +66,4 @@ class TraceDecoder(Decoder):
self.graph = self._optimize_graph(self.script.inlined_graph)
self.input_examples = input_examples
......@@ -1180,7 +1180,7 @@ class OpSet9():
scale=beta)
add_inputs = {"x": val_mm, "y": var_beta}
self.paddle_graph.add_layer(
"paddle.addd",
"paddle.add",
inputs=add_inputs,
outputs=[node.layer_name])
......
......@@ -83,16 +83,21 @@ def aten_adaptive_avg_pool2d(mapper, graph, node):
# 处理输入1,即%_output_size.1
if inputs_name[1] in mapper.attrs:
layer_attrs["output_size"] = mapper.attrs[inputs_name[1]]
graph.add_layer(
"paddle.nn.AdaptiveAvgPool2D",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs, scope_name)
layer_inputs["output_size"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.nn.AdaptiveAvgPool2D",
"paddle.nn.functional.adaptive_avg_pool2d",
inputs=layer_inputs,
outputs=layer_outputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -828,7 +833,7 @@ def aten_constant_pad_nd(mapper, graph, node):
outputs=[inputs_name[0] + "_if", output_name],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.sub",
inputs={"y": inputs_name[0] + "_len"},
......@@ -859,7 +864,7 @@ def aten_constant_pad_nd(mapper, graph, node):
outputs=[output_name],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
layer_inputs["input"] = inputs_name[0]
block.add_layer(
kernel, inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, **layer_attrs)
......@@ -1186,7 +1191,7 @@ def aten_dim(mapper, graph, node):
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["inputs"] = inputs_name[0]
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1468,7 +1473,7 @@ def aten_expand(mapper, graph, node):
outputs=[inputs_name[0] + "_if1", inputs_name[1] + "_var"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"paddle.cast",
inputs={"x": inputs_name[0]},
......@@ -1483,7 +1488,7 @@ def aten_expand(mapper, graph, node):
dtype=string("int64"),
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
......@@ -1558,7 +1563,7 @@ def aten_expand_as(mapper, graph, node):
outputs=[inputs_name[0] + "_if1"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.type",
inputs={"input": inputs_name[1]},
......@@ -1571,7 +1576,7 @@ def aten_expand_as(mapper, graph, node):
scope_name=scope_name,
dtype=inputs_name[1] + "_type")
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
if_layer.inputs["input-1"] = inputs_name[1]
......@@ -1582,7 +1587,7 @@ def aten_expand_as(mapper, graph, node):
outputs=[inputs_name[0] + "_if2"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": layer_outputs[0]},
......@@ -2420,14 +2425,14 @@ def aten_masked_fill_(mapper, graph, node):
outputs=[inputs_name[2] + "_if"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.equal",
inputs={"input": inputs_name[1] + "_mask"},
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.mul",
inputs={"x": inputs_name[1] + "_mask",
......@@ -2528,14 +2533,14 @@ def aten_masked_fill(mapper, graph, node):
outputs=[inputs_name[2] + "_if"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.equal",
inputs={"input": inputs_name[1] + "_mask"},
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.mul",
inputs={"x": inputs_name[1] + "_mask",
......@@ -4157,14 +4162,14 @@ def aten_upsample_bilinear2d(mapper, graph, node):
outputs=[inputs_name[0] + "_if1"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.var2list",
inputs={"input": inputs_name[1]},
outputs=[inputs_name[1]],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[1]
# 处理输入2,即%5421
......
......@@ -208,6 +208,10 @@ def prim_if(layer, indent=1, init_func=[], forward_func=[], layer_id=None, diffe
line = "if {} :".format(get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
block = layer.blocks[0]
if len(block.layers) == 0:
line = "pass"
forward_func.extend(gen_codes([line], indent=indent + 1))
else:
b_init_lines, b_forward_lines = block.gen_dygraph_code(indent=indent + 1)
init_func.extend(b_init_lines)
forward_func.extend(b_forward_lines)
......
......@@ -77,7 +77,7 @@ class PyTorchOpMapper(OpMapper):
current_node_outputs.extend(outputs)
# 初始化
graph = PaddleGraph(parent_layer, graph_type="dygraph")
graph = PaddleGraph(parent_layer=parent_layer, graph_type="dygraph")
if "TopLevelTracedModule" in str(type(self.script)):
graph.set_script(self.script)
current_node_outputs = []
......@@ -277,7 +277,7 @@ class PyTorchOpMapper(OpMapper):
control_output_id = index - 1
output_node_name = parent_layer.outputs[control_output_id]
current_outputs = [output_node_name]
self._check_input(graph, node, input_node_name, current_outputs)
self._check_input(graph, node, input_node_name, current_outputs, scope_name)
graph.add_layer(
"prim.equal",
inputs={'input': input_node_name},
......
......@@ -13,6 +13,7 @@
# limitations under the License.
import numpy as np
import copy
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
from x2paddle.core.util import *
......@@ -21,11 +22,13 @@ from x2paddle.core.util import *
class DygraphAdaptivePool2dFuser(FuseBase):
def __init__(self):
super(DygraphAdaptivePool2dFuser, self).__init__(graph_type="dygraph")
self.patterns = list()
def build_pattern(self):
""" 描述需要替换的adaptive pool2d图结构。
adaptive pool2d层模式python实现代码示例:
x68 = fluid.layers.shape(input=x60)
模式一:
x68 = prim.shape(input=x60)
x69 = len(x68)
x70 = x69 <= 2
if x70 :
......@@ -38,57 +41,64 @@ class DygraphAdaptivePool2dFuser(FuseBase):
for _x79 in range(x77):
x80 = [6, 6][_x79]
x73.append(x80)
x81 = fluid.layers.adaptive_pool2d(input=x60, pool_size=x73, pool_type='avg')
x81 = paddle.nn.functional.adaptive_avg_pool2d(input=x60, pool_size=x73, pool_type='avg')
模式二:
x64 = x60.shape
x65 = len(x64)
x66 = x65 > 2
if x66 :
pass
else:
raise RaiseException('AssertionError: ')
x69 = self.pool2d3(x60)
"""
def gen_name(id):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
# 模式一:
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"prim.shape",
inputs={'input': "pool-input-0"},
outputs=[gen_name(1)])
self.pattern.add_layer(
pattern.add_layer(
"prim.len", inputs={"input": gen_name(1)}, outputs=[gen_name(6)])
self.pattern.add_layer(
pattern.add_layer(
"prim.le", inputs={"x": gen_name(6)}, outputs=[gen_name(8)], y=2)
self.pattern.add_layer("prim.if", {'input': gen_name(8)}, [gen_name(9)])
if_layer = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer, graph_type="dygraph")
pattern.add_layer("prim.if", {'input': gen_name(8)}, [gen_name(9)])
if_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
pattern_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(9)],
input="Exception")
if_layer.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(pattern_block1)
self.pattern.add_layer("prim.list", inputs={}, outputs=[gen_name(10)])
self.pattern.add_layer(
pattern.add_layer("prim.list", inputs={}, outputs=[gen_name(10)])
pattern.add_layer(
"prim.slice",
inputs={"input": gen_name(1), },
outputs=[gen_name(12)],
start=-1,
end=100,
step=1)
self.pattern.add_layer(
outputs=[gen_name(12)])
pattern.add_layer(
"prim.len", inputs={"input": gen_name(12)}, outputs=[gen_name(14)])
self.pattern.add_layer(
pattern.add_layer(
"prim.list",
inputs={"input1": gen_name(14)},
outputs=[gen_name(15)],
input0=2)
self.pattern.add_layer(
outputs=[gen_name(15)])
pattern.add_layer(
"prim.min", inputs={"input": gen_name(15)}, outputs=[gen_name(16)])
self.pattern.add_layer("prim.loop", {'input': gen_name(16)},
pattern.add_layer("prim.loop", {'input': gen_name(16)},
[gen_name(17), gen_name(18)])
loop_layer = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
loop_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block.add_layer(
"prim.getitem",
inputs={"index": gen_name(18)},
outputs=[gen_name(19)],
list=[6, 6])
outputs=[gen_name(19)])
pattern_block.add_layer(
"prim.append",
inputs={"list": gen_name(10),
......@@ -97,13 +107,44 @@ class DygraphAdaptivePool2dFuser(FuseBase):
loop_layer.inputs["input-0"] = gen_name(10)
loop_layer.add_block(pattern_block)
pool_attrs = {'pool_type': string("avg")}
self.pattern.add_layer(
"fluid.layers.adaptive_pool2d",
pattern.add_layer(
"paddle.nn.functional.adaptive_avg_pool2d",
inputs={'input': "pool-input-0",
"pool_size": gen_name(10)},
outputs=[gen_name(21)],
**pool_attrs)
self.pattern.build(inputs={"input-0": "pool-input-0", })
pattern.build(inputs={"input-0": "pool-input-0", })
self.patterns.append(pattern)
# 模式二:
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"prim.shape",
inputs={'input': "pool-input-0"},
outputs=[gen_name(0)])
pattern.add_layer(
"prim.len", inputs={"input": gen_name(0)}, outputs=[gen_name(1)])
pattern.add_layer(
"prim.gt", inputs={"x": gen_name(1)}, outputs=[gen_name(2)], y=2)
pattern.add_layer("prim.if", {'input': gen_name(2)}, [gen_name(3)])
if_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(pattern_block0)
pattern_block1 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
pattern_block1.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(4)],
input="Exception")
if_layer.add_block(pattern_block1)
pattern.add_layer(
"paddle.nn.AdaptiveAvgPool2D",
inputs={"input": "pool-input-0"},
outputs=["pool1", gen_name(5)])
pattern.build(inputs={"input-0": "pool-input-0",
"input-1": "pool-input-0",})
self.patterns.append(pattern)
def insert_new_layer(self, graph, parameters, matches):
parameters = graph.parameters
......@@ -114,20 +155,21 @@ class DygraphAdaptivePool2dFuser(FuseBase):
def gen_new_layer(self, parameters, matches):
layers_id = list(matches.keys())
if matches[layers_id[-1]].kernel == "paddle.nn.functional.adaptive_avg_pool2d":
layer = matches[layers_id[11]]
pool_size = layer.attrs["list"]
layer = matches[layers_id[0]]
input_name = layer.inputs["input"]
layer = matches[layers_id[-1]]
output_name = layer.outputs[0]
pool_type = layer.attrs["pool_type"]
attrs = dict()
attrs["pool_size"] = pool_size
attrs["pool_type"] = pool_type
attrs["output_size"] = pool_size
new_layer = PaddleLayer(
layers_id[0],
"fluid.layers.adaptive_pool2d",
"paddle.nn.functional.adaptive_avg_pool2d",
inputs={"input": input_name},
outputs=[output_name],
**attrs)
else:
new_layer = copy.deepcopy(matches[layers_id[-1]])
return new_layer
......@@ -25,33 +25,33 @@ class DygraphBatchNorm2dFuser(FuseBase):
def build_pattern(self):
""" 描述需要替换的batchnorm2d图结构。
batchnorm2d层模式python实现代码示例:
x336 = fluid.layers.shape(input=x334)
x336 = len(x336)
x337 = x336 != 4
if x337 :
x2011 = x2009.shape
x2011 = len(x2011)
x2012 = x2011 != 4
if x2012 :
raise RaiseException('Exception')
if False :
x351 = fluid.layers.shape(input=x334)
x352 = x351[0]
x353 = len(x351)
x354 = x353 - 2
x357 = x352
for _x356 in range(x354):
x358 = _x356 + 2
x359 = x351[x358]
x360 = x357 * x359
x355 = x360
x361 = x355 == 1
if x361 :
x2026 = x2009.shape
x2027 = x2026[0]
x2028 = len(x2026)
x2029 = x2028 - 2
x2032 = x2027
for _x2031 in range(x2029):
x2033 = _x2031 + 2
x2034 = x2026[x2033]
x2035 = x2032 * x2034
x2030 = x2035
x2036 = x2030 == 1
if x2036 :
raise RaiseException('Exception')
x364 = self.batchnorm7(x334)
x2039 = self.batchnorm50(x2009)
"""
def gen_name(id):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "bn-input-0"},
outputs=[gen_name(0)])
self.pattern.add_layer(
......@@ -60,20 +60,20 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.ne", inputs={"x": gen_name(0)}, outputs=[gen_name(1)], y=4)
self.pattern.add_layer("prim.if", {'input': gen_name(1)}, [gen_name(2)])
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(3)],
input="Exception")
if_layer1.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
if_layer1.add_block(pattern_block1)
self.pattern.add_layer("prim.if", {}, [gen_name(4)], input=False)
if_layer2 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph")
pattern_block0.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "bn-input-0"},
outputs=[gen_name(5)])
pattern_block0.add_layer(
......@@ -93,7 +93,7 @@ class DygraphBatchNorm2dFuser(FuseBase):
outputs=[gen_name(8.1), gen_name(10)])
loop_layer = pattern_block0.layers[list(pattern_block0.layers.keys())[
-1]]
pattern_block0_block0 = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block0_block0 = PaddleGraph(parent_layer=loop_layer, graph_type="dygraph")
pattern_block0_block0.add_layer(
"prim.add", inputs={"x": gen_name(10)}, outputs=[gen_name(11)], y=2)
pattern_block0_block0.add_layer(
......@@ -119,27 +119,24 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.if", inputs={"input": gen_name(14)}, outputs=[gen_name(15)])
if_layer21 = pattern_block0.layers[list(pattern_block0.layers.keys())[
-1]]
pattern_block0_block0 = PaddleGraph(if_layer21, graph_type="dygraph")
pattern_block0_block0 = PaddleGraph(parent_layer=if_layer21, graph_type="dygraph")
pattern_block0_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(15)],
input="Exception")
if_layer21.add_block(pattern_block0_block0)
pattern_block0_block1 = PaddleGraph(if_layer21, graph_type="dygraph")
pattern_block0_block1 = PaddleGraph(parent_layer=if_layer21, graph_type="dygraph")
if_layer21.add_block(pattern_block0_block1)
if_layer2.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph")
if_layer2.add_block(pattern_block1)
if_layer2.inputs["input-0"] = "bn-input-0"
self.pattern.add_layer(
"paddle.nn.BatchNorm",
inputs={"input": "bn-input-0"},
outputs=[gen_name(16), gen_name(17)],
is_test=True,
num_channels=160,
momentum=0.1,
epsilon=0.001)
is_test=True)
self.pattern.build(inputs={"input-0": "bn-input-0"})
def insert_new_layer(self, graph, parameters, matches):
......@@ -148,9 +145,6 @@ class DygraphBatchNorm2dFuser(FuseBase):
graph.layers[new_layer_id] = new_layer
matches.pop(new_layer_id)
# for layer in matches.values():
# print(layer.outputs)
# print("-------")
def gen_new_layer(self, parameters, matches):
layers_id = list(matches.keys())
......
......@@ -31,14 +31,14 @@ class DygraphFcFuser(FuseBase):
x134 = x133 == 2
if x134 :
classifier_6_weight = self.classifier_6_weight
x136 = fluid.layers.transpose(x=classifier_6_weight, perm=[1, 0])
x136 = paddle.transpose(x=classifier_6_weight, perm=[1, 0])
classifier_6_bias = self.classifier_6_bias
x137 = paddle.addmm(input=classifier_6_bias, x=x128, y=x136, beta=1, alpha=1)
x135 = x137
else:
classifier_6_weight = self.classifier_6_weight
x138 = fluid.layers.transpose(x=classifier_6_weight, perm=[1, 0])
x139 = fluid.layers.matmul(x=x128, y=x138)
x138 = paddle.transpose(x=classifier_6_weight, perm=[1, 0])
x139 = paddle.matmul(x=x128, y=x138)
classifier_6_bias = self.classifier_6_bias
x140 = x139 + 1 * classifier_6_bias
x135 = x140
......@@ -48,7 +48,7 @@ class DygraphFcFuser(FuseBase):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "fc-input-0"},
outputs=[gen_name(2)])
self.pattern.add_layer(
......@@ -61,22 +61,20 @@ class DygraphFcFuser(FuseBase):
self.pattern.add_layer("prim.if", {'input': gen_name(3)}, [gen_name(4)])
self.pattern.outputs.append(gen_name(4))
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block0.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(5)],
value="params[{}]".format(string(gen_name(5))))
outputs=[gen_name(5)])
pattern_block0.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs={"x": gen_name(5)},
outputs=[gen_name(6)],
perm=[1, 0])
pattern_block0.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(7)],
value="params[{}]".format(string(gen_name(7))))
outputs=[gen_name(7)])
pattern_block0.add_layer(
"paddle.addmm",
inputs={"input": gen_name(7),
......@@ -90,14 +88,13 @@ class DygraphFcFuser(FuseBase):
pattern_block0.add_layer(
"prim.equal", inputs={'input': gen_name(8)}, outputs=[gen_name(4)])
if_layer1.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block1.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(5)],
value="params[{}]".format(string(gen_name(5))))
outputs=[gen_name(5)])
pattern_block1.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs={"x": gen_name(5)},
outputs=[gen_name(6)],
perm=[1, 0])
......@@ -108,10 +105,9 @@ class DygraphFcFuser(FuseBase):
outputs=[gen_name(9)])
if_layer1.inputs["input-1"] = "fc-input-0"
pattern_block1.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(12)],
value="params[{}]".format(string(gen_name(12))))
outputs=[gen_name(12)])
pattern_block1.add_layer(
"prim.add_",
inputs={"x": gen_name(9),
......@@ -137,9 +133,9 @@ class DygraphFcFuser(FuseBase):
layer = matches[layers_id[3]]
output_name = layer.outputs[0]
layer = matches[layers_id[4]]
weight_name = layer.attrs["value"][8:-2]
weight_name = layer.outputs[0]
layer = matches[layers_id[6]]
bias_name = layer.attrs["value"][8:-2]
bias_name = layer.outputs[0]
attrs = dict()
attrs["in_features"] = parameters[weight_name].shape[1]
attrs["out_features"] = parameters[weight_name].shape[0]
......
......@@ -24,9 +24,12 @@ class GraphOptimizer(object):
self.passes = ["trace_fc_fuse_pass"]
else:
self.passes = [
"dygraph_constant_fuse_pass", "dygraph_batchnorm2d_fuse_pass",
"dygraph_interpolate_bilinear_fuse_pass", "dygraph_fc_fuse_pass",
"dygraph_adaptive_pool2d_fuse_pass", "dygraph_reshape_fuse_pass",
"dygraph_constant_fuse_pass",
"dygraph_batchnorm2d_fuse_pass",
"dygraph_interpolate_bilinear_fuse_pass",
"dygraph_fc_fuse_pass",
"dygraph_adaptive_pool2d_fuse_pass",
"dygraph_reshape_fuse_pass",
"dygraph_dropout_fuse_pass"
]
elif source_frame == "caffe":
......
......@@ -85,7 +85,11 @@ class PatternMatcher(object):
# 判断subgraph中的节点是否被外部图使用到(如若被使用到则无效)
if layer_id in graph.edges_out:
if pattern_layer_id not in pattern.edges_out:
if not set(pattern_layer.outputs).issubset(
if "paddle.nn" in layer.kernel and "functional" not in layer.kernel:
pattern_layer_opt = pattern_layer.outputs[1:]
else:
pattern_layer_opt = pattern_layer.outputs
if not set(pattern_layer_opt).issubset(
pattern.outputs):
# 若pattern当前layer的输出是pattern的输出,则是正确的
if pattern_index == 0 or is_subblock:
......@@ -97,7 +101,11 @@ class PatternMatcher(object):
if len(graph.edges_out[layer_id]) != len(
pattern.edges_out[pattern_layer_id]):
# 如果在每个节点edges_in相同的情况下,edges_out数目相同则说明无节点在subgraph外被用到
if not set(pattern_layer.outputs).issubset(
if "paddle.nn" in layer.kernel and "functional" not in layer.kernel:
pattern_layer_opt = pattern_layer.outputs[1:]
else:
pattern_layer_opt = pattern_layer.outputs
if not set(pattern_layer_opt).issubset(
pattern.outputs):
# 若pattern当前layer的输出是pattern的输出,则是正确的
if pattern_index == 0 or is_subblock:
......@@ -105,6 +113,7 @@ class PatternMatcher(object):
else:
subgraph_id2layers.pop(layer_id)
continue
# 当为控制流时的处理
if layer.kernel == "prim.if" or layer.kernel == "prim.loop":
if len(pattern_layer.blocks) != len(layer.blocks):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册