Debian Codegraph @

tensilelite/Tensile/TensileBenchmarkLibraryClient.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 sys
26 import csv
27 import os
28 from subprocess import Popen, PIPE
29
30 ################################################################################
31 # Benchmark Problem Size
32 ################################################################################
33 def BenchmarkProblemSize(cmdPrefix, row):
34 cmd = cmdPrefix
35 cmd += " --sizes"
36 for size in row:
37 cmd += " "
38 cmd += size.lstrip()
39 sys.stderr.write(cmd)
40 sys.stderr.write("\n")
41 process = Popen(cmd, stdout=PIPE, shell=True)
42 stdout = process.communicate()[0]
43 sys.stderr.write(stdout)
44
45 # find beginning of data
46 initializingIdx = stdout.find("Initializing")
47 newLineIdx = stdout.find("\n", initializingIdx)
48 stdout = stdout[newLineIdx+1:]
49
50 gflopList = []
51 msList = []
52
53 # skip first b/c warmup/lookup
54 newLineIdx = stdout.find("\n")
55 stdout = stdout[newLineIdx+1:]
56
57 # parse every line of data
58 while "\n" in stdout:
59 newLineIdx = stdout.find("\n")
60 lineString = stdout[:newLineIdx]
61 splits = lineString.split(",")
62 if len(splits) < 3:
63 break
64 gflopsString = splits[1].lstrip()
65 msString = splits[3].lstrip()
66 gflops = float(gflopsString)
67 ms = float(msString)
68 gflopList.append(gflops)
69 msList.append(ms)
70
71 # next line
72 stdout = stdout[newLineIdx+1:]
73 return (gflopList, msList)
74
75
76 ################################################################################
77 # Print Stats
78 ################################################################################
79 def PrintStats(header, row, gflopList, msList):
80 meanGFlops = mean(gflopList)
81 meanMs = mean(msList)
82 medianGFlops = median(gflopList)
83 medianMs = median(msList)
84 stddevGFlops = stddev(gflopList) / meanGFlops
85 stddevMs = stddev(msList) / meanMs
86
87 # format output
88 line = ""
89 for size in row:
90 line += "%6u, " % int(size)
91 line += "%9.2f, %9.4f, " % (medianGFlops, medianMs)
92 line += "%9.2f, %9.4f, " % (meanGFlops, meanMs)
93 line += "%9.5f, %9.5f" % (stddevGFlops, stddevMs)
94 sys.stdout.write(line)
95 sys.stdout.write("\n")
96 sys.stdout.flush()
97 sys.stderr.write("[STDOUT] %s\n" % header)
98 sys.stderr.write("[STDOUT] %s\n" % line)
99 sys.stderr.write("[END]\n\n\n")
100 sys.stderr.flush()
101
102
103 ################################################################################
104 # Benchmark Problem Sizes
105 ################################################################################
106 def TensileBenchmarkLibraryClient(userArgs):
107 if len(userArgs) < 2:
108 line = "USAGE: python TensileBenchmarkLibraryClient.py sizes.csv library_client_command > name.txt 2> name.raw.txt \n"
109 sys.stderr.write(line)
110 line = "Example: python TensileBenchmarkLibraryClient.py sizes.csv ./4_LibraryClient/build/client --function-idx 1 --num-benchmarks 100 --use-gpu-timer 0 > nn.txt 2> nn.raw.txt\n"
111 sys.stderr.write(line)
112 line = "[BE SURE TO PIN CLOCKS]\n"
113 sys.stderr.write(line)
114 exit(-1)
115
116 # parse problem sizes path
117 problemSizesPath = os.path.realpath( userArgs[0] )
118 line = "ProblemSizesPath: %s\n" % problemSizesPath
119 sys.stdout.write(line)
120 sys.stderr.write(line)
121
122 # parse library client command
123 libraryClientCommandList = userArgs[1:]
124 libraryClientCommand = " ".join(libraryClientCommandList)
125 line = "LibraryClientCommand: %s\n" % libraryClientCommand
126 sys.stdout.write(line)
127 sys.stdout.write("\n")
128 sys.stderr.write(line)
129 sys.stderr.write("\n\n")
130
131 # read problem sizes file
132 csvFileRaw = open(problemSizesPath, "r")
133 csvFile = csv.reader(csvFileRaw)
134
135 # column headers
136 numIndices = -1
137 for row in csvFile:
138 numIndices = len(row)
139 firstRow = row
140 break
141 header = ""
142 for i in range(0, numIndices):
143 sizeStr = "size%u" % i
144 header += "%6s, " % sizeStr
145 header += "%9s, %9s, " % ( "medianGF", "medianMs" )
146 header += "%9s, %9s, " % ( "meanGF", "meanMs" )
147 header += "%9s, %9s" % ( "rstdGF", "rstdMs" )
148 sys.stdout.write(header)
149 sys.stdout.write("\n")
150
151 # benchmark each problem size
152 for row in [firstRow]:
153 (gflopList, msList) = BenchmarkProblemSize(libraryClientCommand, row)
154 PrintStats(header, row, gflopList, msList)
155
156 # benchmark each problem size
157 for row in csvFile:
158 (gflopList, msList) = BenchmarkProblemSize(libraryClientCommand, row)
159 PrintStats(header, row, gflopList, msList)
160
161 def median(lst):
162 sortedList = sorted(lst)
163 return sortedList[len(sortedList)/2]
164
165 def mean(lst):
166 total = 0
167 for i in range(len(lst)):
168 total += lst[i]
169 return (total / len(lst))
170
171 def stddev(lst):
172 total = 0
173 mn = mean(lst)
174 for i in range(len(lst)):
175 total += pow((lst[i]-mn),2)
176 return (total/(len(lst)-1))**0.5
177
178 # installed "tensileBenchmarkLibraryClient" command
179 def main():
180 TensileBenchmarkLibraryClient(sys.argv[1:])
181
182 if __name__ == "__main__":
183 main()