Debian Codegraph @

tensilelite/Tensile/Utilities/archive/merge_rocblas_yaml_files.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022 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 __future__ import print_function
26 import os
27 import sys
28 import argparse
29
30 HR = "################################################################################"
31
32 ################################################################################
33 # Print Debug
34 ################################################################################
35
36 def printWarning(message):
37 print("Tensile::WARNING: %s" % message)
38 sys.stdout.flush()
39
40 def printExit(message):
41 print("Tensile::FATAL: %s" % message)
42 sys.stdout.flush()
43 sys.exit(-1)
44
45 try:
46 import yaml
47 except ImportError:
48 printExit("You must install PyYAML to use Tensile (to parse config files). See http://pyyaml.org/wiki/PyYAML for installation instructions.")
49
50 def ensurePath( path ):
51 if not os.path.exists(path):
52 os.makedirs(path)
53 return path
54
55 ################################################################################
56 # Library Logic Container
57 ################################################################################
58 class LibraryLogic:
59
60 def __init__(self,filename=None):
61
62 if filename is not None:
63 print ("# Reading Library Logic: " + filename)
64 try:
65 stream = open(filename, "r")
66 except IOError:
67 printExit("Cannot open file: %s" % filename )
68 data = yaml.load(stream, yaml.SafeLoader)
69
70 if isinstance(data, list):
71
72 length = len(data)
73
74 if (length > 0):
75 self.__set_versionString(data[0]["MinimumRequiredVersion"])
76 else:
77 self.__set_versionString(None)
78
79 if (length > 1):
80 self.__set_scheduleName(data[1])
81 else:
82 self.__set_scheduleName(None)
83
84 if (length > 2):
85 self.__set_architectureName(data[2])
86 else:
87 self.__set_architectureName(None)
88
89 if (length > 3):
90 self.__set_deviceNames(data[3])
91 else:
92 self.__set_deviceNames(None)
93
94 if (length > 4):
95 self.__set_problemType(data[4])
96 else:
97 self.__set_problemType(None)
98
99 if (length > 5):
100 self.__set_solutionStates(data[5])
101 else:
102 self.__set_solutionStates(None)
103
104 if (length > 6):
105 self.__set_indexOrder(data[6])
106 else:
107 self.__set_indexOrder(None)
108
109 if (length > 7):
110 exactData = data[7]
111 exactList = list()
112 for exact in exactData:
113 size = exact[0]
114 if (len(size) > 4):
115 exactOut = [size[:4],exact[1]]
116 exactList.append(exactOut)
117 else:
118 exactList.append(exact)
119 self.__set_exactLogic(exactList)
120 else:
121 self.__set_exactLogic(None)
122
123 if (length > 8):
124 self.__set_rangeLogic(data[8])
125 else:
126 self.__set_rangeLogic(None)
127
128 else:
129 printExit("Invalid Logic file: %s" % filename)
130
131 stream.close()
132
133 else:
134 self.__set_versionString(None)
135 self.__set_scheduleName(None)
136 self.__set_architectureName(None)
137 self.__set_deviceNames(None)
138 self.__set_problemType(None)
139 self.__set_solutionStates(None)
140 self.__set_indexOrder(None)
141 self.__set_exactLogic(None)
142 self.__set_rangeLogic(None)
143
144 #versionString
145 def __get_versionString(self):
146 return self.__versionString
147
148 def __set_versionString(self,value):
149 self.__versionString = value
150
151 versionString = property(__get_versionString,__set_versionString)
152
153 #scheduleName
154 def __get_scheduleName(self):
155 return self.__scheduleName
156
157 def __set_scheduleName(self, value):
158 self.__scheduleName = value
159
160 scheduleName = property(__get_scheduleName,__set_scheduleName)
161
162 #architectureName
163 def __get_architectureName(self):
164 return self.__architectureName
165
166 def __set_architectureName(self,value):
167 self.__architectureName = value
168
169 architectureName = property(__get_architectureName,__set_architectureName)
170
171 #deviceNames
172 def __get_deviceNames(self):
173 return self.__deviceNames
174
175 def __set_deviceNames(self,value):
176 self.__deviceNames = value
177
178 deviceNames = property(__get_deviceNames,__set_deviceNames)
179
180
181 #problemTypeState
182 def __get_problemType(self):
183 return self.__problemType
184
185 def __set_problemType(self,value):
186 self.__problemType = value
187
188 problemType = property(__get_problemType,__set_problemType)
189
190 #solutionStates
191 def __get_solutionStates(self):
192 return self.__solutionStates
193
194 def __set_solutionStates(self,value):
195 self.__solutionStates = value
196
197 solutionStates = property(__get_solutionStates,__set_solutionStates)
198
199 #indexOrder
200 def __get_indexOrder(self):
201 return self.__indexOrder
202
203 def __set_indexOrder(self,value):
204 self.__indexOrder = value
205
206 indexOrder = property(__get_indexOrder,__set_indexOrder)
207
208
209 #exactLogic
210 def __get_exactLogic(self):
211 return self.__exactLogic
212
213 def __set_exactLogic(self,value):
214 self.__exactLogic = value
215
216 exactLogic = property(__get_exactLogic,__set_exactLogic)
217
218 #rangeLogic
219 def __get_rangeLogic(self):
220 return self.__rangeLogic
221
222 def __set_rangeLogic(self,value):
223 self.__rangeLogic = value
224
225 rangeLogic = property(__get_rangeLogic,__set_rangeLogic)
226
227 def writeLibraryLogic(self,filename):
228
229 data = []
230
231 data.append({"MinimumRequiredVersion":self.versionString})
232 data.append(self.scheduleName)
233 data.append(self.architectureName)
234 data.append(self.deviceNames)
235 data.append(self.problemType)
236 data.append(self.solutionStates)
237 data.append(self.indexOrder)
238 data.append(self.exactLogic)
239 data.append(self.rangeLogic)
240
241 if not data:
242 printExit("No data to output")
243 else:
244 try:
245 stream = open(filename, "w")
246 yaml.safe_dump(data, stream, default_flow_style=None)
247 stream.close()
248 except IOError:
249 printExit("Cannot open file: %s" % filename)
250
251
252 def MergeTensileLogicFiles(origionalLibraryLogic, exactLibraryLogic):
253
254 mergedLibraryLogic = LibraryLogic()
255
256 solutionList = origionalLibraryLogic.solutionStates
257 solutionListExact = exactLibraryLogic.solutionStates
258
259 indexKey = "SolutionIndex"
260 # zero out solution indexes
261 for solution in solutionList:
262 solution[indexKey] = 0
263
264 for solution in solutionListExact:
265 solution[indexKey] = 0
266
267 newSolutionOffset = len(solutionList)
268
269 filterdSolutionExactList = []
270 replicationMapping = {}
271 idx = 0
272 idxMapping = newSolutionOffset
273
274 mergedSolutionList = []
275 for solution in solutionList:
276 mergedSolutionList.append(solution)
277
278 # construct the mappings from the old exact kernal configurations
279 # to their definitions in the merged files
280 for solution in solutionListExact:
281 if solution in solutionList:
282 # if solution exists in the origional configuration the
283 # its placement in the merged kernel configurations list
284 # gets mapped to the pre-existing configuration
285 idxOrg = solutionList.index(solution)
286 replicationMapping[idx] = idxOrg
287
288 else:
289 filterdSolutionExactList.append(solution)
290 # if the solution does not exist in the origional configurations
291 # it gets mapped to the new offset
292 replicationMapping[idx] = idxMapping
293 mergedSolutionList.append(solution)
294 idxMapping += 1
295
296 idx += 1
297
298 exactLogic = origionalLibraryLogic.exactLogic
299 exactLogicExact = exactLibraryLogic.exactLogic
300
301 filteredExactLogicExact = []
302
303 # use the mapping from above to remap the exact logic
304 # in the merged file
305 for exact in exactLogicExact:
306 # example exact entry [[123,124,1,123], [5, 4312.3]]
307 # the first fiedl in [5, 4312.3] is the mapping to the
308 # kernel configuration
309 kernelIndex = exact[1][0]
310
311 if kernelIndex in replicationMapping:
312 exact[1][0] = replicationMapping[kernelIndex]
313
314 filteredExactLogicExact.append(exact)
315
316 sizeList, _ = zip(*filteredExactLogicExact)
317
318 mergedExactLogic = []
319 for logicMapping in exactLogic:
320 if logicMapping[0] not in sizeList:
321 mergedExactLogic.append(logicMapping)
322
323 for logicMapping in filteredExactLogicExact:
324 mergedExactLogic.append(logicMapping)
325
326 # re index solutions
327 index = 0
328 for solution in mergedSolutionList:
329 solution[indexKey] = index
330 index += 1
331
332
333 mergedLibraryLogic.versionString = origionalLibraryLogic.versionString
334 mergedLibraryLogic.scheduleName = origionalLibraryLogic.scheduleName
335 mergedLibraryLogic.architectureName = origionalLibraryLogic.architectureName
336 mergedLibraryLogic.deviceNames = origionalLibraryLogic.deviceNames
337 mergedLibraryLogic.problemType = origionalLibraryLogic.problemType
338 mergedLibraryLogic.solutionStates = mergedSolutionList
339 mergedLibraryLogic.indexOrder = origionalLibraryLogic.indexOrder
340 mergedLibraryLogic.exactLogic = mergedExactLogic
341 mergedLibraryLogic.rangeLogic = origionalLibraryLogic.rangeLogic
342
343 return mergedLibraryLogic
344
345
346 def ProcessMergeLogicFile(exactFileName, origionalFileName, outputFileName):
347
348 _, fileName = os.path.split(exactFileName)
349
350 print ("processing file: " + fileName)
351
352 libraryLogic = LibraryLogic(origionalFileName)
353 libraryLogicExact = LibraryLogic(exactFileName)
354
355 mergedLibraryLogic = MergeTensileLogicFiles(libraryLogic,libraryLogicExact)
356
357 mergedLibraryLogic.writeLibraryLogic(outputFileName)
358
359 def RunMergeTensileLogicFiles():
360
361 print("")
362 print(HR)
363 print("# Merge Library Logic")
364 print(HR)
365 print("")
366
367 ##############################################################################
368 # Parse Command Line Arguments
369 ##############################################################################
370
371 argParser = argparse.ArgumentParser()
372 argParser.add_argument("OrigionalLogicPath", help="Path to the origional LibraryLogic.yaml input files.")
373 argParser.add_argument("ExactLogicPath", help="Path to the exact LibraryLogic.yaml input files.")
374 argParser.add_argument("OutputPath", help="Where to write library files?")
375
376 args = argParser.parse_args()
377
378 origionalLogicPath = args.OrigionalLogicPath
379 exactLogicPath = args.ExactLogicPath
380 outputPath = args.OutputPath
381 print ("Origional Logic Path: " + origionalLogicPath)
382 print ("Exact Logic Path: " + exactLogicPath)
383 print ("OutputPath: " + outputPath)
384
385 print("")
386 ensurePath(outputPath)
387 if not os.path.exists(exactLogicPath):
388 printExit("LogicPath %s doesn't exist" % exactLogicPath)
389
390 exactLogicFiles = [os.path.join(exactLogicPath, f) for f in os.listdir(exactLogicPath) \
391 if (os.path.isfile(os.path.join(exactLogicPath, f)) \
392 and os.path.splitext(f)[1]==".yaml")]
393
394 for exactLogicFilePath in exactLogicFiles:
395 _, fileName = os.path.split(exactLogicFilePath)
396
397 origionalLogicFilePath = os.path.join(origionalLogicPath, fileName)
398 if os.path.isfile(origionalLogicFilePath):
399
400 outputLogicFilePath = os.path.join(outputPath, fileName)
401
402 try:
403 ProcessMergeLogicFile(exactLogicFilePath, origionalLogicFilePath, outputLogicFilePath)
404 except Exception as ex:
405 print("Exception: {0}".format(ex))
406
407 else:
408 print ("# file does not exist in origional directory " + origionalLogicFilePath)
409
410
411 ################################################################################
412 # Main
413 ################################################################################
414 if __name__ == "__main__":
415 RunMergeTensileLogicFiles()