| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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" |
| 51 |
assembly = "" |
| 52 |
inConfig = False |
| 53 |
for line in contents.splitlines(): |
| 54 |
if line == "---": inConfig = True |
| 55 |
elif line == "...": inConfig = False |
| 56 |
elif inConfig: config += line + "\n" |
| 57 |
else : assembly += line + "\n"; config += "\n" |
| 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 |