Debian Codegraph @

tensilelite/Tensile/BenchmarkSplitter.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 import os
26 import copy
27 import yaml
28 import math
29
30 class BenchmarkSplitter(object):
31
32 """
33 Benchmark splitter class
34 Loads in a benchmark yaml file and splits
35 it into several smaller benchmarks limited
36 by a number of problem sizes entries.
37 """
38
39 @staticmethod
40 def __readConfigFile(benchmarkConfigFile):
41 with open(benchmarkConfigFile) as f:
42 data = yaml.safe_load(f)
43 return data
44
45 # data: a loaded .yaml file
46 # returns: a list of yaml files that
47 # are differentiated by the benchmark problem
48 @staticmethod
49 def __splitByProblem(data):
50 rv = []
51 problemKey = "BenchmarkProblems"
52 for i in range(len(data[problemKey])):
53 result = {}
54 for k in data.keys():
55 if k == problemKey:
56 result[k] = [copy.deepcopy(data[k][i])]
57 else:
58 result[k] = copy.deepcopy(data[k])
59 rv.append(result)
60 return rv
61
62 # data: a loaded .yaml file, containing one benchmark problem section
63 # returns: a list of yaml files that are differentiated by the
64 # benchmark groupings
65 @staticmethod
66 def __splitByBenchmarkGroup(data):
67 rv = []
68 problemKey = "BenchmarkProblems"
69
70 assert len(data[problemKey]) == 1, "Config file must have one BenchmarkProblems group"
71
72 benchmarkProblems = data[problemKey][0]
73
74 # Find the index of the problem type group
75 problemIdx = -1
76 for i in range(len(benchmarkProblems)):
77 if("OperationType" in benchmarkProblems[i].keys()):
78 problemIdx = i
79 break
80
81 assert problemIdx != -1, "Could not find problem type group"
82
83 # Split files on the benchmark group
84 for i in range(len(benchmarkProblems)):
85 if i != problemIdx:
86 result = {}
87 for k in data.keys():
88 if k == problemKey:
89 # Take only the problem group and one benchmarkgroup
90 result[k] = [[copy.deepcopy(benchmarkProblems[problemIdx]), \
91 copy.deepcopy(benchmarkProblems[i])]]
92 else:
93 # copy other sections verbatim
94 result[k] = copy.deepcopy(data[k])
95 rv.append(result)
96 return rv
97
98 # data: a loaded .yaml file, containing one benchmark problem section
99 # and one benchmark group section
100 # returns: a list of yaml files that are differentiated by the
101 # benchmark sizes
102 @staticmethod
103 def __splitByBenchmarkSizes(data, numSizes=1):
104 rv = []
105 problemKey = "BenchmarkProblems"
106
107 assert len(data[problemKey]) == 1, "Config file must have one BenchmarkProblems group"
108
109 benchmarkProblems = data[problemKey][0]
110
111 # Find the index of the problem type group
112 # and the benchmark group
113 problemIdx = -1
114 benchmarkIdx = -1
115 for i in range(len(benchmarkProblems)):
116 groupKeys = benchmarkProblems[i].keys()
117 if("OperationType" in groupKeys):
118 problemIdx = i
119 elif("BenchmarkFinalParameters" in groupKeys):
120 benchmarkIdx = i
121
122 assert len(benchmarkProblems) == 2 \
123 and problemIdx is not -1 \
124 and benchmarkIdx is not -1, \
125 "Config file must have one ProblemType group and one Benchmark group"
126
127 # Grab the problem sizes from the Benchmark group
128 benchmarkGroup = benchmarkProblems[benchmarkIdx]
129 assert "ProblemSizes" in benchmarkGroup["BenchmarkFinalParameters"][0] \
130 and len(benchmarkGroup["BenchmarkFinalParameters"][0]["ProblemSizes"]), \
131 "Benchmark group must have non-empty ProblemSizes"
132
133 problemSizesGroup = benchmarkGroup["BenchmarkFinalParameters"][0]["ProblemSizes"]
134 problemSizesCount = len(problemSizesGroup)
135
136 numFiles = math.ceil(problemSizesCount / numSizes)
137
138 # Split files on the benchmark sizes
139 for i in range(numFiles):
140 result = {}
141 for k in data.keys():
142 if k == problemKey:
143
144 # Create a new benchmark group that has all the old keys but split the sizes.
145 newBenchmarkGroup = {}
146 for bk in benchmarkGroup.keys():
147 if bk == "BenchmarkFinalParameters":
148 newBenchmarkGroup[bk] = [ {"ProblemSizes": [] } ]
149 for j in range(i*numSizes, min((i+1)*numSizes, problemSizesCount)):
150 newBenchmarkGroup[bk][0]["ProblemSizes"].append(copy.deepcopy(problemSizesGroup[j]))
151 else:
152 newBenchmarkGroup[bk] = copy.deepcopy(benchmarkGroup[bk])
153
154 result[k] = [[copy.deepcopy(benchmarkProblems[problemIdx]), copy.deepcopy(newBenchmarkGroup)]]
155 else:
156 result[k] = copy.deepcopy(data[k])
157 rv.append(result)
158 return rv
159
160 # filePath: Name of the file (can be a path)
161 # suffix: Add a suffix of _## to fileName by default
162 # formatting: How the stuffix string is to be treated.
163 # If more indices are needed, you can increase them on the formatting.
164 @staticmethod
165 def __appendFileNameSuffix(filePath, suffix, separator="_", formatting="{:02}"):
166 root, ext = os.path.splitext(filePath)
167 suffixString = (separator + formatting).format(suffix)
168 return root + suffixString + ext
169
170 @staticmethod
171 def splitBenchmarkBySizes(configFile, outputDir, numSizes=1, baseFileName="", separator="_", suffixFormat="{:02}"):
172
173 # Use the configFile as base name if none provided
174 if baseFileName == "":
175 baseFileName = os.path.basename(configFile)
176
177 # If we want to split by sizes, then we must first split files
178 # into separate problems, then split those files into separate
179 # benchmark groups, then split those files into sizes
180 data = BenchmarkSplitter.__readConfigFile(configFile)
181 benchmarksByProblem = BenchmarkSplitter.__splitByProblem(data)
182 benchmarksByGroup = []
183 benchmarksBySize = []
184 for problem in benchmarksByProblem:
185 benchmarksByGroup += BenchmarkSplitter.__splitByBenchmarkGroup(problem)
186 for group in benchmarksByGroup:
187 benchmarksBySize += BenchmarkSplitter.__splitByBenchmarkSizes(group, numSizes)
188
189 # outputDir/basefileName_XX.ext
190 outputFileBase = os.path.join(outputDir, baseFileName)
191 for i in range(len(benchmarksBySize)):
192 outFileName = BenchmarkSplitter.__appendFileNameSuffix(outputFileBase, i, separator, suffixFormat)
193 with open(outFileName, "w") as f:
194 yaml.safe_dump(benchmarksBySize[i], f)