Debian Codegraph @

tensilelite/Tensile/SolutionSelectionLibrary.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022-2025 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 Tensile.SolutionStructs.Naming import getSolutionNameMin, getKernelNameMin
26
27 import csv
28
29 def getSummationKeys(header):
30 keys=[]
31 for i in range(7, len(header)):
32 keystr = header[i].split("=")[1].strip()
33 key = int(keystr)
34 keys.append(key)
35 return keys
36
37 def makeKey(row):
38 key=row[3]
39 for i in range(4, 7):
40 key += "_%s" % row[i].strip()
41 return key
42
43 def getSolutionBaseKey (solution):
44
45 macroTile0 = solution["MacroTile0"]
46 macroTile1 = solution["MacroTile1"]
47 globalSplitU = solution["GlobalSplitU"]
48 localSplitU = solution["WorkGroup"][2]
49
50 key = "%s_%s_%s_%s" % (macroTile0, macroTile1, localSplitU, globalSplitU)
51
52 return key
53
54 def updateIfGT(theDictionary, theKey, theValue):
55 if not theKey in theDictionary:
56 theDictionary[theKey] = theValue
57 else:
58 theOldValue = theDictionary[theKey]
59 if theValue > theOldValue:
60 theDictionary[theKey] = theValue
61
62
63 def updateValidSolutions(validSolutions, analyzerSolutions):
64 solutionsStartIndex = len(analyzerSolutions)
65 validSelectionSolutionsIncluded = []
66 validSelectionSolutionsRemainder = []
67 selectionSolutionsIds = set([])
68 for validSelectionSolution in validSolutions:
69 (validSolution, validSolutionInfo) = validSelectionSolution
70 if validSolution in analyzerSolutions:
71 validExactSolutionIndex = analyzerSolutions.index(validSolution)
72 selectionSolutionsIds.add(validExactSolutionIndex)
73 validExactSolution = analyzerSolutions[validExactSolutionIndex]
74 validSelectionSolutionsIncluded.append((validExactSolution, validSolutionInfo))
75 else:
76 validSelectionSolutionsRemainder.append(validSelectionSolution)
77
78 selectionSolutions = []
79 for i in range(0 ,len(validSelectionSolutionsIncluded)):
80 validSelectionSolution = validSelectionSolutionsIncluded[i]
81 (validSolution, validSolutionInfo) = validSelectionSolution
82 validSolution["Ideals"] = validSolutionInfo
83 analyzerSolutions.append(validSolution)
84
85 solutionsStartIndex = len(analyzerSolutions)
86
87 for i in range(0, len(validSelectionSolutionsRemainder)):
88 validSelectionSolution = validSelectionSolutionsRemainder[i]
89 (validSolution, validSolutionInfo) = validSelectionSolution
90 selectionSolutionIndex = solutionsStartIndex + i
91 selectionSolutionsIds.add(selectionSolutionIndex)
92 splitGSU = False # this is a reminder that we need to add this in to the function signature
93 validSolution["SolutionNameMin"] = getSolutionNameMin(validSolution, splitGSU)
94 validSolution["KernelNameMin"] = getKernelNameMin(validSolution, splitGSU)
95 validSolution["Ideals"] = validSolutionInfo
96 selectionSolutions.append(validSolution)
97
98 selectionSolutionsIdsList = list(selectionSolutionsIds)
99
100 return selectionSolutionsIdsList
101
102
103 def analyzeSolutionSelection(problemType, selectionFileNameList, numSolutionsPerGroup, solutionGroupMap, solutionsList):
104
105 performanceMap = {}
106 solutionsHash = {}
107
108 totalIndices = problemType["TotalIndices"]
109 summationIndex = totalIndices
110 numIndices = totalIndices + problemType["NumIndicesLD"]
111 problemSizeStartIdx = 1
112 totalSizeIdx = problemSizeStartIdx + numIndices
113 solutionStartIdx = totalSizeIdx + 1
114 for fileIdx in range(0, len(selectionFileNameList)):
115 solutions = solutionsList[fileIdx]
116 selectionFileName = selectionFileNameList[fileIdx]
117 numSolutions = numSolutionsPerGroup[fileIdx]
118 rowLength = solutionStartIdx + numSolutions
119 solutionBaseKeys = []
120
121 for solution in solutions:
122 baseKey = getSolutionBaseKey(solution)
123 solutionBaseKeys.append(baseKey)
124
125 selectionfFile = open(selectionFileName, "r")
126 csvFile = csv.reader(selectionfFile)
127
128 firstRow = 0
129 for row in csvFile:
130 if firstRow == 0:
131 firstRow += 1
132 else:
133 sumationId = row[summationIndex].strip()
134
135 solutionIndex = 0
136 for i in range(solutionStartIdx, rowLength):
137 baseKey = solutionBaseKeys[solutionIndex]
138 key = "%s_%s" % (baseKey, sumationId)
139 solution = solutions[solutionIndex]
140 solutionIndex += 1
141 value = float(row[i])
142 if not solution in solutionsHash:
143 dataMap = {}
144 solutionsHash[solution] = dataMap
145
146 updateIfGT(solutionsHash[solution], sumationId, value)
147 if not key in performanceMap:
148 performanceMap[key] = (solution, value)
149 else:
150 _,valueOld = performanceMap[key]
151 if value > valueOld:
152 performanceMap[key] = (solution, value)
153
154
155 validSolutions = []
156 validSolutionSet = set([])
157
158 for key in performanceMap:
159 solution, _ = performanceMap[key]
160 validSolutionSet.add(solution)
161
162 for validSolution in validSolutionSet:
163 dataMap = solutionsHash[validSolution]
164 validSolutions.append((validSolution,dataMap))
165
166 return validSolutions