get_compat_kernel_signature.py 3.3 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
# 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 os

17
skip_list = ["adam_sig.cc", "adamw_sig.cc"]
H
huzhiqiang 已提交
18

19

20 21 22 23 24 25 26
def is_grad_kernel(kernel_info):
    kernel_name = kernel_info.split(",")[0]
    if kernel_name.endswith("_grad"):
        return True
    return False


27 28 29 30 31 32 33 34 35 36 37 38
def parse_compat_registry(kernel_info):
    name, inputs_str, attrs_str, outputs_str = kernel_info.split(",{")
    kernel_info = {}
    kernel_info["inputs"] = inputs_str[:-1].split(",")
    kernel_info["attrs"] = attrs_str[:-1].split(",")
    kernel_info["outputs"] = outputs_str[:-1].split(",")
    return name, kernel_info


def remove_grad_registry(kernels_registry):
    clean_kernel_registry = {}
    for registry in kernels_registry:
39
        if "_grad" not in registry:
40 41 42 43 44 45 46 47
            clean_kernel_registry[registry] = kernels_registry[registry]
    return clean_kernel_registry


def get_compat_kernels_info():
    kernels_info = {}
    compat_files = os.listdir("../../paddle/phi/ops/compat")
    for file_ in compat_files:
48
        if ".cc" not in file_:
49 50 51
            compat_files.remove(file_)

    for file_ in compat_files:
H
huzhiqiang 已提交
52 53
        if file_ in skip_list:
            continue
54 55 56 57 58 59 60 61 62 63 64 65
        with open("../../paddle/phi/ops/compat/" + file_) as in_file:
            txt = in_file.readlines()
            content = ""
            registry = False
            for line in txt:
                if ("KernelSignature(" in line):
                    content = ""
                    registry = True
                if (registry):
                    content += line
                if (registry and ";" in line):
                    data = content.replace("\n", "").replace(
66 67 68
                        " ",
                        "").strip("return").strip("KernelSignature(").strip(
                            "\);").replace("\"", "").replace("\\", "")
69
                    registry = False
70 71
                    if is_grad_kernel(data):
                        continue
72 73 74 75 76
                    name, registry_info = parse_compat_registry(data)

                    if name in kernels_info:
                        cur_reg = kernels_info[name]
                        kernels_info[name]["inputs"] = list(
77 78
                            set(registry_info["inputs"] +
                                kernels_info[name]["inputs"]))
79
                        kernels_info[name]["attrs"] = list(
80 81
                            set(registry_info["attrs"] +
                                kernels_info[name]["attrs"]))
82
                        kernels_info[name]["outputs"] = list(
83 84
                            set(registry_info["outputs"] +
                                kernels_info[name]["outputs"]))
85 86 87 88 89
                    else:
                        kernels_info[name] = registry_info

    compat_registry_ = remove_grad_registry(kernels_info)
    return compat_registry_