| 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 |
import functools |
| 27 |
|
| 28 |
from pathlib import Path |
| 29 |
from typing import List, Dict, NamedTuple |
| 30 |
|
| 31 |
from Tensile.Common import ParallelMap2, print1, IsaVersion, IsaInfo, setVerbosity |
| 32 |
from Tensile.Common.Architectures import SUPPORTED_ISA |
| 33 |
from Tensile.Common.Capabilities import makeIsaInfoMap |
| 34 |
from Tensile.Common.GlobalParameters import assignGlobalParameters |
| 35 |
from Tensile.LibraryIO import readYAML |
| 36 |
from Tensile.Toolchain.Validators import validateToolchain |
| 37 |
|
| 38 |
from .ParseArguments import parseArguments |
| 39 |
from .ValidMatrixInstruction import _validateMatrixInstruction |
| 40 |
from .ValidWorkGroup import _validateWorkGroup |
| 41 |
from .HandleCustomKernel import handleCustomKernel, hasCustomKernel |
| 42 |
|
| 43 |
|
| 44 |
class Check(NamedTuple): |
| 45 |
OnlyCustomKernels: bool |
| 46 |
All: bool |
| 47 |
|
| 48 |
|
| 49 |
def _runChecks( |
| 50 |
logicPath: Path, isaInfoMap: Dict[IsaVersion, IsaInfo], check: Check, files: List[Path] |
| 51 |
): |
| 52 |
""" |
| 53 |
Run checks on the given logic files. |
| 54 |
|
| 55 |
Args: |
| 56 |
logicPath: Path to a directory containing logic files or to an individual logic file. |
| 57 |
isaInfoMap: Map of IsaVersion to IsaInfo. |
| 58 |
check: Object containing flags for checking. |
| 59 |
files: List of logic files to check. |
| 60 |
|
| 61 |
Returns: |
| 62 |
Tuple of (keep, total) where keep is the number of unrejected solutions and |
| 63 |
total is the total number of solutions parsed. |
| 64 |
""" |
| 65 |
keep, total = 0, 0 |
| 66 |
for file in files: |
| 67 |
if "Experimental" in file.parts: |
| 68 |
return keep, total |
| 69 |
|
| 70 |
solutions = [] |
| 71 |
data = readYAML(file) |
| 72 |
problemType = data[4] |
| 73 |
if check.OnlyCustomKernels and hasCustomKernel(file): |
| 74 |
print1(f">> {file.relative_to(logicPath)}") |
| 75 |
solutions = data[5] |
| 76 |
elif check.All: |
| 77 |
print1(f">> {file.relative_to(logicPath)}") |
| 78 |
solutions = data[5] |
| 79 |
|
| 80 |
for s in solutions: |
| 81 |
s, isCustom = handleCustomKernel(s, isaInfoMap) |
| 82 |
if check.OnlyCustomKernels and not isCustom: |
| 83 |
continue |
| 84 |
|
| 85 |
s["ProblemType"] = problemType |
| 86 |
if all( |
| 87 |
[ |
| 88 |
_validateMatrixInstruction(s, isaInfoMap, file.relative_to(logicPath)), |
| 89 |
_validateWorkGroup(s, file.relative_to(logicPath)), |
| 90 |
] |
| 91 |
): |
| 92 |
keep += 1 |
| 93 |
total += 1 |
| 94 |
|
| 95 |
return keep, total |
| 96 |
|
| 97 |
|
| 98 |
def _setup(): |
| 99 |
args = parseArguments() |
| 100 |
|
| 101 |
setVerbosity(args.Verbose) |
| 102 |
jobs = int(args.Jobs) |
| 103 |
cxxCompiler = validateToolchain(args.CxxCompiler) |
| 104 |
logicPath = Path(args.LogicPath) |
| 105 |
|
| 106 |
if not any([args.CheckAll, args.CheckOnlyCustomKernels]): |
| 107 |
print1("No checks specified. Exiting.") |
| 108 |
exit(0) |
| 109 |
check = Check( |
| 110 |
OnlyCustomKernels=args.CheckOnlyCustomKernels, |
| 111 |
All=args.CheckAll, |
| 112 |
) |
| 113 |
|
| 114 |
if logicPath.is_file() and logicPath.suffix == ".yaml": |
| 115 |
files = [logicPath] |
| 116 |
else: |
| 117 |
pattern = "**/*.yaml" |
| 118 |
files = list(logicPath.glob(pattern)) |
| 119 |
if len(files) == 0: |
| 120 |
print1(f"No files found in {logicPath}") |
| 121 |
exit(1) |
| 122 |
print1(f"Found {len(files)} files") |
| 123 |
|
| 124 |
isaInfoMap = makeIsaInfoMap(SUPPORTED_ISA, str(cxxCompiler)) |
| 125 |
assignGlobalParameters({"PrintSolutionRejectionReason": True}, isaInfoMap) |
| 126 |
|
| 127 |
return jobs, isaInfoMap, logicPath, files, check |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
jobs, isaInfoMap, logicPath, files, check = _setup() |
| 132 |
|
| 133 |
batchSize = len(files) // min(len(files), jobs) |
| 134 |
batches = (files[i : i + batchSize] for i in range(0, len(files), batchSize)) |
| 135 |
|
| 136 |
fn = functools.partial(_runChecks, logicPath, isaInfoMap, check) |
| 137 |
keep, total = 0, 0 |
| 138 |
|
| 139 |
results = ParallelMap2(fn, batches, multiArg=False, procs=jobs, return_as="list") |
| 140 |
|
| 141 |
for _keep, _total in results: |
| 142 |
keep += _keep |
| 143 |
total += _total |
| 144 |
|
| 145 |
rejects = total - keep |
| 146 |
print(f"Total {total} solutions") |
| 147 |
print(f"Keep {keep} solutions") |
| 148 |
print(f"Reject {rejects} solutions") |
| 149 |
|
| 150 |
if rejects > 0: |
| 151 |
exit(1) |