parse_op.py 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2022 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.

import argparse

import yaml

19
from parse_utils import parse_op_entry
20 21


22 23 24 25 26
def main(op_yaml_path, output_path, backward):
    with open(op_yaml_path, "rt") as f:
        ops = yaml.safe_load(f)
        if ops is None:
            ops = []
27
        else:
28 29 30
            ops = [
                parse_op_entry(op, "backward_op" if backward else "op")
                for op in ops
31 32 33
            ]

    with open(output_path, "wt") as f:
34
        yaml.safe_dump(ops, f, default_flow_style=None, sort_keys=False)
35 36 37 38


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
39
        description="Parse op yaml into canonical format."
40
    )
41
    parser.add_argument('--op_yaml_path', type=str, help="op yaml file.")
42 43 44
    parser.add_argument(
        "--output_path", type=str, help="path to save parsed yaml file."
    )
45 46 47
    parser.add_argument("--backward", action="store_true", default=False)

    args = parser.parse_args()
48
    main(args.op_yaml_path, args.output_path, args.backward)