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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
|
#!/usr/bin/env python3
# Copyright (C) 2021 - 2022 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.
"""Offline tuning utilities for rocFFT.
Overview
========
General workflow:
- tune: runs a suite of FFTs to collect timing information
- merge: post processes timing information to compute various statistics
General arguments shared between tune and merge commands:
--workspace :
--tuner :
--bench :
Runs/subprocesses are logged to `rocfft-tuner.log`.
Tune
===
The 'run' command drives rocfft-offline-tuner. To use this executable,
you must add -DROCFFT_BUILD_OFFLINE_TUNER=ON to the build option. There
is handy argument -t | --tuner for install.sh (`./install.sh -t or --tuner`)
Test problems are generated using a `ProblemGenerator` and a filter.
The problems generator is the same one as rocfft-perf (bench)
See
$ rocfft-tuner -h
Using the `--suite/-S` option, problems are loaded from a "suites"
file. The default suites file is `suites.py`. Alternatively, you can
load the suite named "qa1" from a file called "mysuites.py" like this:
$ rocfft-tuner -w [workspace] tune -s mysuites:qa1 ...
That is, FILENAME:SUITENAME.
All the output files are stored in the workspace directory.
Sub-foler `ResultSolutions` contains the tuned solution map.
Sub-foler `TuningCandidates` contains intermediate candidate solutions
which are for debugging purpose.
Sub-foler `TuningData` contains csv files recording some benchmarking
numbers and kernel information, which is for analysis purpose.
Merge
===============
Dynamic testing is enabled by specifying more than one `--lib/-i`
option. These are passed down to the benchmarker, and hence it is
assumed that the specific benchmarker is a "dyna" bench.
Multiple output directories are used to store the results.
"""
import argparse
import logging
import subprocess
import statistics
import sys
import os
import tempfile
import re
import collections
import json
import pathlib
from os import listdir
from os.path import isfile, join
from copy import deepcopy
from pathlib import Path
from multiprocessing import Pool
top = Path(__file__).resolve().parent
sys.path.append(str(top))
import perflib
from perflib.generators import Problem
from perflib.utils import flatten
console = logging.StreamHandler()
import types
#
# Helpers
#
def create_launcher(generator):
launcher = collections.defaultdict(list)
for problem in generator.generate_problems():
launcher['problems'].append(problem.toJSON())
return launcher
# get gfx___ which is the prefix of the solution map
def get_local_gpu_gfx():
try:
for line in subprocess.Popen(
args=["rocminfo"], stdout=subprocess.PIPE).stdout.readlines():
if b'amdgcn-amd-amdhsa--' in line:
# gfxNNN[:sramecc][:xnack+/-]
gcn_arch = line.split(b'--')[1].strip()
# raw gfxNNN
gfx_target = gcn_arch.split(b':')[0]
return gfx_target.decode('utf-8')
except:
pass
return ''
# #
# # Commands
# #
def command_tuning(arguments):
"""Run tuning."""
if arguments.workspace:
workspace = Path(arguments.workspace)
workspace.mkdir(parents=True, exist_ok=True)
dump_folder = workspace / "TuningCandidates"
result_folder = workspace / "ResultSolutions"
csv_folder = workspace / "TuningData"
dump_folder.mkdir(parents=True, exist_ok=True)
result_folder.mkdir(parents=True, exist_ok=True)
csv_folder.mkdir(parents=True, exist_ok=True)
os.environ['TUNING_WORKSPACE'] = arguments.workspace
else:
print(
"Workspace not set. use -w /path/of/workspace before command arg")
return
if arguments.input:
tuning_metadata_file = open(arguments.input, 'r')
launcher = json.load(tuning_metadata_file)
print('load from json file, suite argument will be ignored')
elif arguments.suite:
# set up problems
generator = perflib.generators.SuiteProblemGenerator(arguments.suite)
launcher = create_launcher(generator)
else:
print(
"No input data, use -i /path/to/jsonfile or -s suite_name for input"
)
return
# check flag setting, could be from --input file or from argument list
if 'dump_candidates' not in launcher:
launcher['dump_candidates'] = arguments.dump
if 'print_reject_reason' not in launcher:
launcher['print_reject_reason'] = arguments.print_reject
if 'force_full_token' not in launcher and arguments.exact_token is True:
launcher['force_full_token'] = True
if 'overwrite_max_wgs' not in launcher and arguments.max_wgs is not None:
launcher['overwrite_max_wgs'] = arguments.max_wgs
if 'overwrite_min_wgs' not in launcher and arguments.min_wgs is not None:
launcher['overwrite_min_wgs'] = arguments.min_wgs
# remind users if we are using a global value
if 'force_full_token' in launcher:
print("export-full-token is turned on for all problems")
if 'overwrite_max_wgs' in launcher:
print("max-workgroup-sizes is globally overwritten to {}".format(
arguments.max_wgs))
if 'overwrite_min_wgs' in launcher:
print("min-workgroup-sizes is globally overwritten to {}".format(
arguments.min_wgs))
# dump the tuning configuration to a file, next time we can directly use it
tuning_metadata_file = open(workspace / 'tuning_meta_data.json', 'w')
json.dump(launcher, tuning_metadata_file)
# the launcher dict MUST contain the following 3 keys (since we have default value in args)
# set env variable: dump
if launcher['dump_candidates'] == True:
os.environ['DUMP_TUNING'] = '1'
elif 'DUMP_TUNING' in os.environ:
del os.environ['DUMP_TUNING']
# set env variable: print rejection reason
if launcher['print_reject_reason'] == True:
os.environ['PRINT_REJECT_REASON'] = '1'
elif 'PRINT_REJECT_REASON' in os.environ:
del os.environ['PRINT_REJECT_REASON']
# log layer
os.environ['ROCFFT_LAYER'] = '64'
# enable cache file to speed up tuning
os.environ['ROCFFT_RTC_CACHE_PATH'] = 'rocFFT_kernel_cache.db'
tuning_summaries = []
single_summary = {}
merging_metadata = []
single_merging_meta = {}
for prob in launcher['problems']:
# set TUNE_EXACT_PROB when it's set true from cmd, or use the setting in each problem
if 'force_full_token' in launcher or prob['full_token'] == True:
os.environ['TUNE_EXACT_PROB'] = '1'
# overwritting min_ and max_wgs are optional, when not specified, we use the setting in each problem
os.environ['MAX_WGS'] = str(
launcher['overwrite_max_wgs']
) if 'overwrite_max_wgs' in launcher else str(prob['max_wgs'])
os.environ['MIN_WGS'] = str(
launcher['overwrite_min_wgs']
) if 'overwrite_min_wgs' in launcher else str(prob['min_wgs'])
token, outfile, summary, success = perflib.tuner.run(
arguments.tuner,
prob['length'],
direction=prob['direction'],
real=prob['real'],
inplace=prob['inplace'],
precision=prob['precision'],
nbatch=prob['nbatch'],
ntrial=10)
single_summary['problem'] = prob
single_summary['token'] = token
single_summary['outfile'] = outfile
single_summary['result'] = summary
single_summary['valid'] = success
tuning_summaries.append(deepcopy(single_summary))
if success:
single_merging_meta['problem'] = prob
single_merging_meta['outfile'] = outfile
single_merging_meta['token'] = token
merging_metadata.append(deepcopy(single_merging_meta))
if 'TUNE_EXACT_PROB' in os.environ:
del os.environ['TUNE_EXACT_PROB']
del os.environ['ROCFFT_RTC_CACHE_PATH']
del os.environ['MIN_WGS']
del os.environ['MAX_WGS']
# print all summary
print('==================\n[Tuning Summaries]\n==================\n')
for summary in tuning_summaries:
print('===================================================')
print('[Token]: ' + summary['token'])
print(summary['result'])
if (summary['valid'] == False):
print('Tuning Failed.')
else:
print('[Export to File]: ' + summary['outfile'])
# write a summary file saving problems and output file and valid status,
# this file will be used in merging
merging_metadata_file = open(workspace / 'merging_meta_data.json', 'w')
json.dump(merging_metadata, merging_metadata_file)
def validate(validator, testing_meta_data, skip):
for single_meta in testing_meta_data:
if skip is True:
single_meta['Valid'] = True
continue
prob = single_meta['problem']
new_single_solution_file = Path(single_meta['outfile'])
print("\nAccuracy test for the new solution {}...".format(
new_single_solution_file))
# check the solution map file
if not new_single_solution_file.exists():
print("\tNot valid solution map file found! Abort")
continue
# set the explicit solution map filepath for running test
os.environ['ROCFFT_READ_EXPLICIT_SOL_MAP_FILE'] = str(
new_single_solution_file)
# use token, change batch to at least 10, since we are running validation,
# but if the sol is exported with full_token, then we don't modify the token
token = str(single_meta['token'])
if prob['nbatch'] < 10 and prob['full_token'] == False:
old_batch_str = '_batch_' + str(prob['nbatch']) + '_'
new_batch_str = '_batch_10_'
token = token.replace(old_batch_str, new_batch_str)
# run rocfft-test
single_meta['Valid'] = perflib.tuner.accuracy_test(
validator,
prob['length'],
direction=prob['direction'],
real=prob['real'],
inplace=prob['inplace'],
precision=str(prob['precision']),
nbatch=max(prob['nbatch'], 10),
token=token)
del os.environ['ROCFFT_READ_EXPLICIT_SOL_MAP_FILE']
def output_merge_file(solution_merger, old_sol_filepath, insertions,
output_filepath):
"""Merge solutions."""
print("Step1: \n\tReading old solution map from: {}".format(
old_sol_filepath))
if not old_sol_filepath.exists():
old_sol_filepath = None
print("Step2:")
num_inserts = len(insertions)
for i in range(num_inserts):
if i > 0:
old_sol_filepath = output_filepath
new_sol = insertions[i]
probKey = str(new_sol['arch']) + ':' + str(new_sol['probToken'])
print("\t\tInserting new solutions of {}".format(probKey))
success = perflib.tuner.merge(solution_merger,
base_file_path=old_sol_filepath,
new_files=new_sol['solutionFile'],
new_probTokens=probKey,
out_file_path=output_filepath)
print("\t\t\tDone!" if success else "\t\t\tFailed!")
print("Step3:\n\tFinishing writing merge solution map to: {}".format(
output_filepath))
def command_merging(arguments):
"""Merge solutions."""
prefix = get_local_gpu_gfx()
sol_map_filename = Path(prefix + '_rocfft_solution_map.dat')
if arguments.workspace:
workspace = Path(arguments.workspace)
else:
print(
"Workspace not set. use -w /path/of/workspace before command arg")
return
if arguments.metafile:
merging_meta_file = open(arguments.metafile, 'r')
merging_meta_data = json.load(merging_meta_file)
else:
print("No input data, use --metafile=/path/of/merging-meta-file")
return
if arguments.outfolder:
outfolder = workspace / Path(arguments.outfolder)
outfolder.mkdir(parents=True, exist_ok=True)
output_file = outfolder / sol_map_filename
else:
print(
"output folder is not set. use -outfolder=/subfolder/of/outputfolder"
)
return
reference_sol_folder = Path(arguments.ref_sol_folder)
reference_file = reference_sol_folder / sol_map_filename
# check if everything is well...
print("\nThe local AMDGPU is {}".format(prefix))
os.environ['ROCFFT_LAYER'] = '64'
# we disable the build-in solution library,
os.environ['ROCFFT_USE_EMPTY_SOL_MAP'] = '1'
# Do validation first to make it more robust:
# Our kernels might still have un-discovered bugs so we need to do that.
# Finding incorrect kernels also helps us improving kernels.
print("\n###############################################")
print("First, run validation to skip wrong solutions..")
print("###############################################")
skipValidation = arguments.validation == 0
if skipValidation:
print("\nAccuracy test is set to be skipped")
validate(arguments.validator, merging_meta_data, skipValidation)
RunRecord = {}
# First Run - reference
os.environ['ROCFFT_READ_SOL_MAP_FROM_FOLDER'] = str(reference_sol_folder)
print("\n###################################")
print("First run with reference solution..")
print("###################################\n")
print(
"The reference solution map file is {}...\n\n".format(reference_file))
if not reference_file.exists():
print("\tNot valid reference solution map file found! " \
"Will compare with the default plan/kernels in rocFFT\n")
for single_meta in merging_meta_data:
if not single_meta['Valid']:
continue
prob = single_meta['problem']
token, times, success, solToken, matchType = perflib.bench.run(
arguments.bench,
prob['length'],
direction=prob['direction'],
real=prob['real'],
inplace=prob['inplace'],
precision=str(prob['precision']),
nbatch=prob['nbatch'],
ntrial=20,
verbose=True)
times_str = ' '.join(str(t) for t in times)
print("\ntoken: " + token)
print("found root solution with key: " + solToken)
print("times: " + times_str + '\n')
print('---------------------------------------------------\n')
times = flatten(times)
RunRecord[token] = {'RefTimes': times, 'RefSolToken': solToken}
# Second Run - load new solution map from "ROCFFT_READ_EXPLICIT_SOL_MAP_FILE"
print("\n###################################")
print("Second run with new solution map...")
print("###################################\n")
for single_meta in merging_meta_data:
if not single_meta['Valid']:
continue
# get the <problem, out_solution_path> information
prob = single_meta['problem']
new_single_solution_file = Path(single_meta['outfile'])
# check the solution map file
print("The testing solution map file is {}...\n".format(
new_single_solution_file))
if not new_single_solution_file.exists():
print("\tNot valid solution map file found! Abort")
continue
# set the explicit solution map filepath
os.environ['ROCFFT_READ_EXPLICIT_SOL_MAP_FILE'] = str(
new_single_solution_file)
token, times, success, solToken, matchType = perflib.bench.run(
arguments.bench,
prob['length'],
direction=prob['direction'],
real=prob['real'],
inplace=prob['inplace'],
precision=str(prob['precision']),
nbatch=prob['nbatch'],
ntrial=20,
verbose=True)
times_str = ' '.join(str(t) for t in times)
print("\ntoken: " + token)
print("found root solution with key: " + solToken)
print("times: " + times_str + '\n')
print('---------------------------------------------------\n')
times = flatten(times)
RunRecord[token].update({
'NewTimes': times,
'NewSolToken': solToken,
'SolutionFile': new_single_solution_file
})
import scipy.stats
for token, result in RunRecord.items():
Avals = result['RefTimes']
Bvals = result['NewTimes']
result['RefTimes'] = statistics.median(Avals)
result['NewTimes'] = statistics.median(Bvals)
speedup = result['RefTimes'] / result['NewTimes']
_, pval, _, _ = scipy.stats.median_test(Avals, Bvals)
result['SpeedUp'] = speedup
result['Significance'] = pval
del os.environ['ROCFFT_USE_EMPTY_SOL_MAP']
del os.environ['ROCFFT_READ_SOL_MAP_FROM_FOLDER']
if 'ROCFFT_READ_EXPLICIT_SOL_MAP_FILE' in os.environ:
del os.environ['ROCFFT_READ_EXPLICIT_SOL_MAP_FILE']
NewSolutionsToInsert = []
InsertionInfo = {}
# Analysis Summary
print('\n==================\n[Merge Summaries]\n==================')
for single_meta in merging_meta_data:
if not single_meta['Valid']:
print('\n===================================================')
print("The solution in {}:".format(single_meta['outfile']))
print("\tFailed the accuracy test. Please be aware of this")
continue
for token, result in RunRecord.items():
# the problem is not contained in our new solution map, don't waste time comparing
# but it's possible RefSolToken is "" which means we didn't have the solution before
if result['NewSolToken'] == "":
continue
speed_up = result['SpeedUp']
signficance = result['Significance']
confident: bool = signficance <= 0.05
new_is_faster: bool = speed_up > 1.01 and confident
if new_is_faster:
InsertionInfo['arch'] = prefix
InsertionInfo['probToken'] = result['NewSolToken']
InsertionInfo['solutionFile'] = result['SolutionFile']
NewSolutionsToInsert.append(deepcopy(InsertionInfo))
print('\n===================================================')
print("For problem {}:\n".format(token))
print("\tReference solution: {}".format(result['RefSolToken']))
print("\tTime (Median): {} ms ...".format(result['RefTimes']))
print("\tNew solution: {} from file: {}".format(
result['NewSolToken'], result['SolutionFile']))
print("\tTime (Median): {} ms ...".format(result['NewTimes']))
print("\tSpeed-Up run2 over run1: {} / Pval: {} : {}, {}".format(
speed_up, signficance, 'FASTER' if speed_up > 1.0 else 'SLOWER',
'Confident' if confident else 'Not Confident'))
print("\t\t--> {} the new solution\n".format(
'PICK' if new_is_faster else 'DISCARD'))
output_merge_file(arguments.tuner, reference_file, NewSolutionsToInsert,
output_file)
#
# Main
#
def main():
parser = argparse.ArgumentParser(prog='rocfft-tuner')
subparsers = parser.add_subparsers(dest='command')
tuning_parser = subparsers.add_parser('tune', help='tune problems')
merge_parser = subparsers.add_parser('merge', help='merge solutions')
#################
# Shared Arguments
#################
# parser can add arguments shared by both tuner and merger
parser.add_argument('-w',
'--workspace',
type=str,
help='workspace folder keeping all the tuning data',
default="./TUNING_WORKSPACE")
parser.add_argument(
'--tuner',
type=str,
help='tuner executable path, used as tuner and merger',
default='./build/release/library/src/rocfft_offline_tuner')
parser.add_argument('--bench',
type=str,
help='rocfft-bench executable path, used when merging',
default='./build/release/clients/staging/rocfft-bench')
parser.add_argument('--validator',
type=str,
help='rocfft-test executable path, used when merging',
default='./build/release/clients/staging/rocfft-test')
#################
# TUNING COMMAND
#################
tuning_parser.add_argument('-s',
'--suite',
type=str,
help='suite',
action='append')
tuning_parser.add_argument(
'--dump',
help='dump the candidates data file in the workspace, default False',
action='store_true',
default=False)
tuning_parser.add_argument(
'--exact_token',
help=
'(Overwrite globally) output solution for the exact problem (using token including batch, stride, dist, offset), default False',
action='store_true',
default=False)
tuning_parser.add_argument(
'--print_reject',
help='print the configuration rejection reason, default False',
action='store_true',
default=False)
tuning_parser.add_argument(
'--max_wgs',
type=int,
help=
'(Overwrite globally) tuning max workgroups size to the specified value for ALL kernels.',
default=None)
tuning_parser.add_argument(
'--min_wgs',
type=int,
help=
'(Overwrite globally) tuning min workgroups size to the specified value for ALL kernels.',
default=None)
tuning_parser.add_argument(
'-i',
'--input',
type=str,
help='input of tuning meta file including suite, dump, exact setting',
default='')
#################
# MERGE COMMAND
#################
merge_parser.add_argument(
'--ref_sol_folder',
type=str,
help=
'folder of the original solution map data, default is [repo_folder]/solution_map/',
default='./solution_map')
merge_parser.add_argument(
'--outfolder',
type=str,
help=
'folder of the merged solution map data (under workspace), must specify',
default=None)
merge_parser.add_argument(
'--metafile',
type=str,
help='path of the merging meta file, must specify',
default=None)
merge_parser.add_argument(
'--validation',
type=int,
help='do validation test before merge, 1 for True/0 for False, default 1',
default=1)
arguments = parser.parse_args()
if arguments.command == 'tune':
command_tuning(arguments)
if arguments.command == 'merge':
command_merging(arguments)
sys.exit(0)
if __name__ == '__main__':
logging.basicConfig(filename='rocfft-tuner.log',
format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG)
console.setLevel(logging.WARNING)
console.setFormatter(logging.Formatter('%(levelname)-8s: %(message)s'))
logging.getLogger('').addHandler(console)
main()
|