forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_kernel_info.py
180 lines (157 loc) · 6.5 KB
/
parse_kernel_info.py
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
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright (c) 2023 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 paddle
class KernelInfo:
def __init__(self, op_type):
self.op_type = op_type
self.supported_dtypes = set()
def parse_phi_dtypes(self, registered_info_list, device="GPU"):
assert isinstance(registered_info_list, list)
assert device in ["CPU", "GPU"]
# registered_info_list is in format as follows:
# ['(GPU, Undefined(AnyLayout), float32)', '(GPU, Undefined(AnyLayout), float64)']
for kernel_str in registered_info_list:
kernel_strs = (
kernel_str.replace("(", "").replace(")", "").split(",")
)
# for GPU, the kernel type can be GPUDNN.
if device in kernel_strs[0]:
self.supported_dtypes.add(kernel_strs[-1].replace(" ", ""))
# if len(self.supported_dtypes) == 0:
# print("-- [WARNING] No dtypes for op_type={}, device={}. Registered info: {}".format(self.op_type, device, registered_info_list))
def parse_fluid_dtypes(self, registered_info_list, device="gpu"):
assert isinstance(registered_info_list, list)
assert device in ["cpu", "gpu"]
# registered_info_list is in format as follows:
# ['{data_type[::paddle::platform::bfloat16]; data_layout[Undefined(AnyLayout)]; place[Place(gpu:0)]; library_type[PLAIN]}', ...}']
for kernel_str in registered_info_list:
kernel_strs = kernel_str.split(";")
if "place" in kernel_strs[2] and device in kernel_strs[2]:
assert "data_type" in kernel_strs[0]
dtype_str = kernel_strs[0].replace("{data_type[", "")
dtype_str = dtype_str.replace("::paddle::platform::", "")
dtype_str = dtype_str.replace("]", "")
self.supported_dtypes.add(dtype_str)
class KernelRegistryStatistics:
def __init__(self):
self.num_ops_for_dtypes = {
"all": 0,
"float32": 0,
"float16": 0,
"bfloat16": 0,
}
def update(self, supported_dtypes):
for dtype in supported_dtypes:
if dtype in self.num_ops_for_dtypes.keys():
self.num_ops_for_dtypes[dtype] += 1
elif dtype == "float":
self.num_ops_for_dtypes["float32"] += 1
self.num_ops_for_dtypes["all"] += 1
def __str__(self):
res = "{ "
num_floats = int(self.num_ops_for_dtypes["float32"])
for dtype, num in self.num_ops_for_dtypes.items():
res += f"{dtype}: {num:4d}"
if dtype in ["float16", "bfloat16"]:
if num_floats != 0:
percent = float(self.num_ops_for_dtypes[dtype]) / float(
num_floats
)
res += f"({percent * 100:.2f}%)"
else:
res += f"({0:.2f}%)"
res += " "
res += "}"
return res
def parse_paddle_kernels(lib="phi", kernel_type="function", print_detail=False):
assert lib in ["fluid", "phi"]
if lib == "phi":
assert kernel_type in ["function", "structure", "all"]
# phi kernel type can be: function, structure, all
kernel_infos = paddle.base.core._get_registered_phi_kernels(kernel_type)
else:
# fluid, phi, all
assert kernel_type in ["fluid", "phi", "all"]
kernel_infos = paddle.base.core._get_all_register_op_kernels(
kernel_type
)
max_op_type_lengths = 0
stats = KernelRegistryStatistics()
kernel_info_dict = {}
for key, value in kernel_infos.items():
info = KernelInfo(key)
if lib == "phi":
info.parse_phi_dtypes(value, device="GPU")
else:
info.parse_fluid_dtypes(value, device="gpu")
kernel_info_dict[key] = info
if len(info.op_type) > max_op_type_lengths:
max_op_type_lengths = len(info.op_type)
stats.update(info.supported_dtypes)
if print_detail:
print(
"==================== lib={}, kernel_type={} ====================".format(
lib, kernel_type
)
)
print(
"{} : {}".format(
"op_type".ljust(max_op_type_lengths + 4),
"supported_dtypes for GPU",
)
)
for key, value in sorted(kernel_info_dict.items()):
print(
"{} : {}".format(
value.op_type.ljust(max_op_type_lengths + 4),
value.supported_dtypes,
)
)
print("")
return stats
def main(lib):
assert lib in ["fluid", "phi"]
print_detail = False
if lib == "phi":
phi_function_kernels_stats = parse_paddle_kernels(
lib, "function", print_detail=False
)
phi_structure_kernels_stats = parse_paddle_kernels(
lib, "structure", print_detail=False
)
phi_all_kernels_stats = parse_paddle_kernels(
lib, "all", print_detail=print_detail
)
print(
"================================== phi kernels summary =================================="
)
print(f"phi function kernels : {phi_function_kernels_stats}")
print(f"phi structure kernels : {phi_structure_kernels_stats}")
print(f"phi all kernels : {phi_all_kernels_stats}")
print("")
else:
fluid_ops_stats = parse_paddle_kernels(lib, "fluid", print_detail=False)
phi_ops_stats = parse_paddle_kernels(lib, "phi", print_detail=False)
all_ops_stats = parse_paddle_kernels(
lib, "all", print_detail=print_detail
)
print(
"================================== fluid operators summary =================================="
)
print(f"fluid operators : {fluid_ops_stats}")
print(f"phi operators : {phi_ops_stats}")
print(f"all operators : {all_ops_stats}")
print("")
main(lib="fluid")
main(lib="phi")