Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 27, 2023
1 parent 5b1ae31 commit 8b7662a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 61 deletions.
33 changes: 10 additions & 23 deletions src/acc/libsmm_acc/predict/predict_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@

sys.path.append("../")

from kernels.smm_acc import ( # noqa: E402
to_string,
kernel_algorithm,
parameter_types
)
from kernels.smm_acc import to_string, kernel_algorithm, parameter_types # noqa: E402


# ===============================================================================
Expand All @@ -37,8 +33,10 @@ def main(tunedir):
for algorithm in kernel_algorithm.keys():
training_data_file = os.path.join(tunedir, f"raw_training_data_{algorithm}.csv")
if os.path.exists(training_data_file):
print(f"WARNING: Found old data file {training_data_file}, re(move) it first ... exiting")
sys.exit(1)
print(
f"WARNING: Found old data file {training_data_file}, re(move) it first ... exiting"
)
sys.exit(1)

# Find all the 'tune_MxNxK' folders
kernel_folder_pattern = re.compile(r"tune_(\d+)x(\d+)x(\d+)$")
Expand Down Expand Up @@ -87,15 +85,12 @@ def read_log_file(log_folder, m, n, k):
# Parse the log files and collect data
data = list()
for log_file in log_files:

print(f"Processing log file {log_file}")
with open(os.path.join(log_folder, log_file), "r") as f:
log_file_content = f.read().splitlines()

for line in log_file_content:

if "OK" in line: # this line contains autotuning data

# Parse the line
match = autotuning_line.match(line)
assert match is not None, "Found null match: " + line
Expand Down Expand Up @@ -141,7 +136,6 @@ def collect_training_data(kernel_folders, kernel_folder_pattern):
# For each folder:
n_kernels = len(kernel_folders)
for i, kernel_folder in enumerate(kernel_folders):

print(f"\nProcess folder {kernel_folder} ({i+1}/{n_kernels})")

# Find (m, n, k)
Expand All @@ -155,18 +149,16 @@ def collect_training_data(kernel_folders, kernel_folder_pattern):
# Collect info from log files
log_files = [f for f in os.listdir(kernel_folder) if f[-4:] == ".log"]
if len(log_files) > 0:
data = read_log_file(kernel_folder, m, n, k)
data = read_log_file(kernel_folder, m, n, k)
else:
print(f"No log files found in folder {kernel_folder} ... skipping")
continue
print(f"No log files found in folder {kernel_folder} ... skipping")
continue

# ===============================================================================
# Write parameters to CSV
for name_algo, kernel_algo in kernel_algorithm.items():

# if applicable to this mnk
if name_algo in data["algorithm"].values:

# Does collected csv file exist already?
raw_parameters_file_name = os.path.join(
kernel_folder,
Expand Down Expand Up @@ -194,7 +186,6 @@ def merge_data_files(tunedir):
Merge CSV files
"""
for algorithm in kernel_algorithm.keys():

training_data_file = os.path.join(
tunedir, "raw_training_data_{algorithm}.csv".format(algorithm=algorithm)
)
Expand All @@ -207,9 +198,7 @@ def merge_data_files(tunedir):

filenames_pattern = os.path.join(
tunedir,
"tune_*/raw_training_data_*_{algorithm}.csv".format(
algorithm=algorithm
),
"tune_*/raw_training_data_*_{algorithm}.csv".format(algorithm=algorithm),
)
print("Merging all files with pattern:", filenames_pattern)
filenames = glob.glob(filenames_pattern)
Expand All @@ -228,9 +217,7 @@ def merge_data_files(tunedir):
out.write(f.read()) # write the rest of the file
# Write the rest of the files, skipping the header line each time
for i, fn in enumerate(filenames):
print(
"writing from {} ({}/{})".format(fn, i + 1, len(filenames))
)
print("writing from {} ({}/{})".format(fn, i + 1, len(filenames)))
with open(fn) as f:
header_line = next(f) # skip header line
assert header_line == header_line_ref, (
Expand Down
19 changes: 9 additions & 10 deletions src/acc/libsmm_acc/predict/predict_genpars.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from joblib import Parallel, delayed
from predict_helpers import safe_pickle_load
from warnings import simplefilter
simplefilter(action='ignore', category=UserWarning)

simplefilter(action="ignore", category=UserWarning)

sys.path.append("../")
from kernels.smm_acc import to_tuple, to_string # noqa: E402
Expand All @@ -31,7 +32,8 @@
)

# The joblib backend spawns additional processes, which do not inherit the warning filters applied using warnings.filterwarnings
os.environ['PYTHONWARNINGS']='ignore::UserWarning'
os.environ["PYTHONWARNINGS"] = "ignore::UserWarning"


# ===============================================================================
def main(params, njobs, baseline, paths_to_models, chunk_size):
Expand Down Expand Up @@ -140,7 +142,6 @@ def find_optimal_kernel(
optimal_kernels = dict()

else:

# Get predictor features from raw parameters
parameter_sets = PredictiveParameters(
parameter_space, gpu_properties, autotuning_properties, None
Expand Down Expand Up @@ -231,16 +232,15 @@ def get_optimal_kernels(
print("Caching intermediate results to:", ckpt_folder_name)

for i in range(0, num_mnks_by_algo, chunk_size):

# Chunk up tasks
start_chunk = i
end_chunk = int(min(start_chunk + chunk_size, num_mnks_by_algo))
print(f"Completed {i} tasks out of {num_mnks_by_algo}")

# Create checkpoint file or load checkpointed data from it
checkpoint_file_name = os.path.join(
ckpt_folder_name, f"chunk_{start_chunk}-{end_chunk - 1}.json"
)
ckpt_folder_name, f"chunk_{start_chunk}-{end_chunk - 1}.json"
)

if os.path.exists(checkpoint_file_name):
with open(checkpoint_file_name, "r") as f:
Expand All @@ -256,14 +256,15 @@ def get_optimal_kernels(
print(f"Read chunk {start_chunk}-{end_chunk - 1}\n")

else:

if njobs == 1:
j = i
# Ignore joblib and run serially:
for mnk, algo in mnks_by_algo:
j += 1
gc.collect()
print(f"{j:6d} of {num_mnks_by_algo}: Find optimal kernels for mnk = {mnk} algo = {algo}")
print(
f"{j:6d} of {num_mnks_by_algo}: Find optimal kernels for mnk = {mnk} algo = {algo}"
)
optimal_kernels_list_ = find_optimal_kernel(
mnk,
algo,
Expand All @@ -273,7 +274,6 @@ def get_optimal_kernels(
autotuning_properties,
)
else:

# Run prediction tasks in parallel with joblib
optimal_kernels_list_ = Parallel(n_jobs=njobs, verbose=2)(
delayed(find_optimal_kernel, check_pickle=True)(
Expand Down Expand Up @@ -323,7 +323,6 @@ def get_optimal_kernels(


def get_baseline_kernels(mnks_to_predict, gpu_propertes, autotuning_properties):

print("Getting baseline kernels")
baseline_algorithm = "medium"
baseline_kernels = list()
Expand Down
1 change: 0 additions & 1 deletion src/acc/libsmm_acc/predict/predict_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def plot_choice_goodness(
pp,
scaled=True,
):

# Sort in ascending performances
data_mnk = pd.DataFrame()
if scaled:
Expand Down
38 changes: 16 additions & 22 deletions src/acc/libsmm_acc/predict/prepare_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@

sys.path.append("../")

from kernels.smm_acc import ( # noqa: E402
kernel_algorithm,
mnk_pattern
)
from kernels.smm_acc_predict import (
PredictiveParameters,
derived_parameters
)
from kernels.smm_acc import kernel_algorithm, mnk_pattern # noqa: E402
from kernels.smm_acc_predict import PredictiveParameters, derived_parameters


# ===============================================================================
Expand Down Expand Up @@ -119,10 +113,10 @@ def get_performance_closest_to_baseline(
if len(idx_baseline) > 0:
break
else:
assert False, (
'Could not find closest baseline for mnk=({}x{}x{}) and for algorithm "{}".'
"\nLast baseline parameters searched:\n{}"
"\nParameter sets searched:\n".format(m, n, k, algorithm, baseline_pars)
raise AssertionError(
'Could not find closest baseline for mnk=({}x{}x{}) and for algorithm "{}".\nLast baseline parameters searched:\n{}\nParameter sets searched:\n'.format(
m, n, k, algorithm, baseline_pars
)
)

idx_baseline = idx_baseline[0]
Expand All @@ -148,8 +142,7 @@ def process_chunk(data_chunk, algorithm, gpu_properties, autotuning_properties):
# For each (mnk), ...
baseline_performances = dict()
max_performances = dict()
for i, mnk in enumerate(mnks):

for _i, mnk in enumerate(mnks):
data_mnk = data_chunk[data_chunk["mnk"] == mnk]
m, n, k = mnk_pattern.match(mnk).groups()
m, n, k = int(m), int(n), int(k)
Expand Down Expand Up @@ -193,7 +186,6 @@ def write_to_parquet(data_path, algorithm):
print("Found {:40}, skipping".format(parquet_file_done))

else:

print("Didn't find {:40}, generating".format(parquet_file_done))

# [RAW] Read CSV files into Pandas dataframes
Expand Down Expand Up @@ -355,7 +347,6 @@ def write_baseline_and_max_records_per_algorithm(
print("Found {:40}, skipping".format(max_performances_per_algo_file))

else:

print("Processing data of algorithm {}".format(algorithm))
raw_pars_cols = kernel_algorithm[algorithm].launch_parameters
if algorithm in ["largeDB1", "largeDB2"]:
Expand All @@ -370,7 +361,9 @@ def write_baseline_and_max_records_per_algorithm(
delayed(process_chunk, check_pickle=True)(
data_chunk, algorithm, gpu_properties, autotuning_properties
)
for data_chunk in tqdm(pd.read_csv(data_file_raw, chunksize=chunk_size), disable=True)
for data_chunk in tqdm(
pd.read_csv(data_file_raw, chunksize=chunk_size), disable=True
)
)

baseline_performance_dictionaries, maximums_performance_dictionaries = zip(
Expand Down Expand Up @@ -419,7 +412,9 @@ def get_baseline_performance(mnk, base_list, raw_pars_cols):
delayed(get_baseline_performance, check_pickle=True)(
mnk, base_list, raw_pars_cols
)
for mnk, base_list in tqdm(baseline_performance_dictionary.items(), disable=True)
for mnk, base_list in tqdm(
baseline_performance_dictionary.items(), disable=True
)
)

baseline_performances = dict(
Expand Down Expand Up @@ -457,9 +452,9 @@ def plot_baseline(baseline_perfs_by_algo, data_path, algorithms):
baseline_perfs[mnk] = baseline_perfs_by_algo[algo][mnk]
break
else:
assert (
False
), "NOOOO this is actually impossible by def of all_mnks, isn't it?"
raise AssertionError(
"NOOOO this is actually impossible by def of all_mnks, isn't it?"
)

# Sort
mnks = list()
Expand Down Expand Up @@ -667,7 +662,6 @@ def write_derived_data(data_path, algorithm, arch, n_jobs, chunk_size):
print("Found {:40}, skipping".format(derived_training_data_filename))

else:

print("Didn't find {:40}, generating".format(derived_training_data_filename))

# Read max performances, GPU properties and autotuning properties
Expand Down
1 change: 0 additions & 1 deletion src/acc/libsmm_acc/tune/tune_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


def main(param_fn):

# Read new kernel parameters
param_new = "../parameters/parameters.json"
with open(param_new) as f:
Expand Down
5 changes: 1 addition & 4 deletions src/acc/libsmm_acc/tune/tune_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def main(
blocks_from_param_file,
tune_dir: Path,
):

# Read existing parameters
assert (
param_fn.name in gpu_architectures.keys()
Expand Down Expand Up @@ -72,7 +71,7 @@ def main(
triples = combinations(*blocksizes)
print(f"Requested to autotune {len(triples)} triplets")

for (m, n, k) in triples:
for m, n, k in triples:
existing = [kern for kern in autotuned_kernels if kern.can_handle(m, n, k)]
if existing:
print(
Expand Down Expand Up @@ -254,7 +253,6 @@ def gen_benchmark(outdir, gpu_properties, autotuning_properties, compiler, m, n,

# ===============================================================================
def gen_jobfile(outdir, compiler, m, n, k, cpus_per_task, max_num_nodes=0):

file_extension = get_file_extension_from_compiler(compiler)

tprefix = f"tune_{int(m)}x{int(n)}x{int(k)}"
Expand Down Expand Up @@ -334,7 +332,6 @@ def gen_jobfile(outdir, compiler, m, n, k, cpus_per_task, max_num_nodes=0):

# ===============================================================================
def gen_makefile(outdir, compiler, arch):

file_extension = get_file_extension_from_compiler(compiler)

# header
Expand Down

0 comments on commit 8b7662a

Please sign in to comment.