From 12dc7fb15d0189285a74ee6f6e1534595f944c08 Mon Sep 17 00:00:00 2001 From: SunAhong1993 Date: Thu, 19 Nov 2020 11:37:48 +0800 Subject: [PATCH] for sript --- x2paddle/convert.py | 8 +- x2paddle/core/program.py | 23 +- x2paddle/decoder/pytorch_decoder.py | 5 +- .../dygraph/onnx2paddle/opset9/opset.py | 2 +- .../op_mapper/dygraph/pytorch2paddle/aten.py | 47 +- .../dygraph/pytorch2paddle/prim2code.py | 10 +- .../pytorch2paddle/pytorch_op_mapper.py | 4 +- .../fusion/dygraph/adaptive_pool2d_fuser.py | 134 +- .../fusion/dygraph/batchnorm2d_fuser.py | 60 +- x2paddle/optimizer/fusion/dygraph/fc_fuser.py | 40 +- .../dygraph/interpolate_bilinear_fuser.py | 1474 ++--------------- x2paddle/optimizer/optimizer.py | 9 +- x2paddle/optimizer/pattern_matcher.py | 13 +- 13 files changed, 317 insertions(+), 1512 deletions(-) diff --git a/x2paddle/convert.py b/x2paddle/convert.py index b7b1487..23850db 100644 --- a/x2paddle/convert.py +++ b/x2paddle/convert.py @@ -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) diff --git a/x2paddle/core/program.py b/x2paddle/core/program.py index d2ec150..be43c2b 100644 --- a/x2paddle/core/program.py +++ b/x2paddle/core/program.py @@ -281,13 +281,14 @@ class PaddleGraph(object): else: self.gen_dygraph_code(save_dir) self.dump_dygraph_parameter(save_dir) - 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) + # 动转静 + 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]) + self.dygraph2static(save_dir, input_shapes, input_types) def gen_static_code(self, code_dir): def write_code(f, code_list, indent=0): @@ -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: diff --git a/x2paddle/decoder/pytorch_decoder.py b/x2paddle/decoder/pytorch_decoder.py index ca3353e..43281d1 100644 --- a/x2paddle/decoder/pytorch_decoder.py +++ b/x2paddle/decoder/pytorch_decoder.py @@ -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。 @@ -65,4 +65,5 @@ class TraceDecoder(Decoder): exit(0) self.graph = self._optimize_graph(self.script.inlined_graph) self.input_examples = input_examples + diff --git a/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py b/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py index 39bfd23..d842747 100644 --- a/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py +++ b/x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py @@ -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]) diff --git a/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py b/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py index 529325a..d65e14b 100644 --- a/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py +++ b/x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py @@ -83,18 +83,23 @@ 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", - inputs=layer_inputs, - outputs=layer_outputs, - scope_name=scope_name, - **layer_attrs) + graph.add_layer( + "paddle.nn.functional.adaptive_avg_pool2d", + inputs=layer_inputs, + 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 diff --git a/x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py b/x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py index dabf8a9..ec323ae 100644 --- a/x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py +++ b/x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py @@ -208,9 +208,13 @@ 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] - 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) + 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) block = layer.blocks[1] if len(block.layers) > 0: b_init_lines, b_forward_lines = block.gen_dygraph_code( diff --git a/x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py b/x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py index 846aae9..0ebec0f 100644 --- a/x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py +++ b/x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py @@ -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}, diff --git a/x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py b/x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py index 12d8ecd..bc1d350 100644 --- a/x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py +++ b/x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py @@ -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,14 +107,45 @@ 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 new_layer = self.gen_new_layer(parameters, matches) @@ -114,20 +155,21 @@ class DygraphAdaptivePool2dFuser(FuseBase): def gen_new_layer(self, parameters, matches): layers_id = list(matches.keys()) - 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 - new_layer = PaddleLayer( - layers_id[0], - "fluid.layers.adaptive_pool2d", - inputs={"input": input_name}, - outputs=[output_name], - **attrs) + 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] + attrs = dict() + attrs["output_size"] = pool_size + new_layer = PaddleLayer( + layers_id[0], + "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 diff --git a/x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py b/x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py index 24dd27c..ab4e417 100644 --- a/x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py +++ b/x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py @@ -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()) diff --git a/x2paddle/optimizer/fusion/dygraph/fc_fuser.py b/x2paddle/optimizer/fusion/dygraph/fc_fuser.py index b171bc2..92e2000 100644 --- a/x2paddle/optimizer/fusion/dygraph/fc_fuser.py +++ b/x2paddle/optimizer/fusion/dygraph/fc_fuser.py @@ -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] diff --git a/x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py b/x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py index 358eca8..c7fad00 100644 --- a/x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py +++ b/x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import copy import numpy as np from x2paddle.optimizer.pattern_matcher import FuseBase from x2paddle.core.program import PaddleGraph, PaddleLayer @@ -32,135 +33,40 @@ class DygraphInterpolateBilinearFuser(FuseBase): def build_pattern(self): """ 描述需要替换的双线性插值图结构。 interpolate_bilinear层模式python实现代码示例: - x3016 = fluid.layers.shape(input=x3005) - x3016 = len(x3016) - x3017 = x3016 - 2 - x3018 = [] - for _x3020 in range(x3017): - x3018.append(None) - x3021 = (x3005, x8, None, None) - x3022 = fluid.layers.shape(input=x3005) - x3022 = len(x3022) - x3023 = x3022 == 3 - if x3023 : + x2195 = x2181.shape + x2195 = len(x2195) + x2196 = x2195 - 2 + x2197 = [] + for _x2199 in range(x2196): + x2197.append(None) + x2200 = (x2181, x8, None, None) + ... + x2267 = x2266 == 3 + if x2267 : raise RaiseException('Exception') - x3024 = None + x2268 = None else: - x3026 = fluid.layers.shape(input=x3005) - x3026 = len(x3026) - x3027 = x3026 == 4 - if x3027 : - x3044, x3045, x3046, x3047 = x3021 - x3048 = x3045 is None - if x3048 : - x3051 = x3046 is None - x3049 = x3051 - x3050 = x3045 - else: - x3052 = x3045 - x3049 = False - x3050 = x3052 - if x3049 : - raise RaiseException('Exception') - x3055 = x3050 is not None - if x3055 : - x3058 = x3050 - x3059 = x3046 is not None - x3056 = x3059 - x3057 = x3058 - else: - x3056 = False - x3057 = x3050 - if x3056 : - raise RaiseException('Exception') - x3060 = None - x3061 = None - else: - x3060 = x3046 - x3061 = x3057 - x3063 = x3060 is not None - if x3063 : - x3065 = x3060 - x3066 = len(x3065) - x3067 = x3066 != 2 - if x3067 : - raise RaiseException('Exception') - x3064 = x3065 - else: - x3064 = x3060 - x3070 = x3061 is not None - if x3070 : - x3072 = x3061 - x3071 = x3072 - else: - x3071 = None - if x3070 : - x3073 = x3071 - else: - x3074 = x3064 is not None - if x3074 : - x3076 = x3064 - x3075 = x3076 - else: - raise RaiseException('Exception') - x3075 = None - x3078 = x3047 is None - if x3078 : - x3080 = len(x3075) - x3081 = x3080 > 0 - x3086 = 0 - for x3083 in range(2147483647): - x3087 = x3075[x3086] - x3088 = math.floor(x3087) - x3089 = x3088 != x3087 - if x3089 : - x3090 = False - x3091 = x3089 - else: - x3090 = None - x3091 = None - if x3089 : - x3092 = x3090 - x3093 = x3091 - else: - x3092 = True - x3093 = x3089 - x3094 = x3086 + 1 - x3095 = x3094 < x3080 - x3096 = x3095 and x3092 - x3082 = x3093 - x3083 = x3094 - if x3082 : - import warnings - warnings.warn('The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. ', stacklevel=2) - x3099 = [] - for _x3101 in range(2): - x3102 = _x3101 + 2 - x3103 = fluid.layers.shape(x3044)[x3102] - x3104 = float(x3103) - x3105 = x3075[_x3101] - x3106 = x3104 * x3105 - x3107 = math.floor(x3106) - x3099.append(x3107) - x3073 = x3099 - x3108 = x3018[0] - x3109 = x3018[1] - x3073_isinstance = isinstance(x3073, paddle.fluid.Variable) - if x3073_isinstance : - x3073 = x3073.numpy().tolist() - assert x3108 == x3109, 'The x3108 must be x3109!' - x3110 = paddle.nn.functional.interpolate(x=x3005, size=x3073, scale_factor=x3108, align_corners=False, align_mode=0) - x3028 = x3110 + x2270 = x2181.shape + x2270 = len(x2270) + x2271 = x2270 == 4 + if x2271 : + x2274 = x2197[0] + x2275 = x2197[1] + x2233_isinstance = isinstance(x2233, paddle.fluid.Variable) + if x2233_isinstance : + x2233 = x2233.numpy().tolist() + x2276 = paddle.nn.functional.interpolate(x=x2181, size=x2233, scale_factor=x2274, align_corners=False, align_mode=0, mode='bilinear') + x2272 = x2276 else: - x3111 = fluid.layers.shape(input=x3005) - x3111 = len(x3111) - x3112 = x3111 == 5 - if x3112 : + x2277 = x2181.shape + x2277 = len(x2277) + x2278 = x2277 == 5 + if x2278 : raise RaiseException('Exception') else: raise RaiseException('Exception') - x3028 = None - x3024 = x3028 + x2272 = None + x2268 = x2272 """ def gen_name(id): @@ -168,7 +74,7 @@ class DygraphInterpolateBilinearFuser(FuseBase): if self.version_gt_150: self.pattern.add_layer( - "fluid.layers.shape", + "prim.shape", inputs={"input": "interpolate-input-0"}, outputs=[gen_name(9)]) self.pattern.add_layer( @@ -200,686 +106,36 @@ class DygraphInterpolateBilinearFuser(FuseBase): "prim.tuple", inputs={ "input0": "interpolate-input-0", - "input1": "interpolate-input-1", + "input1": "interpolate-input-4", }, - outputs=[gen_name(13)], + outputs=[gen_name(12)], input2=None, input3=None) - self.pattern.add_layer( - "fluid.layers.shape", - inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(14)]) - self.pattern.add_layer( - "prim.len", - inputs={"input": gen_name(14)}, - outputs=[gen_name(14)]) + self.pattern.add_layer( "prim.eq", - inputs={"x": gen_name(14)}, - outputs=[gen_name(15)], + inputs={"x": "interpolate-input-2"}, + outputs=[gen_name(10.1)], y=3) + self.pattern.add_layer( "prim.if", - inputs={"input": gen_name(15)}, - outputs=[gen_name(16)]) - if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[ - -1]] - pattern_block = PaddleGraph(if_layer1, graph_type="dygraph") - pattern_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(17)], - input="Exception") - pattern_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(16)], input=None) - if_layer1.add_block(pattern_block) - pattern_block = PaddleGraph(if_layer1, graph_type="dygraph") - pattern_block.add_layer( - "fluid.layers.shape", - inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(18)]) - pattern_block.add_layer( - "prim.len", - inputs={"input": gen_name(18)}, - outputs=[gen_name(18)]) - pattern_block.add_layer( - "prim.eq", - inputs={"x": gen_name(18)}, - outputs=[gen_name(19)], - y=4) - pattern_block.add_layer( - "prim.if", - inputs={"input": gen_name(19)}, - outputs=[gen_name(20)]) - if_layer2 = pattern_block.layers[list(pattern_block.layers.keys())[ - -1]] - pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph") - pattern_block_block.add_layer( - "prim.tuple_unpack", - inputs={"input": gen_name(13)}, - outputs=[ - gen_name(34), gen_name(35), gen_name(36), gen_name(37) - ]) - pattern_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(35)}, - outputs=[gen_name(38)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(38)}, - outputs=[gen_name(39), gen_name(40)]) - if_layer3 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer3, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(36)}, - outputs=[gen_name(41)], - y=None) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(41)}, - outputs=[gen_name(39)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(40)]) - if_layer3.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer3, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(42)]) - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(39)], input=False) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(40)]) - if_layer3.add_block(pattern_block_block_block) - if_layer3.inputs.update({ - "input-0": gen_name(36), - 'input-1': gen_name(35), - 'input-2': gen_name(35), - }) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(39)}, - outputs=[gen_name(43)]) - if_layer4 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer4, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(44)], - input="Exception") - if_layer4.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer4, graph_type="dygraph") - if_layer4.add_block(pattern_block_block_block) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(40)}, - outputs=[gen_name(45)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(45)}, - outputs=[gen_name(46), gen_name(47)]) - if_layer5 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer5, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(40)}, - outputs=[gen_name(48)]) - pattern_block_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(36)}, - outputs=[gen_name(49)], - y=None) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(49)}, - outputs=[gen_name(46)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(48)}, - outputs=[gen_name(47)]) - if_layer5.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer5, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(46)], input=False) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(40)}, - outputs=[gen_name(47)]) - if_layer5.add_block(pattern_block_block_block) - if_layer5.inputs.update({ - "input-0": gen_name(40), - "input-1": gen_name(36), - "input-3": gen_name(40) - }) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(46)}, - outputs=[gen_name(50), gen_name(51)]) - if_layer6 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer6, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(52)], - input="Exception") - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(50)], input=None) - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(51)], input=None) - if_layer6.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer6, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(36)}, - outputs=[gen_name(50)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(47)}, - outputs=[gen_name(51)]) - if_layer6.add_block(pattern_block_block_block) - if_layer6.inputs.update({ - "input-0": gen_name(36), - "input-1": gen_name(47) - }) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(50)}, - outputs=[gen_name(53)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(53)}, - outputs=[gen_name(54)]) - if_layer7 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer7, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(50)}, - outputs=[gen_name(55)]) - pattern_block_block_block.add_layer( - "prim.len", - inputs={"input": gen_name(55)}, - outputs=[gen_name(56)]) - pattern_block_block_block.add_layer( - "prim.ne", - inputs={"x": gen_name(56)}, - outputs=[gen_name(57)], - y=2) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(57)}, - outputs=[gen_name(58)]) - if_layer8 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer8, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(59)], - input="Exception") - if_layer8.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer8, graph_type="dygraph") - if_layer8.add_block(pattern_block_block_block_block) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(55)}, - outputs=[gen_name(54)]) - if_layer7.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer7, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(50)}, - outputs=[gen_name(54)]) - if_layer7.add_block(pattern_block_block_block) - if_layer7.inputs.update({ - "input-0": gen_name(50), - "input-1": gen_name(50) - }) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(51)}, - outputs=[gen_name(60)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(60)}, - outputs=[gen_name(61)]) - if_layer9 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer9, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(51)}, - outputs=[gen_name(62)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(62)}, - outputs=[gen_name(61)]) - if_layer9.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer9, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(54)}, - outputs=[gen_name(64)], - y=None) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(64)}, - outputs=[gen_name(65)]) - if_layer11 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer11, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(54)}, - outputs=[gen_name(66)]) - pattern_block_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(66)}, - outputs=[gen_name(65)]) - if_layer11.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer11, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(67)], - input="Exception") - pattern_block_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(65)], input=None) - if_layer11.add_block(pattern_block_block_block_block) - if_layer11.inputs.update({"input-0": gen_name(54), }) - pattern_block_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(37)}, - outputs=[gen_name(68)], - y=None) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(68)}, - outputs=[gen_name(69)]) - if_layer12 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer12, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.len", - inputs={"input": gen_name(65)}, - outputs=[gen_name(70)]) - pattern_block_block_block_block.add_layer( - "prim.gt", - inputs={"x": gen_name(70)}, - outputs=[gen_name(71)], - y=0) - pattern_block_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(72)], input=0) - pattern_block_block_block_block.add_layer( - "prim.loop", - inputs={}, - outputs=[gen_name(74), gen_name(75), gen_name(76.1)], - input=2147483647) - loop_layer = pattern_block_block_block_block.layers[list( - pattern_block_block_block_block.layers.keys())[-1]] - pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph") - pattern_loop_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(65), - "element": gen_name(72)}, - outputs=[gen_name(74.1)]) - pattern_loop_block.add_layer( - "prim.floor", - inputs={"input": gen_name(74.1)}, - outputs=[gen_name(75.1)]) - pattern_loop_block.add_layer( - "prim.ne", - inputs={"x": gen_name(75.1), - "y": gen_name(74.1)}, - outputs=[gen_name(76)]) - pattern_loop_block.add_layer( - "prim.if", - inputs={"input": gen_name(76)}, - outputs=[gen_name(77)]) - if_layer13 = pattern_loop_block.layers[list( - pattern_loop_block.layers.keys())[-1]] - pattern_loop_block_block = PaddleGraph( - if_layer13, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(77)], input=False) - if_layer13.add_block(pattern_loop_block_block) - pattern_loop_block_block = PaddleGraph( - if_layer13, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(77)], input=True) - if_layer13.add_block(pattern_loop_block_block) - pattern_loop_block.add_layer( - "prim.add", - inputs={"x": gen_name(72)}, - outputs=[gen_name(81)], - y=1) - pattern_loop_block.add_layer( - "prim.lt", - inputs={"x": gen_name(81), - "y": gen_name(70)}, - outputs=[gen_name(82)]) - pattern_loop_block.add_layer( - "prim.and", - inputs={"x": gen_name(82), - "y": gen_name(77)}, - outputs=[gen_name(83)]) - pattern_loop_block.add_layer( - "prim.equal", - inputs={"input": gen_name(76)}, - outputs=[gen_name(74)]) - pattern_loop_block.add_layer( - "prim.equal", - inputs={"input": gen_name(81)}, - outputs=[gen_name(75)]) - loop_layer.add_block(pattern_loop_block) - loop_layer.inputs.update({ - "input-0": gen_name(65), - "input-1": gen_name(72), - "input-2": gen_name(72), - "input-3": gen_name(70) - }) - pattern_block_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(74)}, - outputs=[gen_name(84)]) - if_layer15 = pattern_block_block_block_block.layers[list( - pattern_block_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block_block = PaddleGraph( - if_layer15, graph_type="dygraph") - pattern_block_block_block_block_block.add_layer( - "prim.warnings", - inputs={}, - outputs=[gen_name(85)], - stacklevel=2, - input="...") - if_layer15.add_block(pattern_block_block_block_block_block) - pattern_block_block_block_block_block = PaddleGraph( - if_layer15, graph_type="dygraph") - if_layer15.add_block(pattern_block_block_block_block_block) - if_layer12.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer12, graph_type="dygraph") - if_layer12.add_block(pattern_block_block_block_block) - if_layer12.inputs.update({ - "input-0": gen_name(65), - "input-1": gen_name(65), - }) - pattern_block_block_block.add_layer( - "prim.list", inputs={}, outputs=[gen_name(86)]) - pattern_block_block_block.add_layer( - "prim.loop", - inputs={}, - outputs=[gen_name(87), gen_name(88)], - input=2) - loop_layer = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph") - pattern_loop_block.add_layer( - "prim.add", - inputs={"x": gen_name(88)}, - outputs=[gen_name(89)], - y=2) - pattern_loop_block.add_layer( - "prim.shape_dim", - inputs={"input": gen_name(34), - "dim": gen_name(89)}, - outputs=[gen_name(90)]) - pattern_loop_block.add_layer( - "prim.float", - inputs={"input": gen_name(90)}, - outputs=[gen_name(91)]) - pattern_loop_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(65), - "element": gen_name(88)}, - outputs=[gen_name(92)]) - pattern_loop_block.add_layer( - "prim.mul", - inputs={"x": gen_name(91), - "y": gen_name(92)}, - outputs=[gen_name(93)]) - pattern_loop_block.add_layer( - "prim.floor", - inputs={"input": gen_name(93)}, - outputs=[gen_name(94)]) - pattern_loop_block.add_layer( - "prim.append", - inputs={"list": gen_name(86), - "element": gen_name(94)}, - outputs=[]) - loop_layer.add_block(pattern_loop_block) - loop_layer.inputs.update({ - "input-0": gen_name(34), - "input-1": gen_name(65), - "input-2": gen_name(86) - }) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(86)}, - outputs=[gen_name(61)]) - if_layer9.add_block(pattern_block_block_block) - if_layer9.inputs.update({ - "input-0": gen_name(51), - "input-1": gen_name(54), - "input-2": gen_name(54), - "input-3": gen_name(37), - "input-4": gen_name(34) - }) - pattern_block_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(11)}, - outputs=[gen_name(95)], - element=0) - pattern_block_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(11)}, - outputs=[gen_name(96)], - element=1) - pattern_block_block.add_layer( - "prim.isinstance", - inputs={"input": gen_name(61)}, - outputs=["interpolate-input-0_isinstance"], - cls="paddle.fluid.Variable") - pattern_block_block.add_layer( - "prim.if", {"input": "interpolate-input-0_isinstance"}, - outputs=["interpolate-input-0_if1"]) - if_layer_isinstance = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer_isinstance, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.var2list", - inputs={"input": gen_name(61)}, - outputs=[gen_name(61)]) - if_layer_isinstance.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer_isinstance, graph_type="dygraph") - if_layer_isinstance.add_block(pattern_block_block_block) - if_layer_isinstance.inputs["input-0"] = gen_name(61) - pattern_block_block.add_layer( - "prim.assert", - inputs={"key": gen_name(95), - "value": gen_name(96)}, - outputs=[gen_name(97) + "_assert"], - type="eq") - pattern_block_block.add_layer( - "paddle.nn.functional.interpolate", - inputs={ - "input": "interpolate-input-0", - "size": gen_name(61), - "scale_factor": gen_name(95) - }, - outputs=[gen_name(97)], - align_corners=False, - align_mode=0) - pattern_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(97)}, - outputs=[gen_name(20)]) - if_layer2.add_block(pattern_block_block) - pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph") - pattern_block_block.add_layer( - "fluid.layers.shape", - inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(98)]) - pattern_block_block.add_layer( - "prim.len", - inputs={"input": gen_name(98)}, - outputs=[gen_name(98)]) - pattern_block_block.add_layer( - "prim.eq", - inputs={"x": gen_name(98)}, - outputs=[gen_name(99)], - y=5) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(99)}, - outputs=[gen_name(100)]) - if_layer16 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer16, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(101)], - input="Exception") - if_layer16.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer16, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(102)], - input="Exception") - if_layer16.add_block(pattern_block_block_block) - pattern_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(20)], input=None) - if_layer2.add_block(pattern_block_block) - if_layer2.inputs.update({ - "input-0": gen_name(13), - "input-1": gen_name(13), - "input-2": "interpolate-input-0", - "input-3": gen_name(11), - "input-5": gen_name(11), - }) - pattern_block.add_layer( - "prim.equal", - inputs={"input": gen_name(20)}, - outputs=[gen_name(16)]) - if_layer1.add_block(pattern_block) - if_layer1.inputs.update({ - "input-2": "interpolate-input-0", - "input-4": gen_name(13), - "input-7": gen_name(11), - "input-9": gen_name(11), - "input-11": "interpolate-input-0", - "input-12": "interpolate-input-0", - }) - self.pattern.build(inputs={ - "input-0": "interpolate-input-0", - "input-1": "interpolate-input-1" - }) - else: - self.pattern.add_layer( - "fluid.layers.shape", - inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(9)]) - self.pattern.add_layer( - "prim.len", - inputs={"input": gen_name(9)}, - outputs=[gen_name(9)]) - self.pattern.add_layer( - "prim.sub", - inputs={"x": gen_name(9)}, - outputs=[gen_name(10)], - y=2) - self.pattern.add_layer( - "prim.list", inputs={}, outputs=[gen_name(11)]) - self.pattern.add_layer( - "prim.loop", - inputs={"input": gen_name(10)}, - outputs=[gen_name(12.1), gen_name(12.2)]) - loop_layer = self.pattern.layers[list(self.pattern.layers.keys())[ - -1]] - pattern_block = PaddleGraph(loop_layer, graph_type="dygraph") - pattern_block.add_layer( - "prim.append", - inputs={"list": gen_name(11)}, - outputs=[], - element=None) - loop_layer.inputs["input-0"] = gen_name(11) - loop_layer.add_block(pattern_block) - self.pattern.add_layer( - "prim.tuple", - inputs={ - "input0": "interpolate-input-0", - "input1": "interpolate-input-1", - }, - outputs=[gen_name(13)], - input2=None, - input3=None) - self.pattern.add_layer( - "fluid.layers.shape", - inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(14)]) - self.pattern.add_layer( - "prim.len", - inputs={"input": gen_name(14)}, + inputs={"input": gen_name(10.1)}, outputs=[gen_name(14)]) - self.pattern.add_layer( - "prim.eq", - inputs={"x": gen_name(14)}, - outputs=[gen_name(15)], - y=3) - self.pattern.add_layer( - "prim.if", - inputs={"input": gen_name(15)}, - outputs=[gen_name(16)]) if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[ -1]] - pattern_block = PaddleGraph(if_layer1, graph_type="dygraph") + pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph") pattern_block.add_layer( "prim.exception", inputs={}, - outputs=[gen_name(17)], + outputs=[gen_name(15)], input="Exception") pattern_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(16)], input=None) + "prim.equal", inputs={}, outputs=[gen_name(14)], input=None) if_layer1.add_block(pattern_block) - pattern_block = PaddleGraph(if_layer1, graph_type="dygraph") + pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph") pattern_block.add_layer( - "fluid.layers.shape", + "prim.shape", inputs={"input": "interpolate-input-0"}, outputs=[gen_name(18)]) pattern_block.add_layer( @@ -891,524 +147,28 @@ class DygraphInterpolateBilinearFuser(FuseBase): inputs={"x": gen_name(18)}, outputs=[gen_name(19)], y=4) + + pattern_block.add_layer( "prim.if", inputs={"input": gen_name(19)}, outputs=[gen_name(20)]) if_layer2 = pattern_block.layers[list(pattern_block.layers.keys())[ -1]] - pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph") - pattern_block_block.add_layer( - "prim.tuple_unpack", - inputs={"input": gen_name(13)}, - outputs=[ - gen_name(34), gen_name(35), gen_name(36), gen_name(37) - ]) - pattern_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(35)}, - outputs=[gen_name(38)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(38)}, - outputs=[gen_name(39), gen_name(40)]) - if_layer3 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer3, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(36)}, - outputs=[gen_name(41)], - y=None) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(41)}, - outputs=[gen_name(39)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(40)]) - if_layer3.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer3, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(42)]) - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(39)], input=False) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(35)}, - outputs=[gen_name(40)]) - if_layer3.add_block(pattern_block_block_block) - if_layer3.inputs.update({ - "input-0": gen_name(36), - 'input-1': gen_name(35), - 'input-2': gen_name(35), - }) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(39)}, - outputs=[gen_name(43)]) - if_layer4 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer4, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(44)], - input="Exception") - if_layer4.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer4, graph_type="dygraph") - if_layer4.add_block(pattern_block_block_block) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(40)}, - outputs=[gen_name(45)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(45)}, - outputs=[gen_name(46), gen_name(47)]) - if_layer5 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer5, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(40)}, - outputs=[gen_name(48)]) - pattern_block_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(36)}, - outputs=[gen_name(49)], - y=None) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(49)}, - outputs=[gen_name(46)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(48)}, - outputs=[gen_name(47)]) - if_layer5.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer5, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(46)], input=False) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(40)}, - outputs=[gen_name(47)]) - if_layer5.add_block(pattern_block_block_block) - if_layer5.inputs.update({ - "input-0": gen_name(40), - "input-1": gen_name(36), - "input-3": gen_name(40) - }) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(46)}, - outputs=[gen_name(50), gen_name(51)]) - if_layer6 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer6, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(52)], - input="Exception") - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(50)], input=None) - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(51)], input=None) - if_layer6.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer6, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(36)}, - outputs=[gen_name(50)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(47)}, - outputs=[gen_name(51)]) - if_layer6.add_block(pattern_block_block_block) - if_layer6.inputs.update({ - "input-0": gen_name(36), - "input-1": gen_name(47) - }) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(50)}, - outputs=[gen_name(53)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(53)}, - outputs=[gen_name(54)]) - if_layer7 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer7, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(50)}, - outputs=[gen_name(55)]) - pattern_block_block_block.add_layer( - "prim.len", - inputs={"input": gen_name(55)}, - outputs=[gen_name(56)]) - pattern_block_block_block.add_layer( - "prim.ne", - inputs={"x": gen_name(56)}, - outputs=[gen_name(57)], - y=2) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(57)}, - outputs=[gen_name(58)]) - if_layer8 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer8, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(59)], - input="Exception") - if_layer8.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer8, graph_type="dygraph") - if_layer8.add_block(pattern_block_block_block_block) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(55)}, - outputs=[gen_name(54)]) - if_layer7.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer7, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(50)}, - outputs=[gen_name(54)]) - if_layer7.add_block(pattern_block_block_block) - if_layer7.inputs.update({ - "input-0": gen_name(50), - "input-1": gen_name(50) - }) - pattern_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(51)}, - outputs=[gen_name(60)], - y=None) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(60)}, - outputs=[gen_name(61)]) - if_layer9 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer9, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(51)}, - outputs=[gen_name(62)]) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(62)}, - outputs=[gen_name(61)]) - if_layer9.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer9, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(61)], input=None) - if_layer9.add_block(pattern_block_block_block) - if_layer9.inputs.update({"input-0": gen_name(51)}) - pattern_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(60)}, - outputs=[gen_name(63)]) - if_layer10 = pattern_block_block.layers[list( - pattern_block_block.layers.keys())[-1]] - pattern_block_block_block = PaddleGraph( - if_layer10, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(61)}, - outputs=[gen_name(63)]) - if_layer10.add_block(pattern_block_block_block) - pattern_block_block_block = PaddleGraph( - if_layer10, graph_type="dygraph") - pattern_block_block_block.add_layer( - "prim.isnot", - inputs={"x": gen_name(54)}, - outputs=[gen_name(64)], - y=None) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(64)}, - outputs=[gen_name(65)]) - if_layer11 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer11, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(54)}, - outputs=[gen_name(66)]) - pattern_block_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(66)}, - outputs=[gen_name(65)]) - if_layer11.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer11, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.exception", - inputs={}, - outputs=[gen_name(67)], - input="Exception") - pattern_block_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(65)], input=None) - if_layer11.add_block(pattern_block_block_block_block) - if_layer11.inputs.update({"input-0": gen_name(54), }) - pattern_block_block_block.add_layer( - "prim.is", - inputs={"x": gen_name(37)}, - outputs=[gen_name(68)], - y=None) - pattern_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(68)}, - outputs=[gen_name(69)]) - if_layer12 = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block = PaddleGraph( - if_layer12, graph_type="dygraph") - pattern_block_block_block_block.add_layer( - "prim.len", - inputs={"input": gen_name(65)}, - outputs=[gen_name(70)]) - pattern_block_block_block_block.add_layer( - "prim.gt", - inputs={"x": gen_name(70)}, - outputs=[gen_name(71)], - y=0) - pattern_block_block_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(72)], input=0) - pattern_block_block_block_block.add_layer( - "prim.loop", - inputs={}, - outputs=[gen_name(74), gen_name(75), gen_name(76.1)], - input=2147483647) - loop_layer = pattern_block_block_block_block.layers[list( - pattern_block_block_block_block.layers.keys())[-1]] - pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph") - pattern_loop_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(65), - "element": gen_name(72)}, - outputs=[gen_name(74.1)]) - pattern_loop_block.add_layer( - "prim.floor", - inputs={"input": gen_name(74.1)}, - outputs=[gen_name(75.1)]) - pattern_loop_block.add_layer( - "prim.ne", - inputs={"x": gen_name(75.1), - "y": gen_name(74.1)}, - outputs=[gen_name(76)]) - pattern_loop_block.add_layer( - "prim.if", - inputs={"input": gen_name(76)}, - outputs=[gen_name(77), gen_name(78)]) - if_layer13 = pattern_loop_block.layers[list( - pattern_loop_block.layers.keys())[-1]] - pattern_loop_block_block = PaddleGraph( - if_layer13, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(77)], input=False) - pattern_loop_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(76)}, - outputs=[gen_name(78)]) - if_layer13.add_block(pattern_loop_block_block) - pattern_loop_block_block = PaddleGraph( - if_layer13, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(77)], input=None) - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(78)], input=None) - if_layer13.add_block(pattern_loop_block_block) - if_layer13.inputs.update({"input-0": gen_name(76), }) - pattern_loop_block.add_layer( - "prim.if", - inputs={"input": gen_name(76)}, - outputs=[gen_name(79), gen_name(80)]) - if_layer14 = pattern_loop_block.layers[list( - pattern_loop_block.layers.keys())[-1]] - pattern_loop_block_block = PaddleGraph( - if_layer14, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(77)}, - outputs=[gen_name(79)]) - pattern_loop_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(78)}, - outputs=[gen_name(80)]) - if_layer14.add_block(pattern_loop_block_block) - pattern_loop_block_block = PaddleGraph( - if_layer14, graph_type="dygraph") - pattern_loop_block_block.add_layer( - "prim.equal", inputs={}, outputs=[gen_name(79)], input=True) - pattern_loop_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(76)}, - outputs=[gen_name(80)]) - if_layer14.add_block(pattern_loop_block_block) - if_layer14.inputs.update({ - "input-0": gen_name(77), - "input-1": gen_name(78), - "input-2": gen_name(76) - }) - pattern_loop_block.add_layer( - "prim.add", - inputs={"x": gen_name(72)}, - outputs=[gen_name(81)], - y=1) - pattern_loop_block.add_layer( - "prim.lt", - inputs={"x": gen_name(81), - "y": gen_name(70)}, - outputs=[gen_name(82)]) - pattern_loop_block.add_layer( - "prim.and", - inputs={"x": gen_name(82), - "y": gen_name(79)}, - outputs=[gen_name(83)]) - pattern_loop_block.add_layer( - "prim.equal", - inputs={"input": gen_name(80)}, - outputs=[gen_name(74)]) - pattern_loop_block.add_layer( - "prim.equal", - inputs={"input": gen_name(81)}, - outputs=[gen_name(75)]) - loop_layer.add_block(pattern_loop_block) - loop_layer.inputs.update({ - "input-0": gen_name(65), - "input-1": gen_name(72), - "input-2": gen_name(72), - "input-3": gen_name(70) - }) - pattern_block_block_block_block.add_layer( - "prim.if", - inputs={"input": gen_name(74)}, - outputs=[gen_name(84)]) - if_layer15 = pattern_block_block_block_block.layers[list( - pattern_block_block_block_block.layers.keys())[-1]] - pattern_block_block_block_block_block = PaddleGraph( - if_layer15, graph_type="dygraph") - pattern_block_block_block_block_block.add_layer( - "prim.warnings", - inputs={}, - outputs=[gen_name(85)], - stacklevel=2, - input="...") - if_layer15.add_block(pattern_block_block_block_block_block) - pattern_block_block_block_block_block = PaddleGraph( - if_layer15, graph_type="dygraph") - if_layer15.add_block(pattern_block_block_block_block_block) - if_layer12.add_block(pattern_block_block_block_block) - pattern_block_block_block_block = PaddleGraph( - if_layer12, graph_type="dygraph") - if_layer12.add_block(pattern_block_block_block_block) - if_layer12.inputs.update({ - "input-0": gen_name(65), - "input-1": gen_name(65), - }) - pattern_block_block_block.add_layer( - "prim.list", inputs={}, outputs=[gen_name(86)]) - pattern_block_block_block.add_layer( - "prim.loop", - inputs={}, - outputs=[gen_name(87), gen_name(88)], - input=2) - loop_layer = pattern_block_block_block.layers[list( - pattern_block_block_block.layers.keys())[-1]] - pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph") - pattern_loop_block.add_layer( - "prim.add", - inputs={"x": gen_name(88)}, - outputs=[gen_name(89)], - y=2) - pattern_loop_block.add_layer( - "prim.shape_dim", - inputs={"input": gen_name(34), - "dim": gen_name(89)}, - outputs=[gen_name(90)]) - pattern_loop_block.add_layer( - "prim.float", - inputs={"input": gen_name(90)}, - outputs=[gen_name(91)]) - pattern_loop_block.add_layer( - "prim.getitem", - inputs={"list": gen_name(65), - "element": gen_name(88)}, - outputs=[gen_name(92)]) - pattern_loop_block.add_layer( - "prim.mul", - inputs={"x": gen_name(91), - "y": gen_name(92)}, - outputs=[gen_name(93)]) - pattern_loop_block.add_layer( - "prim.floor", - inputs={"input": gen_name(93)}, - outputs=[gen_name(94)]) - pattern_loop_block.add_layer( - "prim.append", - inputs={"list": gen_name(86), - "element": gen_name(94)}, - outputs=[]) - loop_layer.add_block(pattern_loop_block) - loop_layer.inputs.update({ - "input-0": gen_name(34), - "input-1": gen_name(65), - "input-2": gen_name(86) - }) - pattern_block_block_block.add_layer( - "prim.equal", - inputs={"input": gen_name(86)}, - outputs=[gen_name(63)]) - if_layer10.add_block(pattern_block_block_block) - if_layer10.inputs.update({ - "input-0": gen_name(61), - "input-1": gen_name(54), - "input-2": gen_name(54), - "input-3": gen_name(37), - "input-4": gen_name(34) - }) + pattern_block_block = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph") pattern_block_block.add_layer( "prim.getitem", inputs={"list": gen_name(11)}, - outputs=[gen_name(95)], + outputs=[gen_name(21)], element=0) pattern_block_block.add_layer( "prim.getitem", inputs={"list": gen_name(11)}, - outputs=[gen_name(96)], + outputs=[gen_name(22)], element=1) pattern_block_block.add_layer( "prim.isinstance", - inputs={"input": gen_name(63)}, + inputs={"input": "interpolate-input-3"}, outputs=["interpolate-input-0_isinstance"], cls="paddle.fluid.Variable") pattern_block_block.add_layer( @@ -1420,133 +180,127 @@ class DygraphInterpolateBilinearFuser(FuseBase): if_layer_isinstance, graph_type="dygraph") pattern_block_block_block.add_layer( "prim.var2list", - inputs={"input": gen_name(63)}, - outputs=[gen_name(63)]) + inputs={"input": "interpolate-input-3"}, + outputs=["interpolate-input-3"]) if_layer_isinstance.add_block(pattern_block_block_block) pattern_block_block_block = PaddleGraph( if_layer_isinstance, graph_type="dygraph") if_layer_isinstance.add_block(pattern_block_block_block) - if_layer_isinstance.inputs["input-0"] = gen_name(63) - pattern_block_block.add_layer( - "prim.assert", - inputs={"key": gen_name(95), - "value": gen_name(96)}, - outputs=[gen_name(97) + "_assert"], - type="eq") + if_layer_isinstance.inputs["input-0"] = "interpolate-input-3" pattern_block_block.add_layer( "paddle.nn.functional.interpolate", inputs={ "input": "interpolate-input-0", - "size": gen_name(63), - "scale_factor": gen_name(95) + "size": "interpolate-input-3", + "scale_factor": gen_name(21) }, - outputs=[gen_name(97)], - align_corners=False, - align_mode=0) + outputs=[gen_name(23)]) pattern_block_block.add_layer( "prim.equal", - inputs={"input": gen_name(97)}, + inputs={"input": gen_name(23)}, outputs=[gen_name(20)]) if_layer2.add_block(pattern_block_block) pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph") pattern_block_block.add_layer( - "fluid.layers.shape", + "prim.shape", inputs={"input": "interpolate-input-0"}, - outputs=[gen_name(98)]) + outputs=[gen_name(24)]) pattern_block_block.add_layer( "prim.len", - inputs={"input": gen_name(98)}, - outputs=[gen_name(98)]) + inputs={"input": gen_name(24)}, + outputs=[gen_name(24)]) pattern_block_block.add_layer( "prim.eq", - inputs={"x": gen_name(98)}, - outputs=[gen_name(99)], + inputs={"x": gen_name(24)}, + outputs=[gen_name(25)], y=5) pattern_block_block.add_layer( "prim.if", - inputs={"input": gen_name(99)}, - outputs=[gen_name(100)]) - if_layer16 = pattern_block_block.layers[list( + inputs={"input": gen_name(25)}, + outputs=[gen_name(26)]) + if_layer3 = pattern_block_block.layers[list( pattern_block_block.layers.keys())[-1]] pattern_block_block_block = PaddleGraph( - if_layer16, graph_type="dygraph") + parent_layer=if_layer3, graph_type="dygraph") pattern_block_block_block.add_layer( "prim.exception", inputs={}, - outputs=[gen_name(101)], + outputs=[gen_name(27)], input="Exception") - if_layer16.add_block(pattern_block_block_block) + if_layer3.add_block(pattern_block_block_block) pattern_block_block_block = PaddleGraph( - if_layer16, graph_type="dygraph") + parent_layer=if_layer3, graph_type="dygraph") pattern_block_block_block.add_layer( "prim.exception", inputs={}, - outputs=[gen_name(102)], + outputs=[gen_name(28)], input="Exception") - if_layer16.add_block(pattern_block_block_block) + if_layer3.add_block(pattern_block_block_block) pattern_block_block.add_layer( "prim.equal", inputs={}, outputs=[gen_name(20)], input=None) if_layer2.add_block(pattern_block_block) if_layer2.inputs.update({ - "input-0": gen_name(13), - "input-1": gen_name(13), - "input-2": "interpolate-input-0", + "input-0": "interpolate-input-0", + "input-1": "interpolate-input-3", + "input-2": "interpolate-input-3", "input-3": gen_name(11), "input-5": gen_name(11), }) pattern_block.add_layer( "prim.equal", inputs={"input": gen_name(20)}, - outputs=[gen_name(16)]) + outputs=[gen_name(14)]) if_layer1.add_block(pattern_block) if_layer1.inputs.update({ - "input-2": "interpolate-input-0", - "input-4": gen_name(13), - "input-7": gen_name(11), - "input-9": gen_name(11), - "input-11": "interpolate-input-0", - "input-12": "interpolate-input-0", + 'input-2': 'interpolate-input-0', + 'input-4': gen_name(11), + 'input-6': gen_name(11), + 'input-8': 'interpolate-input-0', + 'input-9': 'interpolate-input-3', + 'input-10': 'interpolate-input-0' }) self.pattern.build(inputs={ "input-0": "interpolate-input-0", - "input-1": "interpolate-input-1" + "input-1": "interpolate-input-1", + "input-2": "interpolate-input-2", + "input-3": "interpolate-input-3", + "input-4": "interpolate-input-4" }) + + + def insert_new_layer(self, graph, parameters, matches): - new_layers = self.gen_new_layer(parameters, matches) - new_layer_id = list(matches.keys())[0] - graph.layers[new_layer_id] = new_layers[0] - matches.pop(new_layer_id) - new_layer_id = list(matches.keys())[0] - graph.layers[new_layer_id] = new_layers[1] - block_layer = new_layers[1].blocks[0].layers.pop( - list(new_layers[1].blocks[0].layers.keys())[-1]) - new_layers[1].blocks[0].layers[new_layer_id + ".0.0"] = block_layer - matches.pop(new_layer_id) - new_layer_id = list(matches.keys())[0] - graph.layers[new_layer_id] = new_layers[2] - matches.pop(new_layer_id) + new_layer = self.gen_new_layer(parameters, matches) + global_layers = graph.get_global_layers() + new_matches = dict() + is_match = False + for layer_id, layer in global_layers.items(): + if layer_id == list(matches.keys())[0] and not is_match: + new_matches[layer_id] = layer + is_match = True + if is_match: + new_matches[layer_id] = layer + if layer_id == list(matches.keys())[-1]: + break + new_layer_id = new_layer.layer_id + graph.layers[new_layer_id] = new_layer + new_matches.pop(new_layer_id) + matches.clear() + for layer_id, layer in new_matches.items(): + matches[layer_id] = layer + def gen_new_layer(self, parameters, matches): layers = list() layers_id = list(matches.keys()) layer = matches[layers_id[6]] size = layer.inputs["input1"] - layer = matches[layers_id[92]] - layer.inputs["input"] = size - layers.append(layer) - layer = matches[layers_id[93]] - block_layer = layer.blocks[0].layers[list(layer.blocks[0].layers.keys()) - [0]] - block_layer.inputs["input"] = size - block_layer.outputs[0] = size - layer.inputs["input-0"] = size - layers.append(layer) - layer = matches[layers_id[-1]] - outputs = layer.outputs - layer = matches[layers_id[96]] - layer.inputs.pop("scale_factor") - layer.inputs["size"] = size - layer.outputs = outputs - layers.append(layer) - return layers + layer = matches[layers_id[19]] + new_layer = copy.deepcopy(layer) + layer = matches[layers_id[9]] + new_layer.outputs[0] = layer.outputs[0] + new_layer.layer_id = layers_id[7] + new_layer.inputs.pop("scale_factor") + new_layer.inputs["size"] = size + return new_layer diff --git a/x2paddle/optimizer/optimizer.py b/x2paddle/optimizer/optimizer.py index 6bbe11f..663d0e2 100644 --- a/x2paddle/optimizer/optimizer.py +++ b/x2paddle/optimizer/optimizer.py @@ -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": diff --git a/x2paddle/optimizer/pattern_matcher.py b/x2paddle/optimizer/pattern_matcher.py index 826c900..4561389 100644 --- a/x2paddle/optimizer/pattern_matcher.py +++ b/x2paddle/optimizer/pattern_matcher.py @@ -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): -- GitLab