Debian Codegraph @

tensilelite/Tensile/TensileClientConfig.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2016-2024 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
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: # Tensile Config file (first entry)
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: # Solution Selection "base" file
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: # Tensile config file (first entry)
76 sizeList = config["BenchmarkProblems"][0][1]["BenchmarkFinalParameters"][0]["ProblemSizes"]
77 except (TypeError, LookupError):
78 pass
79 else:
80 return sizeList
81
82 # check if whole config looks like a size list
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 # argument parsing
116 # yapf: disable
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 # yapf: enable
125
126 addCommonArguments(argParser)
127 args = argParser.parse_args(userArgs)
128
129 # loop through all configs and parse out all the data we can
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 # if we got data from the config, keep it
140 # if we already had that data, warn and exit
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) # TODO doesn't seem to work for range sizes
173
174 # update globals
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 # write output
184 ClientWriter.writeClientConfigIni(True, sizes, "", "", "", "", conProblemType, "", [], "", args.OutputConfig, None)
185
186
187 def main():
188 TensileClientConfig(sys.argv[1:])