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
|
# 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.
"""Bench launch utils."""
import logging
import pathlib
import re
import subprocess
import time
from perflib.utils import cjoin
import asyncio
import sys
from asyncio.subprocess import PIPE, STDOUT
def run(tuner,
length,
direction=-1,
real=False,
inplace=True,
precision='single',
nbatch=1,
ntrial=1,
device=None,
verbose=False,
timeout=10):
"""Run rocFFT tuner and return best solution"""
cmd = [pathlib.Path(tuner).resolve()]
if isinstance(length, int):
cmd += ['--length', length]
else:
cmd += ['--length'] + [cjoin([str(len) for len in length])]
cmd += ['-N', ntrial]
cmd += ['-b', nbatch]
if not inplace:
cmd += ['-o']
if precision == 'half':
cmd += ['--precision', 'half']
elif precision == 'single':
cmd += ['--precision', 'single']
elif precision == 'double':
cmd += ['--precision', 'double']
if device is not None:
cmd += ['--device', device]
if real:
if direction == -1:
cmd += ['-t', 2, '--itype', 2, '--otype', 3]
if direction == 1:
cmd += ['-t', 3, '--itype', 3, '--otype', 2]
else:
if direction == -1:
cmd += ['-t', 0]
if direction == 1:
cmd += ['-t', 1]
cmd = [str(x) for x in cmd]
logging.info('tunning: ' + ' '.join(cmd))
if verbose:
print('tunning: ' + ' '.join(cmd))
tokenToken = "Token: "
outFileToken = "[OUTPUT_FILE]: "
resultToken = "[Result]: "
token = ""
outFileName = ""
msg = "[Solution]:\n"
async def run_command(*args, timeout=None):
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE)
nonlocal token
nonlocal outFileName
nonlocal msg
while True:
try:
line = await asyncio.wait_for(process.stdout.readline(),
timeout)
except asyncio.TimeoutError:
logging.info(
"timeout expired. killed. Please check the process.")
print("timeout expired. killed. Please check the process.")
process.kill() # Timeout or some criterion is not satisfied
break
if not line:
break
else:
line = line.decode('utf-8').rstrip('\n')
print(line)
if line.startswith(tokenToken):
token = line[len(tokenToken):]
elif line.startswith(outFileToken):
outFileName = line[len(outFileToken):]
elif line.startswith(resultToken):
msg += line[len(resultToken):] + '\n'
return await process.wait() # Wait for the child process to exit
if sys.platform == "win32":
loop = asyncio.ProactorEventLoop() # For subprocess' pipes on Windows
asyncio.set_event_loop(loop)
else:
loop = asyncio.new_event_loop()
returncode = loop.run_until_complete(run_command(*cmd, timeout=10))
success = returncode == 0
loop.close()
return token, outFileName, msg, success
def accuracy_test(validator,
length,
direction=-1,
real=False,
inplace=True,
precision='single',
nbatch=1,
token=None,
timeout=10):
"""Run rocFFT test."""
cmd = [pathlib.Path(validator).resolve()]
cmd += ['--gtest_filter=man*']
# use token if we have it
if token != None:
cmd += ['--token', token]
# else, specify each arg
else:
if isinstance(length, int):
cmd += ['--length', length]
else:
cmd += ['--length'] + list(length)
cmd += ['-b', nbatch]
if not inplace:
cmd += ['-o']
if precision == 'half':
cmd += ['--precision', 'half']
elif precision == 'single':
cmd += ['--precision', 'single']
elif precision == 'double':
cmd += ['--precision', 'double']
if real:
if direction == -1:
cmd += ['-t', 2, '--itype', 2, '--otype', 3]
if direction == 1:
cmd += ['-t', 3, '--itype', 3, '--otype', 2]
else:
if direction == -1:
cmd += ['-t', 0]
if direction == 1:
cmd += ['-t', 1]
cmd = [str(x) for x in cmd]
logging.info('accuracy testing: ' + ' '.join(cmd))
print('accuracy testing: ' + ' '.join(cmd))
passToken = "[ PASSED ] 1 test"
passed = False
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in proc.stdout:
line = line.decode('utf-8').rstrip('\n')
if line.startswith(passToken):
print(line)
passed = True
try:
proc.wait(timeout=None if timeout == 0 else timeout)
except subprocess.TimeoutExpired:
logging.info("timeout expired. killed. Please check the process.")
proc.kill()
success = proc.returncode == 0
if not success:
print('[ FAILED ]: ' + ' '.join(cmd))
return success
def merge(merger,
base_file_path,
new_files,
new_probTokens,
out_file_path,
verbose=False,
timeout=30):
"""Run rocFFT tuner with command merge"""
cmd = [pathlib.Path(merger).resolve()]
cmd += ['--command', '1']
cmd += ['--new_sol_file', str(new_files)]
cmd += ['--new_probkey', str(new_probTokens)]
cmd += ['--output_sol_file', str(out_file_path)]
if base_file_path is not None:
cmd += ['--base_sol_file', str(base_file_path)]
cmd = [str(x) for x in cmd]
logging.info('merging: ' + ' '.join(cmd))
if verbose:
print('merging: ' + ' '.join(cmd))
# cpp merger simply return code, so no need to capture msg
# but since the merger has some recursive operation on tree,
# so using wait is still good to prevent any infinity loop bug..
proc = subprocess.Popen(cmd)
try:
proc.wait(timeout=None if timeout == 0 else timeout)
except subprocess.TimeoutExpired:
logging.info("timeout expired. killed. Please check the process.")
proc.kill()
success = proc.returncode == 0
if not success:
print('Failed on merging:' + ' '.join(cmd))
return success
|