generate_op.py 13.9 KB
Newer Older
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16 17 18 19 20 21
# 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.

import argparse
import os
from pathlib import Path

import yaml
from jinja2 import Environment, FileSystemLoader, StrictUndefined

22 23 24 25 26 27 28 29 30 31 32 33 34 35
from filters import (
    to_op_attr_type,
    to_opmaker_name,
    to_opmaker_name_cstr,
    to_pascal_case,
)
from tests import (
    is_base_api,
    is_vec,
    is_scalar,
    is_initializer_list,
    supports_inplace,
    supports_no_need_buffer,
)
36
from filters import to_input_name, cartesian_prod_mapping
37 38 39
from parse_utils import to_named_dict

file_loader = FileSystemLoader(Path(__file__).parent / "templates")
40 41 42 43 44 45 46 47
env = Environment(
    loader=file_loader,
    keep_trailing_newline=True,
    trim_blocks=True,
    lstrip_blocks=True,
    undefined=StrictUndefined,
    extensions=['jinja2.ext.do'],
)
48 49 50 51 52
env.filters["to_op_attr_type"] = to_op_attr_type
env.filters["to_opmaker_name"] = to_opmaker_name
env.filters["to_pascal_case"] = to_pascal_case
env.filters["to_input_name"] = to_input_name
env.filters["to_opmaker_name_cstr"] = to_opmaker_name_cstr
53
env.filters["cartesian_prod_mapping"] = cartesian_prod_mapping
54 55 56 57 58 59 60 61
env.tests["base_api"] = is_base_api
env.tests["vec"] = is_vec
env.tests["scalar"] = is_scalar
env.tests["initializer_list"] = is_initializer_list
env.tests["supports_inplace"] = supports_inplace
env.tests["supports_no_need_buffer"] = supports_no_need_buffer


62 63 64 65 66 67 68
def restruct_io(api):
    api["input_dict"] = to_named_dict(api["inputs"])
    api["attr_dict"] = to_named_dict(api["attrs"])
    api["output_dict"] = to_named_dict(api["outputs"])
    return api


69 70
# replace name of op and params for OpMaker
def replace_compat_name(api_op_map, forward_api_dict, backward_api_dict):
71 72 73 74 75 76 77
    def get_api_and_op_name(api_item):
        names = api_item.split('(')
        if len(names) == 1:
            return names[0].strip(), names[0].strip()
        else:
            return names[0].strip(), names[1].split(')')[0].strip()

78
    for api_args in api_op_map:
79
        api_name, op_name = get_api_and_op_name(api_args['op'])
80
        if api_name not in forward_api_dict:
81
            continue
82
        forward_api_item = forward_api_dict[api_name]
83 84 85
        has_backward = True if forward_api_item['backward'] else False
        if has_backward:
            backward_api_item = backward_api_dict[forward_api_item['backward']]
86 87 88 89
        if api_name != op_name:
            forward_api_item['op_name'] = op_name
        if 'backward' in api_args and has_backward:
            bw_api_name, bw_op_name = get_api_and_op_name(
90 91
                api_args['backward'].split(',')[0]
            )
92 93
            forward_api_item['backward'] = bw_op_name
            backward_api_item['op_name'] = bw_op_name
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        key_set = ['inputs', 'attrs', 'outputs']
        args_map = {}
        for key in key_set:
            if key in api_args:
                args_map.update(api_args[key])
                for args_item in forward_api_item[key]:
                    if args_item['name'] in api_args[key]:
                        args_item['name'] = api_args[key][args_item['name']]
                if has_backward:
                    for args_item in backward_api_item['forward'][key]:
                        if args_item['name'] in api_args[key]:
                            args_item['name'] = api_args[key][args_item['name']]
        forward_api_item['infer_meta']['param'] = [
            args_map[param] if param in args_map else param
            for param in forward_api_item['infer_meta']['param']
        ]
        forward_api_item['kernel']['param'] = [
            args_map[param] if param in args_map else param
            for param in forward_api_item['kernel']['param']
        ]
        if forward_api_item['kernel']['data_type']:
            forward_api_item['kernel']['data_type']['candidates'] = [
117 118 119 120
                args_map[param] if param in args_map else param
                for param in forward_api_item['kernel']['data_type'][
                    'candidates'
                ]
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            ]
        if forward_api_item['kernel']['backend']:
            forward_api_item['kernel']['backend']['candidates'] = [
                args_map[param] if param in args_map else param
                for param in forward_api_item['kernel']['backend']['candidates']
            ]
        if forward_api_item['kernel']['layout']:
            forward_api_item['kernel']['layout']['candidates'] = [
                args_map[param] if param in args_map else param
                for param in forward_api_item['kernel']['layout']['candidates']
            ]
        if forward_api_item['inplace']:
            inplace_map = {}
            for key, val in forward_api_item['inplace'].items():
                if key in args_map:
                    key = args_map[key]
                if val in args_map:
                    val = args_map[val]
                inplace_map[key] = val
            forward_api_item['inplace'] = inplace_map

        if has_backward:
            for args_item in backward_api_item['inputs']:
                if args_item['name'] in args_map:
                    args_item['name'] = args_map[args_item['name']]
146 147 148 149 150 151 152
                elif (
                    args_item['name'].endswith('_grad')
                    and args_item['name'][:-5] in args_map
                ):
                    args_map[args_item['name']] = (
                        args_map[args_item['name'][:-5]] + '_grad'
                    )
153 154 155 156 157
                    args_item['name'] = args_map[args_item['name']]
            for args_item in backward_api_item['attrs']:
                if args_item['name'] in args_map:
                    args_item['name'] = args_map[args_item['name']]
            for args_item in backward_api_item['outputs']:
158 159 160 161 162 163 164
                if (
                    args_item['name'].endswith('_grad')
                    and args_item['name'][:-5] in args_map
                ):
                    args_map[args_item['name']] = (
                        args_map[args_item['name'][:-5]] + '_grad'
                    )
165 166
                    args_item['name'] = args_map[args_item['name']]

167 168 169
            if 'invoke' in backward_api_item:
                backward_api_item['invoke']['args'] = [
                    args_map[param.strip()]
170 171
                    if param.strip() in args_map
                    else param.strip()
172 173 174 175
                    for param in backward_api_item['invoke']['args'].split(',')
                ]
                continue

176 177 178 179 180 181 182 183 184 185
            backward_api_item['infer_meta']['param'] = [
                args_map[param] if param in args_map else param
                for param in backward_api_item['infer_meta']['param']
            ]
            backward_api_item['kernel']['param'] = [
                args_map[param] if param in args_map else param
                for param in backward_api_item['kernel']['param']
            ]
            if backward_api_item['kernel']['data_type']:
                backward_api_item['kernel']['data_type']['candidates'] = [
186 187 188 189
                    args_map[param] if param in args_map else param
                    for param in backward_api_item['kernel']['data_type'][
                        'candidates'
                    ]
190 191 192
                ]
            if backward_api_item['kernel']['backend']:
                backward_api_item['kernel']['backend']['candidates'] = [
193 194 195 196
                    args_map[param] if param in args_map else param
                    for param in backward_api_item['kernel']['backend'][
                        'candidates'
                    ]
197 198 199
                ]
            if backward_api_item['kernel']['layout']:
                backward_api_item['kernel']['layout']['candidates'] = [
200 201 202 203
                    args_map[param] if param in args_map else param
                    for param in backward_api_item['kernel']['layout'][
                        'candidates'
                    ]
204 205 206 207 208 209
                ]
            if backward_api_item['no_need_buffer']:
                backward_api_item['no_need_buffer'] = [
                    args_map[param] if param in args_map else param
                    for param in backward_api_item['no_need_buffer']
                ]
210 211 212 213 214 215 216 217 218
            if backward_api_item['inplace']:
                inplace_map = {}
                for key, val in backward_api_item['inplace'].items():
                    if key in args_map:
                        key = args_map[key]
                    if val in args_map:
                        val = args_map[val]
                    inplace_map[key] = val
                backward_api_item['inplace'] = inplace_map
219

220

221 222
def process_invoke_op(forward_api_dict, backward_api_dict):
    for bw_api in backward_api_dict.values():
223 224 225 226
        if 'invoke' in bw_api:
            invoke_op = bw_api['invoke']['func']
            args_list = bw_api['invoke']['args']
            args_index = 0
227
            if invoke_op in forward_api_dict:
228 229 230 231 232
                reuse_op = forward_api_dict[invoke_op]
                bw_api['invoke']['inputs'] = []
                bw_api['invoke']['attrs'] = []
                bw_api['invoke']['outputs'] = []
                for input_item in reuse_op['inputs']:
233 234 235 236 237 238
                    bw_api['invoke']['inputs'].append(
                        {
                            'name': input_item['name'],
                            'value': args_list[args_index],
                        }
                    )
239 240 241
                    args_index = args_index + 1
                for attr in reuse_op['attrs']:
                    if args_index < len(args_list):
242 243 244 245 246 247 248 249
                        attr_value = (
                            f"this->GetAttr(\"{args_list[args_index]}\")"
                            if args_list[args_index] in bw_api['attr_dict']
                            else args_list[args_index]
                        )
                        bw_api['invoke']['attrs'].append(
                            {'name': attr['name'], 'value': attr_value}
                        )
250 251 252 253
                        args_index = args_index + 1
                    else:
                        break
                for idx, output_item in enumerate(reuse_op['outputs']):
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
                    bw_api['invoke']['outputs'].append(
                        {
                            'name': output_item['name'],
                            'value': bw_api['outputs'][idx]['name'],
                        }
                    )


def main(
    ops_yaml_path,
    backward_yaml_path,
    op_compat_yaml_path,
    op_version_yaml_path,
    output_op_path,
    output_arg_map_path,
):
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    with open(ops_yaml_path, "rt") as f:
        apis = yaml.safe_load(f)
        apis = [restruct_io(api) for api in apis]
    forward_api_dict = to_named_dict(apis)

    with open(backward_yaml_path, "rt") as f:
        backward_apis = yaml.safe_load(f)
        backward_apis = [restruct_io(api) for api in backward_apis]
    backward_api_dict = to_named_dict(backward_apis)

    with open(op_version_yaml_path, "rt") as f:
        api_versions = yaml.safe_load(f)
    # add api version info into api
    for api_version in api_versions:
        forward_api_dict[api_version['op']]['version'] = api_version['version']

    with open(op_compat_yaml_path, "rt") as f:
        api_op_map = yaml.safe_load(f)

    for api in apis:
        api['op_name'] = api['name']
    for bw_api in backward_apis:
        bw_api['op_name'] = bw_api['name']

    replace_compat_name(api_op_map, forward_api_dict, backward_api_dict)

    # prepare for invoke case
    process_invoke_op(forward_api_dict, backward_api_dict)

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    # fill backward field for an api if another api claims it as forward
    for name, backward_api in backward_api_dict.items():
        forward_name = backward_api["forward"]["name"]
        if forward_name in backward_api_dict:
            forward_api = backward_api_dict[forward_name]
            if forward_api["backward"] is None:
                forward_api["backward"] = name

    api_dict = {}
    api_dict.update(forward_api_dict)
    api_dict.update(backward_api_dict)

    if len(apis) == 0 and len(backward_apis) == 0:
        if os.path.isfile(output_op_path):
            os.remove(output_op_path)
        if os.path.isfile(output_arg_map_path):
            os.remove(output_arg_map_path)
        return

    op_template = env.get_template('op.c.j2')
    with open(output_op_path, "wt") as f:
320 321 322
        msg = op_template.render(
            apis=apis, backward_apis=backward_apis, api_dict=api_dict
        )
323 324 325 326 327 328 329 330 331 332
        f.write(msg)

    ks_template = env.get_template('ks.c.j2')
    with open(output_arg_map_path, 'wt') as f:
        msg = ks_template.render(apis=apis, backward_apis=backward_apis)
        f.write(msg)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        description="Generate operator file from api yaml."
    )
    parser.add_argument(
        '--ops_yaml_path', type=str, help="parsed ops yaml file."
    )
    parser.add_argument(
        '--backward_yaml_path', type=str, help="parsed backward ops yaml file."
    )
    parser.add_argument(
        '--op_compat_yaml_path', type=str, help="ops args compat yaml file."
    )
    parser.add_argument(
        '--op_version_yaml_path', type=str, help="ops version yaml file."
    )
    parser.add_argument(
        "--output_op_path", type=str, help="path to save generated operators."
    )
350 351 352
    parser.add_argument(
        "--output_arg_map_path",
        type=str,
353 354
        help="path to save generated argument mapping functions.",
    )
355 356

    args = parser.parse_args()
357 358 359 360 361 362 363 364
    main(
        args.ops_yaml_path,
        args.backward_yaml_path,
        args.op_compat_yaml_path,
        args.op_version_yaml_path,
        args.output_op_path,
        args.output_arg_map_path,
    )