convert_prim.py 7.4 KB
Newer Older
S
SunAhong1993 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 64
#   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.


def convert_prim(layer, indent=1, init_func=[], forward_func=[]):
    def gen_codes(code_list, indent=0):
        indent_blank = "    " * indent
        codes = []
        for code_line in code_list:
            if code_line.strip() == "":
                codes.append('\n')
            else:
                codes.append(indent_blank + code_line + '\n')
        return codes

    if layer.kernel == "prim.if":
        line = "if {} :".format(list(layer.inputs.values())[0])
        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)
        block = layer.blocks[1]
        if len(block.layers) > 0:
            line = "else:"
            forward_func.extend(gen_codes([line], indent=indent))
            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)
        return
    elif layer.kernel == "prim.loop":
        loop_range = list(layer.inputs.values())[0]
        if list(layer.inputs.values())[0] is None:
            loop_range = str(layer.attrs[list(layer.inputs.keys())[0]])
        line = "for {} in range({}):".format(layer.outputs[1], loop_range)
        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)
        return
    elif layer.kernel == "prim.equal":
        line = "{} = {}".format(layer.outputs[0],
                                list(layer.inputs.values())[0])
    elif layer.kernel == "prim.constant":
        line = "{} = {}".format(layer.outputs[0], layer.attrs["value"])
    elif layer.kernel == "prim.list":
        inputs_list = list(layer.inputs.values())
        for i, input in enumerate(inputs_list):
            if input is None:
S
SunAhong1993 已提交
65
                inputs_list[i] = str(layer.attrs[list(layer.inputs.keys())[i]])
S
SunAhong1993 已提交
66 67 68 69 70 71 72 73 74 75
        inputs_str = ', '.join(inputs_list)
        line = "{} = [{}]".format(layer.outputs[0], inputs_str)
    elif layer.kernel == "prim.exception":
        exception = list(layer.inputs.values())[0]
        if list(layer.inputs.values())[0] is None:
            exception = str(layer.attrs[list(layer.inputs.keys())[0]])
        line = "raise RaiseException({})".format(exception)
    elif layer.kernel == "prim.min":
        line = "{} = min({})".format(layer.outputs[0],
                                     list(layer.inputs.values())[0])
S
SunAhong1993 已提交
76
    elif layer.kernel == "prim.add_":
S
SunAhong1993 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        line = "{} = {} + {} * {}".format(layer.outputs[0],
                                          list(layer.inputs.values())[0],
                                          layer.attrs["alpha"],
                                          list(layer.inputs.values())[1])
    elif layer.kernel == "prim.append":
        line = "{} = {}.append({})".format(layer.outputs[0],
                                           list(layer.inputs.values())[0],
                                           list(layer.inputs.values())[1])
    elif layer.kernel == "prim.shape":
        line = "{} = {}.shape".format(layer.outputs[0],
                                      list(layer.inputs.values())[0])
    elif layer.kernel == "prim.len":
        line = "{} = len({})".format(layer.outputs[0],
                                     list(layer.inputs.values())[0])
    elif layer.kernel == "prim.eq":
        line = "{} = {} == {}".format(layer.outputs[0],
                                      list(layer.inputs.values())[0],
                                      list(layer.inputs.values())[1])
    elif layer.kernel == "prim.assert":
        if layer.attrs["type"] == "eq":
            if isinstance(layer.attrs["value"], list):
                s = ""
                for v in layer.attrs["value"]:
                    s += "{} == {} or ".format(layer.attrs["key"], v)
                if len(s) > 0:
                    s = s[:-4]
                line = "assert {}, \'The {} must be {}!\'".format(
                    s, layer.attrs["key"], layer.attrs["value"])
            else:
                line = "assert {} == {}, \'The {} must be {}!\'".format(
                    layer.attrs["key"], layer.attrs["value"],
                    layer.attrs["key"], layer.attrs["value"])
        else:
            raise Exception("Not implement yet!")
    elif layer.kernel == "prim.getitem":
        item0 = list(layer.inputs.values())[0]
        if list(layer.inputs.values())[0] is None:
            item0 = str(layer.attrs[list(layer.inputs.keys())[0]])
        item1 = list(layer.inputs.values())[1]
        if list(layer.inputs.values())[1] is None:
            item1 = str(layer.attrs[list(layer.inputs.keys())[1]])
        line = "{} = {}[{}]".format(layer.outputs[0], item0, item1)
    elif layer.kernel == "prim.le":
        item0 = list(layer.inputs.values())[0]
        if list(layer.inputs.values())[0] is None:
            item0 = str(layer.attrs[list(layer.inputs.keys())[0]])
        item1 = list(layer.inputs.values())[1]
        if list(layer.inputs.values())[1] is None:
            item1 = str(layer.attrs[list(layer.inputs.keys())[1]])
        line = "{} = {} < {}".format(layer.outputs[0], item0, item1)
S
SunAhong1993 已提交
127 128 129 130
    elif layer.kernel == "prim.ne":
        item0 = list(layer.inputs.values())[0]
        item1 = list(layer.inputs.values())[1]
        line = "{} = {} < {}".format(layer.outputs[0], item0, item1)
S
SunAhong1993 已提交
131
    elif layer.kernel == "prim.slice":
S
SunAhong1993 已提交
132 133 134 135
        inputs_str = ""
        for v in list(layer.inputs.values())[1:]:
            inputs_str += "{}:".format(v)
        inputs_str = inputs_str[:-1]
S
SunAhong1993 已提交
136
        line = "{} = {}[{}]".format(layer.outputs[0],
S
SunAhong1993 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                                    list(layer.inputs.values())[0], inputs_str)
    elif layer.kernel == "prim.add":
        line = "{} = {} + {}".format(layer.outputs[0],
                                     list(layer.inputs.values())[0],
                                     list(layer.inputs.values())[1])
    elif layer.kernel == "prim.sub":
        line = "{} = {} - {}".format(layer.outputs[0],
                                     list(layer.inputs.values())[0],
                                     list(layer.inputs.values())[1])
    elif layer.kernel == "prim.mul":
        line = "{} = {} * {}".format(layer.outputs[0],
                                     list(layer.inputs.values())[0],
                                     list(layer.inputs.values())[1])
    elif layer.kernel == "prim.neg":
        line = "{} = -{}".format(layer.outputs[0],
                                 list(layer.inputs.values())[0])
    else:
        print(layer.kernel)
        line = ""
S
SunAhong1993 已提交
156
    forward_func.extend(gen_codes([line], indent=indent))