test_generator.py 6.2 KB
Newer Older
G
add  
gongweibao 已提交
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
# Copyright (c) 2018 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.

from __future__ import print_function
import re
import functools
import warnings
import string

from six.moves import cStringIO
from paddle.fluid.proto import framework_pb2
from paddle.fluid.framework import OpProtoHolder, Variable
from paddle.fluid.layer_helper import LayerHelper

G
fix  
gongweibao 已提交
26
g_filer_attrs = ['op_role', 'op_role_var', 'op_namescope']
G
add  
gongweibao 已提交
27

G
add  
gongweibao 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

def _convert_(name):
    """
    Formatting.

    Args:
       name: The name/alias

    This function takes in a name and converts it to a standard format of
    group1_group2. Where as per the regular expression, group1 can have
    alphabets and numbers and group2 has capital alphabets.

    """
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()


def _get_inputs(op_type):
    op_proto = OpProtoHolder.instance().get_op_proto(op_type)
    inputs = dict()
    for ipt in op_proto.inputs:
G
fix  
gongweibao 已提交
49
        inputs[ipt.name] = ipt.comment
G
add  
gongweibao 已提交
50

G
add  
gongweibao 已提交
51 52
    return inputs

G
add  
gongweibao 已提交
53 54 55 56 57 58 59

def _get_outputs(op_type):
    op_proto = OpProtoHolder.instance().get_op_proto(op_type)
    outputs = {}
    for ipt in op_proto.outputs:
        outputs[ipt.name] = ""

G
add  
gongweibao 已提交
60 61 62
    return outputs


G
fix  
gongweibao 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
_two_dollar_pattern_ = re.compile(r"\$\$([^\$]+)\$\$")
_single_dollar_pattern_ = re.compile(r"\$([^\$]+)\$")
_two_bang_pattern_ = re.compile(r"!!([^!]+)!!")


def escape_math(text):
    return _two_bang_pattern_.sub(
        r'$$\1$$',
        _single_dollar_pattern_.sub(r':math:`\1`',
                                    _two_dollar_pattern_.sub(r"!!\1!!", text)))


def get_comment(op_type):
    op_proto = OpProtoHolder.instance().get_op_proto(op_type)

    comment_lines = op_proto.comment.split("\n")
    comment = ""
    for line in comment_lines:
        line = line.strip()
        if len(line) != 0:
            comment += escape_math(line)
            comment += " "
        elif len(comment) != 0:
            comment += "\n    "

    return comment


G
add  
gongweibao 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
def _get_attrs(op_type):
    op_proto = OpProtoHolder.instance().get_op_proto(op_type)
    return op_proto.attrs


def get_indent_space(indent, space_num=4):
    ret = ""
    for i in range(0, indent * space_num):
        ret += " "

    return ret


def get_input_comments(op_type, indent=2):
    ret = ""
    inputs = _get_inputs(op_type)
    for t in inputs:
G
fix  
gongweibao 已提交
108 109
        ret += get_indent_space(2) + "%s (Type): %s\n" % (_convert_(t),
                                                          inputs[t])
G
add  
gongweibao 已提交
110

G
add  
gongweibao 已提交
111 112 113
    for t in _get_attrs(op_type):
        if t.name in g_filer_attrs:
            continue
G
fix  
gongweibao 已提交
114 115
        ret += get_indent_space(2) + "%s (%s): %s\n" % (
            _convert_(t.name), t.type, _convert_(t.comment))
G
add  
gongweibao 已提交
116

G
add  
gongweibao 已提交
117
    return ret
G
add  
gongweibao 已提交
118

G
add  
gongweibao 已提交
119 120 121 122 123 124 125

def get_output_comments(op_type, indent=2):
    ret = ""
    for t in _get_outputs(op_type):
        ret += get_indent_space(2) + "output(${%s_type}): ${%s_comment}\n" % (
            _convert_(t), _convert_(t))
    return ret
G
add  
gongweibao 已提交
126 127 128


def get_func_args(op_type):
G
add  
gongweibao 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    ret = ""
    inputs = _get_inputs(op_type)
    for t in inputs:
        ret += "%s," % _convert_(t)

    for t in _get_attrs(op_type):
        if t.name in g_filer_attrs:
            continue

        default = re.findall("\(.+\, default (.+)\(?\)", t.comment)
        if len(default) > 0:
            #print(default[0])
            ret += "{}={},".format(_convert_(t.name), default[0])
            continue

        ret += "%s=," % _convert_(t.name)

    return ret.strip(',')
G
add  
gongweibao 已提交
147 148 149


def get_inputs(op_type):
G
add  
gongweibao 已提交
150 151 152
    ret = "inputs={"
    inputs = _get_inputs(op_type)
    for t in inputs:
G
fix  
gongweibao 已提交
153
        ret += "'{}': {},".format(t, _convert_(t))
G
add  
gongweibao 已提交
154 155 156 157 158 159 160 161 162
    ret = ret.strip(",")
    ret += "}"

    if ret == "inputs={}":
        return ""

    return ret


G
add  
gongweibao 已提交
163
def get_outputs(op_type):
G
add  
gongweibao 已提交
164 165 166
    ret = "outputs={"
    inputs = _get_outputs(op_type)
    for t in inputs:
G
fix  
gongweibao 已提交
167
        ret += "'{}': {},".format(t, _convert_(t))
G
add  
gongweibao 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    ret = ret.strip(",")
    ret += "}"

    if ret == "inputs={}":
        return ""

    return ret


def get_attrs(op_type):
    ret = "attrs={"
    for t in _get_attrs(op_type):
        if t.name in g_filer_attrs:
            continue

G
fix  
gongweibao 已提交
183
        ret += "'%s': %s," % (t.name, _convert_(t.name))
G
add  
gongweibao 已提交
184 185 186 187 188 189 190 191

    ret = ret.strip(",")
    ret += "}"

    return ret


def get_outvars(op_type, indent=1):
G
fix  
gongweibao 已提交
192
    inputs = _get_inputs(op_type)
G
fix  
gongweibao 已提交
193 194
    if len(inputs) == 0:
        return ""
G
add  
gongweibao 已提交
195 196 197 198
    ret = ""
    for t in _get_outputs(op_type):
        ret += get_indent_space(
            indent
G
fix  
gongweibao 已提交
199 200
        ) + "%s = helper.create_tmp_variable(dtype=helper.input_dtype('%s'))\n" % (
            (_convert_(t), list(inputs)[0]))
G
add  
gongweibao 已提交
201 202
    ret = ret.strip('\n')
    return ret
G
add  
gongweibao 已提交
203 204 205 206 207 208 209 210


def get_op_py(op_type):
    input_comments = get_input_comments(op_type)
    output_comments = get_output_comments(op_type)
    args = get_func_args(op_type)
    inputs = get_inputs(op_type)
    outputs = get_outputs(op_type)
G
add  
gongweibao 已提交
211 212
    attrs = get_attrs(op_type)
    out_vars = get_outvars(op_type)
G
fix  
gongweibao 已提交
213
    comment = get_comment(op_type)
G
add  
gongweibao 已提交
214 215 216 217

    code = """
def {op_type}({args}):
    \"\"\"
G
add  
gongweibao 已提交
218
    {comment}
G
add  
gongweibao 已提交
219
    Args:
G
add  
gongweibao 已提交
220
{input_comments}
G
fix  
gongweibao 已提交
221

G
add  
gongweibao 已提交
222
    Returns:
G
add  
gongweibao 已提交
223
{output_comments}
G
add  
gongweibao 已提交
224
    \"\"\"
G
add  
gongweibao 已提交
225 226 227
    
    helper = LayerHelper('{op_type}', **locals())
{generated_outvar}
G
add  
gongweibao 已提交
228 229 230
    helper.append_op(
        type='{op_type}',
        {inputs},
G
add  
gongweibao 已提交
231 232 233 234
        {outputs},
        {attrs})    
    
    return out
G
add  
gongweibao 已提交
235
""".format(
G
fix  
gongweibao 已提交
236
        comment=comment,
G
add  
gongweibao 已提交
237
        input_comments=input_comments.strip('\n'),
G
add  
gongweibao 已提交
238 239
        output_comments=output_comments,
        args=args,
G
add  
gongweibao 已提交
240
        generated_outvar=out_vars,
G
add  
gongweibao 已提交
241 242
        op_type=op_type,
        inputs=inputs,
G
add  
gongweibao 已提交
243 244
        outputs=outputs,
        attrs=attrs)
G
add  
gongweibao 已提交
245 246 247 248

    return code


G
fix  
gongweibao 已提交
249
#print(get_op_py("uniform_random_batch_size_like"))
G
add  
gongweibao 已提交
250
#print(get_op_py("gaussian_random"))
G
fix  
gongweibao 已提交
251 252 253 254 255
print(get_op_py("sampling_id"))
print(get_op_py("gaussian_random_batch_size_like"))
print(get_op_py("sum"))
print(get_op_py("slice"))
print(get_op_py("shape"))