2023-12-18 09:32:41 -06:00
|
|
|
##############################################################################bl
|
|
|
|
|
# MIT License
|
|
|
|
|
#
|
2025-01-23 13:09:32 -06:00
|
|
|
# Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All Rights Reserved.
|
2023-12-18 09:32:41 -06: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:
|
|
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
# Common helper routines for testing collateral
|
|
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
|
import os
|
2025-03-10 14:42:56 -04:00
|
|
|
import re
|
2023-12-18 09:32:41 -06:00
|
|
|
import shutil
|
2025-01-02 13:29:47 -08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2023-12-20 17:31:48 -06:00
|
|
|
import pandas as pd
|
2025-02-20 17:51:57 -05:00
|
|
|
|
2023-12-18 09:32:41 -06:00
|
|
|
|
|
|
|
|
def check_resource_allocation():
|
|
|
|
|
"""Check if CTEST resource allocation is enabled for parallel testing and set
|
|
|
|
|
HIP_VISIBLE_DEVICES variable accordingly with assigned gpu index.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if "CTEST_RESOURCE_GROUP_COUNT" not in os.environ:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if "CTEST_RESOURCE_GROUP_0_GPUS" in os.environ:
|
|
|
|
|
resource = os.environ["CTEST_RESOURCE_GROUP_0_GPUS"]
|
|
|
|
|
# extract assigned gpu id from env var: example format -> 'id:0,slots:1'
|
|
|
|
|
for item in resource.split(","):
|
|
|
|
|
key, value = item.split(":")
|
|
|
|
|
if key == "id":
|
|
|
|
|
os.environ["HIP_VISIBLE_DEVICES"] = value
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
2025-03-10 14:42:56 -04:00
|
|
|
def check_file_pattern(pattern, file_path):
|
|
|
|
|
"""Check if the given pattern exists in the file"""
|
|
|
|
|
content = ""
|
|
|
|
|
with open(file_path) as f:
|
|
|
|
|
content = f.read()
|
|
|
|
|
return len(re.findall(pattern, content)) != 0
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 09:32:41 -06:00
|
|
|
def get_output_dir(suffix="_output", clean_existing=True):
|
|
|
|
|
"""Provides a unique output directory based on the name of the calling test function with a suffix applied.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
suffix (str, optional): suffix to append to output_dir. Defaults to "_output".
|
|
|
|
|
clean_existing (bool, optional): Whether to remove existing directory if exists. Defaults to True.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
output_dir = inspect.stack()[1].function + suffix
|
|
|
|
|
if clean_existing:
|
2025-01-02 13:29:47 -08:00
|
|
|
if Path(output_dir).exists():
|
2023-12-18 09:32:41 -06:00
|
|
|
shutil.rmtree(output_dir)
|
|
|
|
|
return output_dir
|
2024-03-15 13:32:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_workload_dir(input_dir, suffix="_tmp", clean_existing=True):
|
|
|
|
|
"""Provides a unique input workoad directory with contents of input_dir
|
|
|
|
|
based on the name of the calling test function.
|
|
|
|
|
|
|
|
|
|
Setup is a NOOP when tests run serially.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if "PYTEST_XDIST_WORKER_COUNT" not in os.environ:
|
|
|
|
|
return input_dir
|
|
|
|
|
|
|
|
|
|
output_dir = inspect.stack()[1].function + suffix
|
|
|
|
|
if clean_existing:
|
2025-01-02 13:29:47 -08:00
|
|
|
if Path(output_dir).exists():
|
2024-03-15 13:32:00 -05:00
|
|
|
shutil.rmtree(output_dir)
|
|
|
|
|
|
|
|
|
|
shutil.copytree(input_dir, output_dir)
|
|
|
|
|
return output_dir
|
2023-12-20 17:31:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_output_dir(cleanup, output_dir):
|
2025-01-02 13:29:47 -08:00
|
|
|
"""Remove output directory generated from rocprofiler-compute execution
|
2023-12-20 17:31:48 -06:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cleanup (boolean): flag to enable/disable directory cleanup
|
|
|
|
|
output_dir (string): name of directory to remove
|
|
|
|
|
"""
|
|
|
|
|
if cleanup:
|
2025-01-02 13:29:47 -08:00
|
|
|
if Path(output_dir).exists():
|
2024-05-28 21:53:47 +00:00
|
|
|
try:
|
|
|
|
|
shutil.rmtree(output_dir)
|
|
|
|
|
except OSError as e:
|
|
|
|
|
print("WARNING: shutil.rmdir(output_dir): directory may not be empty...")
|
2023-12-20 17:31:48 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:41:31 -06:00
|
|
|
def check_csv_files(output_dir, num_devices, num_kernels):
|
2023-12-20 17:31:48 -06:00
|
|
|
"""Check profiling output csv files for expected number of entries (based on kernel invocations)
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
output_dir (string): output directory containing csv files
|
|
|
|
|
num_kernels (int): number of kernels expected to have been profiled
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
dict: dictionary housing file contents as pandas dataframe
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
file_dict = {}
|
|
|
|
|
files_in_workload = os.listdir(output_dir)
|
|
|
|
|
for file in files_in_workload:
|
|
|
|
|
if file.endswith(".csv"):
|
|
|
|
|
file_dict[file] = pd.read_csv(output_dir + "/" + file)
|
2024-01-24 11:52:03 -06:00
|
|
|
if "roofline" in file:
|
|
|
|
|
assert len(file_dict[file].index) >= num_devices
|
|
|
|
|
elif not "sysinfo" in file:
|
2023-12-20 17:31:48 -06:00
|
|
|
assert len(file_dict[file].index) >= num_kernels
|
|
|
|
|
elif file.endswith(".pdf"):
|
|
|
|
|
file_dict[file] = "pdf"
|
|
|
|
|
return file_dict
|