| 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 |
import functools |
| 26 |
import math |
| 27 |
import os |
| 28 |
import sys |
| 29 |
import time |
| 30 |
import re |
| 31 |
|
| 32 |
from inspect import currentframe, getframeinfo |
| 33 |
from copy import deepcopy |
| 34 |
from enum import Enum |
| 35 |
from math import log |
| 36 |
from pathlib import Path |
| 37 |
|
| 38 |
from Tensile import __version__ |
| 39 |
|
| 40 |
from rocisa import rocIsa |
| 41 |
|
| 42 |
import pickle |
| 43 |
|
| 44 |
def fastdeepcopy(x): |
| 45 |
|
| 46 |
return pickle.loads(pickle.dumps(x)) |
| 47 |
|
| 48 |
|
| 49 |
_global_ti = rocIsa.getInstance() |
| 50 |
|
| 51 |
_verbosity = 1 |
| 52 |
|
| 53 |
def setVerbosity(v: int): |
| 54 |
global _verbosity |
| 55 |
_verbosity = v |
| 56 |
|
| 57 |
def getVerbosity(): |
| 58 |
return _verbosity |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
def print1(message): |
| 67 |
if getVerbosity() >= 1: |
| 68 |
print(message) |
| 69 |
sys.stdout.flush() |
| 70 |
|
| 71 |
|
| 72 |
def print2(message): |
| 73 |
if getVerbosity() >= 2: |
| 74 |
print(message) |
| 75 |
sys.stdout.flush() |
| 76 |
|
| 77 |
|
| 78 |
def printWarning(message): |
| 79 |
print("Tensile::WARNING: %s" % message) |
| 80 |
sys.stdout.flush() |
| 81 |
|
| 82 |
|
| 83 |
def printExit(message): |
| 84 |
print("Tensile::FATAL: %s" % message) |
| 85 |
sys.stdout.flush() |
| 86 |
sys.exit(-1) |
| 87 |
|
| 88 |
|
| 89 |
def hasParam(name, structure): |
| 90 |
if isinstance(structure, list): |
| 91 |
for l in structure: |
| 92 |
if hasParam(name, l): |
| 93 |
return True |
| 94 |
return False |
| 95 |
elif isinstance(structure, dict): |
| 96 |
return name in structure |
| 97 |
else: |
| 98 |
return name == structure |
| 99 |
|
| 100 |
|
| 101 |
def isExe(filePath): |
| 102 |
return os.path.isfile(filePath) and os.access(filePath, os.X_OK) |
| 103 |
|
| 104 |
|
| 105 |
def locateExe(defaultPath, exeName): |
| 106 |
|
| 107 |
if defaultPath: |
| 108 |
exePath = os.path.join(defaultPath, exeName) |
| 109 |
if isExe(exePath): |
| 110 |
return exePath |
| 111 |
|
| 112 |
for path in os.environ["PATH"].split(os.pathsep): |
| 113 |
exePath = os.path.join(path, exeName) |
| 114 |
if isExe(exePath): |
| 115 |
return exePath |
| 116 |
|
| 117 |
raise OSError(f"Failed to locate {exeName}") |
| 118 |
|
| 119 |
|
| 120 |
def ensurePath(path): |
| 121 |
try: |
| 122 |
os.makedirs(path) |
| 123 |
except FileExistsError: |
| 124 |
pass |
| 125 |
except OSError: |
| 126 |
raise OSError('Failed to create directory "%s" ' % (path)) |
| 127 |
return path |
| 128 |
|
| 129 |
|
| 130 |
def roundUp(f): |
| 131 |
return (int)(math.ceil(f)) |
| 132 |
|
| 133 |
|
| 134 |
def elineno(): |
| 135 |
""" |
| 136 |
Return the file name and line number of the caller. |
| 137 |
""" |
| 138 |
frame = getframeinfo(currentframe().f_back) |
| 139 |
return f"{Path(frame.filename).name}:{frame.lineno}" |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
def versionIsCompatible(queryVersionString): |
| 148 |
(qMajor, qMinor, qStep) = queryVersionString.split(".") |
| 149 |
(tMajor, tMinor, tStep) = __version__.split(".") |
| 150 |
|
| 151 |
|
| 152 |
if qMajor != tMajor: |
| 153 |
return False |
| 154 |
|
| 155 |
|
| 156 |
if int(qMinor) > int(tMinor): |
| 157 |
return False |
| 158 |
if qMinor == tMinor: |
| 159 |
if int(qStep) > int(tStep): |
| 160 |
return False |
| 161 |
return True |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
class ProgressBar: |
| 169 |
def __init__(self, maxValue, width=80): |
| 170 |
self.char = "|" |
| 171 |
self.maxValue = maxValue |
| 172 |
self.width = width |
| 173 |
self.maxTicks = self.width - 7 |
| 174 |
|
| 175 |
self.priorValue = 0 |
| 176 |
self.fraction = 0 |
| 177 |
self.numTicks = 0 |
| 178 |
self.createTime = time.time() |
| 179 |
|
| 180 |
def increment(self, value=1): |
| 181 |
self.update(self.priorValue + value) |
| 182 |
|
| 183 |
def update(self, value): |
| 184 |
currentFraction = 1.0 * value / self.maxValue |
| 185 |
currentNumTicks = int(currentFraction * self.maxTicks) |
| 186 |
if currentNumTicks > self.numTicks: |
| 187 |
self.numTicks = currentNumTicks |
| 188 |
self.fraction = currentFraction |
| 189 |
self.printStatus() |
| 190 |
self.priorValue = value |
| 191 |
|
| 192 |
def printStatus(self): |
| 193 |
sys.stdout.write("\r") |
| 194 |
sys.stdout.write( |
| 195 |
"[%-*s] %3d%%" % (self.maxTicks, self.char * self.numTicks, self.fraction * 100) |
| 196 |
) |
| 197 |
if self.numTicks == self.maxTicks: |
| 198 |
stopTime = time.time() |
| 199 |
sys.stdout.write(" (%-.1f secs elapsed)\n" % (stopTime - self.createTime)) |
| 200 |
sys.stdout.flush() |
| 201 |
|
| 202 |
def finish(self): |
| 203 |
pass |
| 204 |
|
| 205 |
|
| 206 |
class DataDirection(Enum): |
| 207 |
NONE = (0,) |
| 208 |
READ = (1,) |
| 209 |
WRITE = 2 |
| 210 |
|
| 211 |
|
| 212 |
class SpinnyThing: |
| 213 |
def __init__(self): |
| 214 |
self.chars = ["|", "/", "-", "\\"] |
| 215 |
self.index = 0 |
| 216 |
|
| 217 |
def increment(self, value=1): |
| 218 |
sys.stdout.write("\b" + self.chars[self.index]) |
| 219 |
sys.stdout.flush() |
| 220 |
self.index = (self.index + 1) % len(self.chars) |
| 221 |
|
| 222 |
def finish(self): |
| 223 |
sys.stdout.write("\b*\n") |
| 224 |
sys.stdout.flush() |
| 225 |
|
| 226 |
|
| 227 |
def iterate_progress(obj, *args, **kwargs): |
| 228 |
try: |
| 229 |
progress = ProgressBar(len(obj)) |
| 230 |
except TypeError: |
| 231 |
progress = SpinnyThing() |
| 232 |
for o in obj: |
| 233 |
yield o |
| 234 |
progress.increment() |
| 235 |
progress.finish() |
| 236 |
|
| 237 |
|
| 238 |
try: |
| 239 |
from tqdm import tqdm |
| 240 |
except ImportError: |
| 241 |
tqdm = iterate_progress |
| 242 |
|
| 243 |
|
| 244 |
def state(obj): |
| 245 |
if hasattr(obj, "state"): |
| 246 |
return obj.state() |
| 247 |
|
| 248 |
if hasattr(obj.__class__, "StateKeys"): |
| 249 |
rv = {} |
| 250 |
for key in obj.__class__.StateKeys: |
| 251 |
attr = key |
| 252 |
if isinstance(key, tuple): |
| 253 |
(key, attr) = key |
| 254 |
rv[key] = state(getattr(obj, attr)) |
| 255 |
return rv |
| 256 |
|
| 257 |
if isinstance(obj, dict): |
| 258 |
return {k: state(v) for k, v in obj.items()} |
| 259 |
|
| 260 |
if isinstance(obj, (str, int, float)): |
| 261 |
return obj |
| 262 |
|
| 263 |
try: |
| 264 |
return [state(i) for i in obj] |
| 265 |
except TypeError: |
| 266 |
pass |
| 267 |
|
| 268 |
return obj |
| 269 |
|
| 270 |
|
| 271 |
def state_key_ordering(cls): |
| 272 |
def tup(obj): |
| 273 |
return tuple([getattr(obj, k) for k in cls.StateKeys]) |
| 274 |
|
| 275 |
def lt(a, b): |
| 276 |
return tup(a) < tup(b) |
| 277 |
|
| 278 |
def eq(a, b): |
| 279 |
return tup(a) == tup(b) |
| 280 |
|
| 281 |
cls.__lt__ = lt |
| 282 |
cls.__eq__ = eq |
| 283 |
|
| 284 |
return functools.total_ordering(cls) |
| 285 |
|
| 286 |
|
| 287 |
def hash_combine(*objs, **kwargs): |
| 288 |
shift = 1 |
| 289 |
if "shift" in kwargs: |
| 290 |
shift = kwargs["shift"] |
| 291 |
|
| 292 |
if len(objs) == 1: |
| 293 |
objs = objs[0] |
| 294 |
|
| 295 |
rv = 0 |
| 296 |
try: |
| 297 |
it = iter(objs) |
| 298 |
rv = next(it) |
| 299 |
for value in it: |
| 300 |
rv = (rv << shift) ^ value |
| 301 |
except TypeError: |
| 302 |
return objs |
| 303 |
except StopIteration: |
| 304 |
pass |
| 305 |
return rv |
| 306 |
|
| 307 |
|
| 308 |
def hash_objs(*objs, **kwargs): |
| 309 |
return hash(tuple(objs)) |
| 310 |
|
| 311 |
|
| 312 |
def ClientExecutionLock(lockPath: str): |
| 313 |
if not lockPath: |
| 314 |
return open(os.devnull) |
| 315 |
|
| 316 |
import filelock |
| 317 |
|
| 318 |
return filelock.FileLock(lockPath) |
| 319 |
|
| 320 |
|
| 321 |
def assignParameterWithDefault(destinationDictionary, key, sourceDictionary, defaultDictionary): |
| 322 |
if key in sourceDictionary: |
| 323 |
destinationDictionary[key] = deepcopy(sourceDictionary[key]) |
| 324 |
else: |
| 325 |
destinationDictionary[key] = deepcopy(defaultDictionary[key]) |
| 326 |
|
| 327 |
|
| 328 |
def isRhel8() -> bool: |
| 329 |
""" |
| 330 |
Check if the current OS is Red Hat Enterprise Linux 8 by reading the /etc/os-release file. |
| 331 |
|
| 332 |
Returns: |
| 333 |
True if the current OS is RHEL 8, False otherwise |
| 334 |
""" |
| 335 |
file = Path("/etc/os-release") |
| 336 |
pattern = r'NAME="Red Hat Enterprise Linux".*VERSION_ID="8\.\d+"' |
| 337 |
if not file.exists(): |
| 338 |
return False |
| 339 |
with open(file, "r") as f: |
| 340 |
content = f.read() |
| 341 |
match = re.search(pattern, content, re.DOTALL) |
| 342 |
if match: |
| 343 |
printWarning("Rhel8 environments may not support all tools for system queries such as rocm-smi.") |
| 344 |
return True |
| 345 |
return False |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
def log2(x): |
| 352 |
return int(log(x, 2) + 0.5) |
| 353 |
|
| 354 |
def ceilDivide(numerator, denominator): |
| 355 |
|
| 356 |
|
| 357 |
try: |
| 358 |
if numerator < 0 or denominator < 0: |
| 359 |
raise ValueError |
| 360 |
except ValueError: |
| 361 |
print("ERROR: Can't have a negative register value") |
| 362 |
return 0 |
| 363 |
try: |
| 364 |
div = int((numerator+denominator-1) // denominator) |
| 365 |
except ZeroDivisionError: |
| 366 |
print("ERROR: Divide by 0") |
| 367 |
return 0 |
| 368 |
return div |
| 369 |
|
| 370 |
def roundUpToNearestMultiple(numerator, denominator): |
| 371 |
return ceilDivide(numerator,denominator)*int(denominator) |