Debian Codegraph @

tensilelite/Tensile/CustomKernels.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 . import CUSTOM_KERNEL_PATH
26 from Tensile.Common.ValidParameters import checkParametersAreValid, validParameters, newMIValidParameters
27
28 import yaml
29
30 import os
31
32 def isCustomKernelConfig(config):
33 return "CustomKernelName" in config and config["CustomKernelName"]
34
35 def getCustomKernelFilepath(name, directory=CUSTOM_KERNEL_PATH):
36 return os.path.join(directory, (name + ".s"))
37
38 def getAllCustomKernelNames(directory=CUSTOM_KERNEL_PATH):
39 return [fname[:-2] for fname in os.listdir(directory) if fname.endswith(".s")]
40
41 def getCustomKernelContents(name, directory=CUSTOM_KERNEL_PATH):
42 try:
43 with open(getCustomKernelFilepath(name, directory)) as f:
44 return f.read()
45 except:
46 raise RuntimeError("Failed to find custom kernel: {}".format(os.path.join(directory, name)))
47
48 def getCustomKernelConfigAndAssembly(name, directory=CUSTOM_KERNEL_PATH):
49 contents = getCustomKernelContents(name, directory)
50 config = "\n" #Yaml configuration properties
51 assembly = ""
52 inConfig = False
53 for line in contents.splitlines():
54 if line == "---": inConfig = True #Beginning of yaml section
55 elif line == "...": inConfig = False #End of yaml section
56 elif inConfig: config += line + "\n"
57 else : assembly += line + "\n"; config += "\n" #Second statement to keep line numbers consistent for yaml errors
58
59 return (config, assembly)
60
61 def readCustomKernelConfig(name, directory=CUSTOM_KERNEL_PATH):
62 rawConfig, _ = getCustomKernelConfigAndAssembly(name, directory)
63 try:
64 return yaml.safe_load(rawConfig)["custom.config"]
65 except yaml.scanner.ScannerError as e:
66 raise RuntimeError("Failed to read configuration for custom kernel: {0}\nDetails:\n{1}".format(name, e))
67
68 def getCustomKernelConfig(
69 kernelName: str, internalSupportParams: dict, directory: str = CUSTOM_KERNEL_PATH
70 ) -> dict:
71 """
72 Retrieves and validates the configuration for a custom kernel.
73
74 Args:
75 kernelName: The name of the custom kernel.
76 internalSupportParams: A dictionary of internal support parameters to be merged with the kernel configuration.
77 directory: The directory where custom kernel files are located. Defaults to CUSTOM_KERNEL_PATH.
78
79 Returns:
80 dict: The validated configuration dictionary for the custom kernel.
81
82 Raises:
83 RuntimeError: If the custom kernel configuration is missing required fields or if there is an error reading the configuration.
84 """
85 kernelConfig = readCustomKernelConfig(kernelName, directory)
86 if "InternalSupportParams" not in kernelConfig:
87 raise RuntimeError(f"Custom kernel {kernelName} config must have 'InternalSupportParams'")
88
89 if "KernArgsVersion" not in kernelConfig["InternalSupportParams"]:
90 raise RuntimeError(f"Custom kernel {kernelName} config must have 'KernArgsVersion'")
91
92 kernelIsp = kernelConfig["InternalSupportParams"]
93 for key in internalSupportParams:
94 if key not in kernelIsp:
95 kernelIsp[key] = internalSupportParams[key]
96
97 validParameters.update(newMIValidParameters)
98
99 for k, v in kernelConfig.items():
100 if k != "ProblemType":
101 checkParametersAreValid((k, [v]), validParameters)
102
103 kernelConfig["KernelLanguage"] = "Assembly"
104 kernelConfig["CustomKernelName"] = kernelName
105
106 return kernelConfig