fluid_code.py 3.4 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from x2paddle.core.graph import GraphNode

J
jiangjiajun 已提交
17 18 19 20 21

class Layer(object):
    def __init__(self):
        self.op = None
        self.param_attr = dict()
J
jiangjiajun 已提交
22
        self.inputs = dict()
J
jiangjiajun 已提交
23 24 25
        self.output = None

    def get_code(self):
J
jiangjiajun 已提交
26 27
        layer_code = ""
        if self.output is not None:
28 29 30 31 32
            if isinstance(self.output, str):
                layer_code = self.output + " = "
            else:
                layer_code = self.output.layer_name + " = "

J
jiangjiajun 已提交
33 34
        layer_code = layer_code + "fluid.layers." + self.op + "("

J
jiangjiajun 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        if isinstance(self.inputs, list):
            in_list = "["
            for input in self.inputs:
                assert isinstance(
                    input, GraphNode), "Type of input should be GraphNode"
                if hasattr(input, "index"):
                    in_list += (input.layer_name + "[{}]".format(input.index) +
                                ", ")
                else:
                    in_list += (input.layer_name + ", ")
            inlist = in_list.strip(", ") + "], "
        elif isinstance(self.inputs, dict):
            for key, input in self.inputs.items():
                assert isinstance(
                    input, GraphNode), "Type of input should be GraphNode"
                if hasattr(input, "index"):
                    layer_code = layer_code + key + "={}, ".format(
                        input.layer_name + "[{}]".format(input.index))
                else:
                    layer_code = layer_code + key + "={}, ".format(
                        input.layer_name)
        elif isinstance(self.inputs, GraphNode):
            if hasattr(self.inputs, "index"):
                layer_code += (self.inputs.layer_name +
                               "[{}]".format(self.inputs.index) + ", ")
            else:
                layer_code += (self.inputs.layer_name + ", ")
        else:
            raise Exception("Unknown type of inputs.")
J
jiangjiajun 已提交
64 65

        for key, value in self.param_attr.items():
66
            layer_code = layer_code + key + "={}, ".format(value)
J
jiangjiajun 已提交
67
        layer_code = layer_code.strip(", ")
J
jiangjiajun 已提交
68

69 70
        return layer_code + ")"

J
jiangjiajun 已提交
71 72 73

class FluidCode(object):
    def __init__(self):
J
jiangjiajun 已提交
74
        self.layers = list()
J
jiangjiajun 已提交
75

J
jiangjiajun 已提交
76 77 78
    def add_layer(self, op, inputs, output, param_attr=None):
        layer = Layer()
        layer.op = op
79 80
        if inputs is not None:
            layer.inputs = inputs
J
jiangjiajun 已提交
81 82 83 84 85 86 87 88 89
        layer.output = output
        if param_attr is not None:
            layer.param_attr = param_attr
        self.layers.append(layer)

    def add_note(self, note):
        # note should be string
        self.layers.append(note)

J
jiangjiajun 已提交
90 91 92
    def clear(self):
        self.layers = list()

J
jiangjiajun 已提交
93 94 95 96 97 98 99
    def gen_codes(self):
        codes = list()
        for layer in self.layers:
            if isinstance(layer, Layer):
                codes.append(layer.get_code())
            elif isinstance(layer, str):
                codes.append(layer)