| 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 |
""" |
| 26 |
A component is a piece of code that is chosen among compatible components based on the current hardware capabilities and/or kernel options. |
| 27 |
|
| 28 |
The class hierarchy is automatically used to detect which type of component a given class belongs to. For example, all the MAC components should inherit from the MAC class. |
| 29 |
|
| 30 |
Most components should be able to get away with defining their requirements via the class properties (e.g.): |
| 31 |
|
| 32 |
```python |
| 33 |
class FMA_NonPacked(MAC): |
| 34 |
asmCaps = {"v_fma_f16": True, |
| 35 |
"v_pk_fma_f16": False} |
| 36 |
#archCaps = {} |
| 37 |
kernel = {"ProblemType": {"DataType": DataType(DataTypeEnum.Half), |
| 38 |
"HighPrecisionAccumulate": False}} |
| 39 |
``` |
| 40 |
|
| 41 |
Values in the dictionaries can be lambdas for more advanced logic: |
| 42 |
|
| 43 |
```python |
| 44 |
class FMA_HPA_MAD_MIX(MAC): |
| 45 |
asmCaps = {"v_mad_mix_f32": True} |
| 46 |
#archCaps = {} |
| 47 |
kernel = {"ProblemType": {"DataType": DataType(DataTypeEnum.Half), |
| 48 |
"HighPrecisionAccumulate": True}, |
| 49 |
} |
| 50 |
``` |
| 51 |
|
| 52 |
Any more advanced logic should be implemented by overriding the matches() method. |
| 53 |
|
| 54 |
Components are found by calling `Component.<subtype>.find(writer)` where `writer` is a `KernelWriter` object: |
| 55 |
|
| 56 |
```python |
| 57 |
component = Component.MAC.find(self) |
| 58 |
if component: |
| 59 |
return component(self, m, innerUnroll) |
| 60 |
|
| 61 |
# No component was found, fall back to existing code... |
| 62 |
``` |
| 63 |
|
| 64 |
With this fallback mechanism, components can be added one at a time, without disrupting existing code. |
| 65 |
|
| 66 |
Components can be categorized in different files in the `Tensile/Components` directory. Each file should be listed in the `__all__` member of `Tensile/Components/__init__.py`. |
| 67 |
""" |
| 68 |
|
| 69 |
import abc |
| 70 |
from collections.abc import Mapping |
| 71 |
import inspect |
| 72 |
from dataclasses import dataclass |
| 73 |
|
| 74 |
@dataclass |
| 75 |
class LraTileProperties: |
| 76 |
""" |
| 77 |
Lra tile assignment properties. |
| 78 |
""" |
| 79 |
|
| 80 |
def PartialMatch(pattern, obj, debug=False, level=0): |
| 81 |
indent = " " * level |
| 82 |
if debug and level == 0: |
| 83 |
print("") |
| 84 |
if hasattr(pattern, "__call__"): |
| 85 |
if not pattern(obj): |
| 86 |
if debug: |
| 87 |
print("{indent}call({obj}) == False".format(indent=indent, obj=obj)) |
| 88 |
return False |
| 89 |
elif isinstance(pattern, Mapping) and \ |
| 90 |
isinstance(obj, Mapping): |
| 91 |
for key, value in pattern.items(): |
| 92 |
if key not in obj: |
| 93 |
if debug: |
| 94 |
print("{indent}{key} not in object.".format(indent=indent, key=key)) |
| 95 |
return False |
| 96 |
|
| 97 |
if debug: |
| 98 |
print("{indent} recursing into {key}".format(indent=indent, key=key)) |
| 99 |
if not PartialMatch(value, obj[key], debug, level+1): |
| 100 |
return False |
| 101 |
|
| 102 |
elif pattern != obj: |
| 103 |
if debug: |
| 104 |
print("{indent}{pattern} != {obj}".format(indent=indent, pattern=pattern, obj=obj)) |
| 105 |
return False |
| 106 |
|
| 107 |
if debug: |
| 108 |
print("{indent}: True".format(indent=indent)) |
| 109 |
return True |
| 110 |
|
| 111 |
class ComponentMeta(abc.ABCMeta): |
| 112 |
""" |
| 113 |
Metaclass which auto-registers each subclass in an "implementations" |
| 114 |
member of its parent class, to allow for hierarchical searching. |
| 115 |
""" |
| 116 |
def __init__(cls, name, bases, namespace, **kwargs): |
| 117 |
if inspect.isabstract(cls): |
| 118 |
cls.implementations = {} |
| 119 |
|
| 120 |
for base in bases: |
| 121 |
base.implementations[name] = cls |
| 122 |
setattr(base, name, cls) |
| 123 |
|
| 124 |
class Component(metaclass=ComponentMeta): |
| 125 |
""" |
| 126 |
Modular component which allows kernel components to be specified based on |
| 127 |
capability rather than based on individual architecture IDs. |
| 128 |
""" |
| 129 |
|
| 130 |
@classmethod |
| 131 |
def matches(cls, writer, debug=False): |
| 132 |
if hasattr(cls, "versions"): |
| 133 |
if not writer.version in cls.versions: |
| 134 |
return False |
| 135 |
|
| 136 |
attrs = ["asmCaps", "archCaps", "kernel"] |
| 137 |
for attr in attrs: |
| 138 |
if hasattr(cls, attr): |
| 139 |
if not PartialMatch(getattr(cls, attr), getattr(writer.states, attr), debug): |
| 140 |
return False |
| 141 |
|
| 142 |
return True |
| 143 |
|
| 144 |
@classmethod |
| 145 |
def findAll(cls, writer, debug=False, *args, **kwargs): |
| 146 |
found = [] |
| 147 |
for name, impl in cls.implementations.items(): |
| 148 |
if debug: |
| 149 |
print(name) |
| 150 |
if inspect.isabstract(impl): |
| 151 |
foundHere = impl.findAll(writer, debug, *args, **kwargs) |
| 152 |
if debug: |
| 153 |
print(name, ": found ", foundHere) |
| 154 |
found += foundHere |
| 155 |
elif impl.matches(writer, debug, *args, **kwargs): |
| 156 |
if debug: |
| 157 |
print(name, ": found impl") |
| 158 |
found.append(impl) |
| 159 |
else: |
| 160 |
if debug: |
| 161 |
print(name, "mismatch!") |
| 162 |
|
| 163 |
return found |
| 164 |
|
| 165 |
@classmethod |
| 166 |
def find(cls, writer, debug=False, *args, **kwargs): |
| 167 |
found = cls.findAll(writer, debug, *args, **kwargs) |
| 168 |
|
| 169 |
if len(found) == 0: |
| 170 |
return None |
| 171 |
|
| 172 |
if len(found) > 1: |
| 173 |
raise RuntimeError("Found {} implementations for {}".format(len(found), cls.__name__)) |
| 174 |
|
| 175 |
return found[0]() |
| 176 |
|
| 177 |
@classmethod |
| 178 |
def componentPath(cls, path=None, bases=None): |
| 179 |
if path is None: |
| 180 |
path = [] |
| 181 |
|
| 182 |
if bases is None: |
| 183 |
bases = cls.__bases__ |
| 184 |
|
| 185 |
if not isinstance(cls, str): |
| 186 |
className = cls.__name__ |
| 187 |
path = [className] + path |
| 188 |
|
| 189 |
if cls == Component or len(bases) == 0: |
| 190 |
return path |
| 191 |
return bases[0].componentPath(path) |
| 192 |
|
| 193 |
@abc.abstractmethod |
| 194 |
def __call__(self): |
| 195 |
""" |
| 196 |
Concrete subclasses must implement __call__. |
| 197 |
""" |
| 198 |
|
| 199 |
def commentHeader(self): |
| 200 |
""" |
| 201 |
Returns a comment which helps identify where a piece of code was generated. |
| 202 |
""" |
| 203 |
return "{}".format('.'.join(self.componentPath())) |
| 204 |
|
| 205 |
class MAC(Component): |
| 206 |
""" |
| 207 |
Multiply-accumulate block. |
| 208 |
""" |
| 209 |
|
| 210 |
class Signature(Component): |
| 211 |
""" |
| 212 |
Function signature block. |
| 213 |
""" |
| 214 |
|
| 215 |
class LocalRead(Component): |
| 216 |
""" |
| 217 |
Local read block. |
| 218 |
""" |
| 219 |
|
| 220 |
class SumUnroll(Component): |
| 221 |
""" |
| 222 |
Sum unroll block. |
| 223 |
""" |
| 224 |
@abc.abstractmethod |
| 225 |
def initSumUnroll(self, writer, kernel): |
| 226 |
pass |
| 227 |
|
| 228 |
@abc.abstractmethod |
| 229 |
def loopSum(self, writer, kernel, tP, u, innerUnroll): |
| 230 |
pass |
| 231 |
|
| 232 |
@abc.abstractmethod |
| 233 |
def storeSumLDS(self, writer, kernel, tP): |
| 234 |
pass |
| 235 |
|
| 236 |
class ShiftVectorComponents(Component): |
| 237 |
""" |
| 238 |
Shift vector components block. |
| 239 |
""" |
| 240 |
|
| 241 |
class ComputeStoreVgprs(Component): |
| 242 |
""" |
| 243 |
Compute store vgprs block. |
| 244 |
""" |
| 245 |
|
| 246 |
class NotLocalFullTileElements(Component): |
| 247 |
""" |
| 248 |
Not local full tile elements block. |
| 249 |
""" |
| 250 |
|
| 251 |
class LraTileAssignment(Component): |
| 252 |
""" |
| 253 |
Lra tile assignment block. |
| 254 |
""" |
| 255 |
|
| 256 |
class PackData(Component): |
| 257 |
""" |
| 258 |
Pack data block. |
| 259 |
""" |
| 260 |
|
| 261 |
class SIA(Component): |
| 262 |
""" |
| 263 |
ScheduleIterAlg block. |
| 264 |
""" |
| 265 |
@abc.abstractmethod |
| 266 |
def schedIntoIteration(self, writer, kernel, tensorParametersA, tensorParametersB, \ |
| 267 |
localWriteEndIter, firstIter, lastLoop, lastLc, globalReadIncACode, \ |
| 268 |
globalReadIncBCode): |
| 269 |
pass |
| 270 |
|
| 271 |
class GlobalWriteComponents(Component): |
| 272 |
pass |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
from .Components import * |