Debian Codegraph @

tensilelite/Tensile/TensileUpdateLibrary.py

raw symbols SCIP
1 ###############################################################################
2 #
3 # Copyright (C) 2022-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 LibraryIO
26 from .Tensile import addCommonArguments, argUpdatedGlobalParameters
27
28 from .Common import assignGlobalParameters, print1, restoreDefaultGlobalParameters, HR, \
29 globalParameters, architectureMap, ensurePath, ParallelMap, __version__
30
31 import argparse
32 import copy
33 import itertools
34 import os
35 import sys
36
37
38 def UpdateLogic(filename, logicPath, outputPath):
39 libYaml = LibraryIO.readYAML(filename)
40 # parseLibraryLogicData mutates the original data, so make a copy
41 fields = LibraryIO.parseLibraryLogicData(copy.deepcopy(libYaml), filename)
42 (_, _, problemType, solutions, _, _, _) = fields
43
44 # problem type object to state
45 problemTypeState = problemType.state
46 problemTypeState["DataType"] = problemTypeState["DataType"].value
47 problemTypeState["DataTypeA"] = problemTypeState["DataTypeA"].value
48 problemTypeState["DataTypeB"] = problemTypeState["DataTypeB"].value
49 problemTypeState["DataTypeE"] = problemTypeState["DataTypeE"].value
50 problemTypeState["DataTypeAmaxD"] = problemTypeState["DataTypeAmaxD"].value
51 problemTypeState["DestDataType"] = problemTypeState["DestDataType"].value
52 problemTypeState["ComputeDataType"] = problemTypeState["ComputeDataType"].value
53 problemTypeState["BiasDataTypeList"] = [btype.value for btype in problemTypeState["BiasDataTypeList"]]
54 problemTypeState["ActivationComputeDataType"] = problemTypeState["ActivationComputeDataType"].value
55 problemTypeState["ActivationType"] = problemTypeState["ActivationType"].value
56 problemTypeState["F32XdlMathOp"] = problemTypeState["F32XdlMathOp"].value
57 if "DataTypeMetadata" in problemTypeState:
58 problemTypeState["DataTypeMetadata"] = problemTypeState["DataTypeMetadata"].value
59
60 # solution objects to state
61 solutionList = []
62 for solution in solutions:
63 solutionState = solution.getAttributes()
64 solutionState["ProblemType"] = solutionState["ProblemType"].state
65 solutionState["ProblemType"]["DataType"] = \
66 solutionState["ProblemType"]["DataType"].value
67 solutionState["ProblemType"]["DataTypeA"] = \
68 solutionState["ProblemType"]["DataTypeA"].value
69 solutionState["ProblemType"]["DataTypeB"] = \
70 solutionState["ProblemType"]["DataTypeB"].value
71 solutionState["ProblemType"]["DataTypeE"] = \
72 solutionState["ProblemType"]["DataTypeE"].value
73 solutionState["ProblemType"]["DataTypeAmaxD"] = \
74 solutionState["ProblemType"]["DataTypeAmaxD"].value
75 solutionState["ProblemType"]["DestDataType"] = \
76 solutionState["ProblemType"]["DestDataType"].value
77 solutionState["ProblemType"]["ComputeDataType"] = \
78 solutionState["ProblemType"]["ComputeDataType"].value
79 solutionState["ProblemType"]["BiasDataTypeList"] = \
80 [btype.value for btype in solutionState["ProblemType"]["BiasDataTypeList"]]
81 solutionState["ProblemType"]["ActivationComputeDataType"] = \
82 solutionState["ProblemType"]["ActivationComputeDataType"].value
83 solutionState["ProblemType"]["ActivationType"] = \
84 solutionState["ProblemType"]["ActivationType"].value
85 solutionState["ProblemType"]["F32XdlMathOp"] = \
86 solutionState["ProblemType"]["F32XdlMathOp"].value
87 if "DataTypeMetadata" in solutionState["ProblemType"]:
88 solutionState["ProblemType"]["DataTypeMetadata"] = \
89 solutionState["ProblemType"]["DataTypeMetadata"].value
90
91 solutionState["ISA"] = list(solutionState["ISA"])
92 solutionList.append(solutionState)
93
94 # update yaml
95 libYaml[0] = {"MinimumRequiredVersion":__version__}
96 libYaml[4] = problemTypeState
97 libYaml[5] = solutionList
98
99 if outputPath != "":
100 filename = filename.replace(logicPath, outputPath)
101 ensurePath(os.path.dirname(filename))
102 LibraryIO.writeYAML(filename, libYaml, explicit_start=False, explicit_end=False)
103
104 def TensileUpdateLibrary(userArgs):
105 print1("")
106 print1(HR)
107 print1("#")
108 print1("# Tensile Update Library v{}".format(__version__))
109
110 # argument parsing and related setup
111 argParser = argparse.ArgumentParser()
112 argParser.add_argument("--logic_path", type=os.path.realpath, help="Path to LibraryLogic.yaml files.")
113 argParser.add_argument("--output_path", type=os.path.realpath, default=None, help="Where to place updated logic file.")
114
115 addCommonArguments(argParser)
116 args = argParser.parse_args(userArgs)
117
118 libPath = args.logic_path
119 print1("# Library Logic: {}".format(libPath))
120 print1("#")
121 print1(HR)
122 print1("")
123
124 # setup global parameters
125 restoreDefaultGlobalParameters()
126 assignGlobalParameters({})
127 overrideParameters = argUpdatedGlobalParameters(args)
128 for key, value in overrideParameters.items():
129 print1("Overriding {0}={1}".format(key, value))
130 globalParameters[key] = value
131
132 # Recursive directory search
133 logicArchs = set()
134 for key, name in architectureMap.items():
135 logicArchs.add(name)
136 logicFiles = []
137 for root, dirs, files in os.walk(args.logic_path):
138 logicFiles += [os.path.join(root, f) for f in files
139 if os.path.splitext(f)[1]==".yaml" \
140 and (any(logicArch in os.path.splitext(f)[0] for logicArch in logicArchs) \
141 or "hip" in os.path.splitext(f)[0]) ]
142
143 # update logic file
144 outputPath = ""
145 if args.output_path:
146 outputPath = ensurePath(os.path.abspath(args.output_path))
147
148 print("# LibraryLogicFiles:" % logicFiles)
149 for logicFile in logicFiles:
150 print("# %s" % logicFile)
151 fIter = zip(logicFiles, itertools.repeat(args.logic_path), itertools.repeat(outputPath))
152 libraries = ParallelMap(UpdateLogic, fIter, "Updating logic files", method=lambda x: x.starmap)
153
154
155 def main():
156 TensileUpdateLibrary(sys.argv[1:])