Files
rocm-systems/src/rocprof_compute_base.py
T

298 خطوط
10 KiB
Python

##############################################################################bl
# MIT License
#
2024-01-24 17:11:39 -06:00
# Copyright (c) 2021 - 2024 Advanced Micro Devices, Inc. All Rights Reserved.
#
# 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:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# 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
# 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.
##############################################################################el
import argparse
import logging
import sys
import os
2023-10-31 15:11:17 -05:00
from pathlib import Path
import shutil
2024-02-08 12:23:20 -06:00
from utils.specs import MachineSpecs, generate_machine_specs
2024-03-04 12:57:25 -06:00
from utils.utils import (
demarcate,
get_version,
get_version_display,
detect_rocprof,
get_submodules,
console_debug,
2024-03-04 12:57:25 -06:00
console_log,
console_error,
set_locale_encoding,
2024-03-04 12:57:25 -06:00
)
from utils.logger import (
setup_console_handler,
setup_logging_priority,
setup_file_handler,
)
2023-10-31 15:11:17 -05:00
from argparser import omniarg_parser
import config
import pandas as pd
import importlib
2024-01-24 12:44:42 -06:00
SUPPORTED_ARCHS = {
"gfx906": {"mi50": ["MI50", "MI60"]},
"gfx908": {"mi100": ["MI100"]},
"gfx90a": {"mi200": ["MI210", "MI250", "MI250X"]},
"gfx940": {"mi300": ["MI300A_A0"]},
"gfx941": {"mi300": ["MI300X_A0"]},
"gfx942": {"mi300": ["MI300A_A1", "MI300X_A1"]},
}
MI300_CHIP_IDS = {
"29856": "MI300A_A1",
"29857": "MI300X_A1",
"29858": "MI308X",
}
2024-02-16 15:34:28 -06:00
class RocProfCompute:
def __init__(self):
self.__args = None
self.__profiler_mode = None
self.__analyze_mode = None
2024-02-16 15:34:28 -06:00
self.__soc_name = (
set()
) # gpu name, or in case of analyze mode, all loaded gpu name(s)
self.__soc = dict() # set of key, value pairs. Where arch->OmniSoc() obj
2023-10-31 15:11:17 -05:00
self.__version = {
"ver": None,
"ver_pretty": None,
}
self.__options = {}
2024-01-24 12:44:42 -06:00
self.__supported_archs = SUPPORTED_ARCHS
2024-03-01 11:52:31 -06:00
self.__mspec: MachineSpecs = None # to be initalized in load_soc_specs()
setup_console_handler()
2023-10-31 15:11:17 -05:00
self.set_version()
self.parse_args()
self.__mode = self.__args.mode
self.__loglevel = setup_logging_priority(
self.__args.verbose, self.__args.quiet, self.__mode
)
setattr(self.__args, "loglevel", self.__loglevel)
set_locale_encoding()
2023-10-31 15:11:17 -05:00
if self.__mode == "profile":
self.detect_profiler()
elif self.__mode == "analyze":
self.detect_analyze()
2024-03-04 12:57:25 -06:00
console_debug("Execution mode = %s" % self.__mode)
2024-03-04 12:57:25 -06:00
2023-10-31 15:11:17 -05:00
def print_graphic(self):
2024-03-01 11:52:31 -06:00
"""Log program name as ascii art to terminal."""
ascii_art = r"""
__ _
_ __ ___ ___ _ __ _ __ ___ / _| ___ ___ _ __ ___ _ __ _ _| |_ ___
| '__/ _ \ / __| '_ \| '__/ _ \| |_ _____ / __/ _ \| '_ ` _ \| '_ \| | | | __/ _ \
| | | (_) | (__| |_) | | | (_) | _|_____| (_| (_) | | | | | | |_) | |_| | || __/
|_| \___/ \___| .__/|_| \___/|_| \___\___/|_| |_| |_| .__/ \__,_|\__\___|
|_| |_|
2024-02-16 15:34:28 -06:00
"""
2024-01-30 17:25:16 -06:00
print(ascii_art)
def get_mode(self):
return self.__mode
2024-02-16 15:34:28 -06:00
2023-10-31 15:11:17 -05:00
def set_version(self):
vData = get_version(config.rocprof_compute_home)
2023-10-31 15:11:17 -05:00
self.__version["ver"] = vData["version"]
2024-02-16 15:34:28 -06:00
self.__version["ver_pretty"] = get_version_display(
vData["version"], vData["sha"], vData["mode"]
)
2023-10-31 15:11:17 -05:00
return
2024-03-04 12:57:25 -06:00
2023-10-31 15:11:17 -05:00
def detect_profiler(self):
2024-02-16 15:34:28 -06:00
if (
self.__args.lucky == True
or self.__args.summaries == True
or self.__args.use_rocscope
):
2023-10-31 15:11:17 -05:00
if not shutil.which("rocscope"):
2024-01-30 17:25:16 -06:00
console_error("Rocscope must be in PATH")
2023-10-31 15:11:17 -05:00
else:
self.__profiler_mode = "rocscope"
else:
rocprof_cmd = detect_rocprof()
if str(rocprof_cmd).endswith("rocprof"):
self.__profiler_mode = "rocprofv1"
elif str(rocprof_cmd).endswith("rocprofv2"):
self.__profiler_mode = "rocprofv2"
else:
2024-03-04 12:57:25 -06:00
console_error(
"Incompatible profiler: %s. Supported profilers include: %s"
% (rocprof_cmd, get_submodules("rocprof_compute_profile"))
2024-03-04 12:57:25 -06:00
)
2023-10-31 15:11:17 -05:00
return
2024-02-16 15:34:28 -06:00
def detect_analyze(self):
if self.__args.gui:
self.__analyze_mode = "web_ui"
else:
self.__analyze_mode = "cli"
return
2023-10-31 15:11:17 -05:00
@demarcate
2024-03-01 11:52:31 -06:00
def load_soc_specs(self, sysinfo: dict = None):
"""Load OmniSoC instance for RocProfCompute run"""
self.__mspec = generate_machine_specs(self.__args, sysinfo)
if self.__args.specs:
print(self.__mspec)
sys.exit(0)
arch = self.__mspec.gpu_arch
2023-10-31 15:11:17 -05:00
# NB: This checker is a bit redundent. We already check this in specs module
if arch not in self.__supported_archs.keys():
2024-02-08 12:23:20 -06:00
console_error("%s is an unsupported SoC" % arch)
soc_module = importlib.import_module("rocprof_compute_soc.soc_" + arch)
2024-03-01 11:52:31 -06:00
soc_class = getattr(soc_module, arch + "_soc")
self.__soc[arch] = soc_class(self.__args, self.__mspec)
return
2024-01-30 17:25:16 -06:00
2023-10-31 15:11:17 -05:00
def parse_args(self):
parser = argparse.ArgumentParser(
description="Command line interface for AMD's GPU profiler, ROCm Compute Profiler",
2024-02-16 15:34:28 -06:00
prog="tool",
formatter_class=lambda prog: argparse.RawTextHelpFormatter(
2023-10-31 15:11:17 -05:00
prog, max_help_position=30
),
usage="rocprof-compute [mode] [options]",
2023-10-31 15:11:17 -05:00
)
2024-02-16 15:34:28 -06:00
omniarg_parser(
parser, config.rocprof_compute_home, self.__supported_archs, self.__version
2024-02-16 15:34:28 -06:00
)
2023-10-31 15:11:17 -05:00
self.__args = parser.parse_args()
if self.__args.mode == None:
if self.__args.specs:
print(generate_machine_specs(self.__args))
sys.exit(0)
2023-10-31 15:11:17 -05:00
parser.print_help(sys.stderr)
console_error(
"rocprof-compute requires you pass a valid mode. Detected None."
)
return
@demarcate
def run_profiler(self):
2023-10-31 15:11:17 -05:00
self.print_graphic()
self.load_soc_specs()
2023-10-31 15:11:17 -05:00
# Update default path
if self.__args.path == os.path.join(os.getcwd(), "workloads"):
2024-03-01 11:52:31 -06:00
self.__args.path = os.path.join(
self.__args.path, self.__args.name, self.__mspec.gpu_model
)
# instantiate desired profiler
2023-10-31 15:11:17 -05:00
if self.__profiler_mode == "rocprofv1":
from rocprof_compute_profile.profiler_rocprof_v1 import rocprof_v1_profiler
2024-03-01 11:52:31 -06:00
profiler = rocprof_v1_profiler(
self.__args, self.__profiler_mode, self.__soc[self.__mspec.gpu_arch]
)
2023-10-31 15:11:17 -05:00
elif self.__profiler_mode == "rocprofv2":
from rocprof_compute_profile.profiler_rocprof_v2 import rocprof_v2_profiler
2024-03-01 11:52:31 -06:00
profiler = rocprof_v2_profiler(
self.__args, self.__profiler_mode, self.__soc[self.__mspec.gpu_arch]
)
elif self.__profiler_mode == "rocscope":
from rocprof_compute_profile.profiler_rocscope import rocscope_profiler
2024-03-01 11:52:31 -06:00
profiler = rocscope_profiler(
self.__args, self.__profiler_mode, self.__soc[self.__mspec.gpu_arch]
)
else:
2024-01-30 17:25:16 -06:00
console_error("Unsupported profiler")
2024-02-16 15:34:28 -06:00
# -----------------------
# run profiling workflow
2024-03-01 11:52:31 -06:00
# -----------------------
self.__soc[self.__mspec.gpu_arch].profiling_setup()
# enable file-based logging
setup_file_handler(self.__args.loglevel, self.__args.path)
profiler.pre_processing()
2023-10-31 15:11:17 -05:00
profiler.run_profiling(self.__version["ver"], config.prog)
profiler.post_processing()
self.__soc[self.__mspec.gpu_arch].post_profiling()
return
@demarcate
2024-01-30 10:43:30 -06:00
def update_db(self):
2023-10-31 15:11:17 -05:00
self.print_graphic()
2024-01-30 10:43:30 -06:00
from utils.db_connector import DatabaseConnector
2024-02-16 15:34:28 -06:00
2024-01-30 10:43:30 -06:00
db_connection = DatabaseConnector(self.__args)
2024-02-16 15:34:28 -06:00
# -----------------------
2024-01-30 10:43:30 -06:00
# run database workflow
2024-02-16 15:34:28 -06:00
# -----------------------
2024-01-30 10:43:30 -06:00
db_connection.pre_processing()
if self.__args.upload:
db_connection.db_import()
else:
db_connection.db_remove()
2024-02-16 15:34:28 -06:00
return
@demarcate
def run_analysis(self):
2023-10-31 15:11:17 -05:00
self.print_graphic()
2024-03-04 12:57:25 -06:00
console_log("Analysis mode = %s" % self.__analyze_mode)
if self.__analyze_mode == "cli":
from rocprof_compute_analyze.analysis_cli import cli_analysis
2024-02-16 15:34:28 -06:00
2023-12-18 16:37:01 -06:00
analyzer = cli_analysis(self.__args, self.__supported_archs)
elif self.__analyze_mode == "web_ui":
from rocprof_compute_analyze.analysis_webui import webui_analysis
2024-02-16 15:34:28 -06:00
2023-12-18 16:37:01 -06:00
analyzer = webui_analysis(self.__args, self.__supported_archs)
else:
2024-03-11 11:14:45 -05:00
console_error("Unsupported analysis mode -> %s" % self.__analyze_mode)
2024-02-16 15:34:28 -06:00
# -----------------------
# run analysis workflow
2024-02-16 15:34:28 -06:00
# -----------------------
analyzer.sanitize()
2024-02-16 15:34:28 -06:00
# Load required SoC(s) from input
for d in analyzer.get_args().path:
sys_info = pd.read_csv(Path(d[0], "sysinfo.csv"))
2024-03-01 11:52:31 -06:00
sys_info = sys_info.to_dict("list")
sys_info = {key: value[0] for key, value in sys_info.items()}
self.load_soc_specs(sys_info)
2023-12-18 16:37:01 -06:00
analyzer.set_soc(self.__soc)
analyzer.pre_processing()
analyzer.run_analysis()
return