| 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 |
from . import ClientWriter |
| 26 |
from . import LibraryIO |
| 27 |
from .Contractions import ProblemType as ContractionsProblemType |
| 28 |
from Tensile.SolutionStructs.Problem import ProblemType, ProblemSizes |
| 29 |
from .Common import globalParameters, print1, printExit, printWarning, assignGlobalParameters, \ |
| 30 |
restoreDefaultGlobalParameters, HR, __version__ |
| 31 |
from .Tensile import addCommonArguments, argUpdatedGlobalParameters |
| 32 |
|
| 33 |
import argparse |
| 34 |
import os |
| 35 |
import sys |
| 36 |
|
| 37 |
|
| 38 |
def getGlobalParams(config): |
| 39 |
"""Try to parse out global parameters from the places it could be""" |
| 40 |
globalParams = None |
| 41 |
try: |
| 42 |
globalParams = config["GlobalParameters"] |
| 43 |
except (TypeError, LookupError): |
| 44 |
pass |
| 45 |
|
| 46 |
return globalParams |
| 47 |
|
| 48 |
|
| 49 |
def getProblemDict(config): |
| 50 |
"""Try to parse out a problem type dict from the places it could be""" |
| 51 |
problemDict = None |
| 52 |
try: |
| 53 |
problemDict = config["BenchmarkProblems"][0][0] |
| 54 |
if len(config["BenchmarkProblems"]) > 1: |
| 55 |
printWarning("More than one BenchmarkProblem in config file: only using first") |
| 56 |
except (TypeError, LookupError): |
| 57 |
pass |
| 58 |
else: |
| 59 |
return problemDict |
| 60 |
|
| 61 |
try: |
| 62 |
problemDict = config["ProblemType"] |
| 63 |
except (TypeError, LookupError): |
| 64 |
pass |
| 65 |
|
| 66 |
return problemDict |
| 67 |
|
| 68 |
|
| 69 |
def getSizeList(config): |
| 70 |
""" |
| 71 |
Try to parse out a size list from the places it could be |
| 72 |
(including the whole config being the list) |
| 73 |
""" |
| 74 |
sizeList = None |
| 75 |
try: |
| 76 |
sizeList = config["BenchmarkProblems"][0][1]["BenchmarkFinalParameters"][0]["ProblemSizes"] |
| 77 |
except (TypeError, LookupError): |
| 78 |
pass |
| 79 |
else: |
| 80 |
return sizeList |
| 81 |
|
| 82 |
|
| 83 |
keep = True |
| 84 |
if type(config) is list: |
| 85 |
for i in config: |
| 86 |
if not (type(i) is dict and ("Exact" in i or "Range" in i)): |
| 87 |
keep = False |
| 88 |
break |
| 89 |
else: |
| 90 |
keep = False |
| 91 |
|
| 92 |
if keep: |
| 93 |
sizeList = config |
| 94 |
return sizeList |
| 95 |
|
| 96 |
|
| 97 |
def parseConfig(config): |
| 98 |
"""Parse out all data we can get from the config""" |
| 99 |
globalParams = getGlobalParams(config) |
| 100 |
problemDict = getProblemDict(config) |
| 101 |
sizeList = getSizeList(config) |
| 102 |
|
| 103 |
return (globalParams, problemDict, sizeList) |
| 104 |
|
| 105 |
|
| 106 |
def TensileClientConfig(userArgs): |
| 107 |
print1("") |
| 108 |
print1(HR) |
| 109 |
print1("#") |
| 110 |
print1("# Tensile Client Config v{}".format(__version__)) |
| 111 |
print1("#") |
| 112 |
print1(HR) |
| 113 |
print1("") |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
argParser = argparse.ArgumentParser() |
| 118 |
argParser.add_argument("ConfigYaml", type=os.path.realpath, nargs="+", |
| 119 |
help="Config yaml(s) containing parameters and problem and size information") |
| 120 |
argParser.add_argument("--output-config", "-o", dest="OutputConfig", type=os.path.realpath, |
| 121 |
required=True, help="Path to output resulting client config file") |
| 122 |
argParser.add_argument("--merge-sizes", dest="MergeSizes", action="store_true", |
| 123 |
help="Allow sizes from multiple config files") |
| 124 |
|
| 125 |
|
| 126 |
addCommonArguments(argParser) |
| 127 |
args = argParser.parse_args(userArgs) |
| 128 |
|
| 129 |
|
| 130 |
configs = [LibraryIO.read(x) for x in args.ConfigYaml] |
| 131 |
|
| 132 |
globalParams = {} |
| 133 |
problemDict = None |
| 134 |
sizeList = None |
| 135 |
|
| 136 |
for config in configs: |
| 137 |
(myGlobalParams, myProblemDict, mySizeList) = parseConfig(config) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if myGlobalParams is not None: |
| 142 |
if globalParams == {} or globalParams == myGlobalParams: |
| 143 |
globalParams = myGlobalParams |
| 144 |
else: |
| 145 |
printExit("Multiple definitions for GlobalParameters found:\n{}\nand\n{}".format( |
| 146 |
globalParams, myGlobalParams)) |
| 147 |
|
| 148 |
if myProblemDict is not None: |
| 149 |
if problemDict is None or problemDict == myProblemDict: |
| 150 |
problemDict = myProblemDict |
| 151 |
else: |
| 152 |
printExit("Multiple definitions for ProblemType found:\n{}\nand\n{}".format( |
| 153 |
problemDict, myProblemDict)) |
| 154 |
|
| 155 |
if mySizeList is not None: |
| 156 |
if sizeList is None or sizeList == mySizeList: |
| 157 |
sizeList = mySizeList |
| 158 |
elif args.MergeSizes: |
| 159 |
sizeList += mySizeList |
| 160 |
else: |
| 161 |
printExit("Multiple size lists found:\n{}\nand\n{}\n" |
| 162 |
"Run with --merge-sizes to keep all size lists found".format( |
| 163 |
sizeList, mySizeList)) |
| 164 |
|
| 165 |
if problemDict is None: |
| 166 |
printExit("No ProblemType found; cannot produce output") |
| 167 |
if sizeList is None: |
| 168 |
printExit("No SizeList found; cannot produce output") |
| 169 |
|
| 170 |
ssProblemType = ProblemType(problemDict) |
| 171 |
conProblemType = ContractionsProblemType.FromOriginalState(ssProblemType) |
| 172 |
sizes = ProblemSizes(ssProblemType, sizeList) |
| 173 |
|
| 174 |
|
| 175 |
restoreDefaultGlobalParameters() |
| 176 |
assignGlobalParameters(globalParams) |
| 177 |
|
| 178 |
overrideParameters = argUpdatedGlobalParameters(args) |
| 179 |
for key, value in overrideParameters.items(): |
| 180 |
print1("Overriding {0}={1}".format(key, value)) |
| 181 |
globalParameters[key] = value |
| 182 |
|
| 183 |
|
| 184 |
ClientWriter.writeClientConfigIni(True, sizes, "", "", "", "", conProblemType, "", [], "", args.OutputConfig, None) |
| 185 |
|
| 186 |
|
| 187 |
def main(): |
| 188 |
TensileClientConfig(sys.argv[1:]) |