Files
rocm-systems/src/utils/kernel_name_shortener.py
T

134 rivejä
5.4 KiB
Python
Raaka Normaali näkymä Historia

2023-02-13 09:26:12 -06:00
##############################################################################bl
# MIT License
2023-02-13 14:50:24 -06:00
#
2025-01-23 13:09:32 -06:00
# Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All Rights Reserved.
2023-02-13 14:50:24 -06:00
#
2022-11-04 14:49:36 -05:00
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
2023-02-13 14:50:24 -06:00
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
2023-02-13 14:50:24 -06:00
#
2022-11-04 14:49:36 -05:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2022-11-04 14:49:36 -05:00
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2023-02-13 09:26:12 -06:00
##############################################################################el
2022-11-04 14:49:36 -05:00
import re
import subprocess
2025-01-02 13:29:47 -08:00
from pathlib import Path
2022-11-04 14:49:36 -05:00
import pandas as pd
2023-08-15 14:00:36 -05:00
from utils.logger import console_debug, console_error, console_log
2022-11-04 14:49:36 -05:00
cache = dict()
2023-08-10 11:16:50 -05:00
2024-02-16 15:34:28 -06:00
# Note: shortener is now dependent on a rocprof install with llvm
2024-03-12 15:54:52 -05:00
def kernel_name_shortener(df, level):
2023-08-15 14:00:36 -05:00
def shorten_file(df, level):
global cache
2023-08-09 10:46:57 -05:00
column_name = ""
if "Kernel_Name" in df:
column_name = "Kernel_Name"
2023-08-15 14:00:36 -05:00
if "Name" in df:
column_name = "Name"
2023-08-09 10:46:57 -05:00
if column_name == "Kernel_Name" or column_name == "Name":
2023-08-15 14:00:36 -05:00
# loop through all indices
for index in df.index:
original_name = df.loc[index, column_name]
2023-08-15 14:00:36 -05:00
if original_name in cache:
continue
2023-08-10 11:13:27 -05:00
2023-08-24 16:43:45 -05:00
cmd = [cpp_filt, original_name]
2023-08-10 11:13:27 -05:00
2023-08-15 14:09:38 -05:00
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
2023-08-10 11:13:27 -05:00
2023-08-15 14:00:36 -05:00
demangled_name, e = proc.communicate()
demangled_name = str(demangled_name, "UTF-8").strip()
2023-08-09 10:46:57 -05:00
2023-08-15 14:00:36 -05:00
# cache miss, add the shortened name to the dictionary
new_name = ""
matches = ""
2023-08-09 10:46:57 -05:00
2023-08-15 14:09:38 -05:00
names_and_args = re.compile(
r"(?P<name>[( )A-Za-z0-9_]+)([ ,*<>()]+)(::)?"
)
2023-08-09 10:46:57 -05:00
2023-08-15 14:00:36 -05:00
# works for name Kokkos::namespace::init_lock_array_kernel_threadid(int) [clone .kd]
if names_and_args.search(demangled_name):
matches = names_and_args.findall(demangled_name)
else:
# Works for first case '__amd_rocclr_fillBuffer.kd'
cache[original_name] = new_name
if new_name == None or new_name == "":
cache[original_name] = demangled_name
2023-08-09 10:46:57 -05:00
continue
2023-08-15 14:00:36 -05:00
current_level = 0
for name in matches:
##can cause errors if a function name or argument is equal to 'clone'
if name[0] == "clone":
continue
if len(name) == 3:
if name[2] == "::":
continue
2023-08-09 10:46:57 -05:00
if current_level < level:
2023-08-15 14:00:36 -05:00
new_name += name[0]
# closing '>' is to be taken account by the while loop
if name[1].count(">") == 0:
if current_level < level:
2023-08-15 14:09:38 -05:00
if not (
current_level == level - 1 and name[1].count("<") > 0
):
2023-08-15 14:00:36 -05:00
new_name += name[1]
current_level += name[1].count("<")
curr_index = 0
# cases include '>' '> >, ' have to go in depth here to not lose account of commas and current level
while name[1].count(">") > 0 and curr_index < len(name[1]):
if current_level < level:
new_name += name[1][curr_index:]
current_level -= name[1][curr_index:].count(">")
curr_index = len(name[1])
elif name[1][curr_index] == (">"):
current_level -= 1
curr_index += 1
2023-08-09 10:46:57 -05:00
2023-08-15 14:00:36 -05:00
cache[original_name] = new_name
if new_name == None or new_name == "":
cache[original_name] = demangled_name
2023-08-09 10:46:57 -05:00
df[column_name] = df[column_name].map(cache)
2023-08-09 10:46:57 -05:00
2023-08-15 14:00:36 -05:00
return df
2023-08-15 14:09:38 -05:00
2023-08-15 14:00:36 -05:00
# Only shorten if valid shortening level
if level < 5:
2025-01-02 13:29:47 -08:00
cpp_filt = str(Path("/usr").joinpath("bin", "c++filt"))
if not Path(cpp_filt).is_file():
2024-03-04 12:57:25 -06:00
console_error(
"Could not resolve c++filt in expected directory: %s" % cpp_filt
)
2024-03-12 15:54:52 -05:00
try:
modified_df = shorten_file(df, level)
console_log("profiling", "Kernel_Name shortening complete.")
return modified_df
except pd.errors.EmptyDataError:
console_debug("profiling", "Skipping shortening on empty csv")