|
1 |
| -from typing import Any, Callable, Dict |
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Any, Callable, Dict, Optional, Sequence, Union |
2 | 3 |
|
3 |
| -from torch.fx.node import Target |
| 4 | +from torch.fx.node import Target, Node |
4 | 5 | from torch_tensorrt.fx.converter_registry import CONVERTERS
|
5 | 6 |
|
6 |
| -DYNAMO_CONVERTERS: Dict[Target, Any] = dict(CONVERTERS) |
| 7 | + |
| 8 | +@dataclass(frozen=True) |
| 9 | +class GenericNode: |
| 10 | + """Class representing a generic Torch FX Node |
| 11 | +
|
| 12 | + Convenience class for when we do not have access to the node, |
| 13 | + and instead only its target, args, and kwargs, as is the case |
| 14 | + in the TRTInterpreter |
| 15 | + """ |
| 16 | + |
| 17 | + target: Any |
| 18 | + args: Sequence[Any] |
| 19 | + kwargs: Dict[str, Any] |
| 20 | + |
| 21 | + |
| 22 | +# Defines the different types of valid nodes |
| 23 | +NodeKinds = Union[GenericNode, Node] |
| 24 | + |
| 25 | + |
| 26 | +@dataclass(frozen=True) |
| 27 | +class ConverterSupport: |
| 28 | + """Class representing a converter implementation and support function |
| 29 | +
|
| 30 | + Args: |
| 31 | + check_args: Function which takes in a NodeKind and returns a bool indicating |
| 32 | + whether that node can be supported by its companion converter. Note that |
| 33 | + this function must only access the .target, .args, and .kwargs fields |
| 34 | + of the node, and cannot modify the node or its graph |
| 35 | + converter_implementation: Function which converts said node to a TRT equivalent |
| 36 | + """ |
| 37 | + |
| 38 | + check_args: Callable[[NodeKinds], bool] = field(default=lambda node: True) |
| 39 | + converter_implementation: Callable |
| 40 | + |
| 41 | + |
| 42 | +# Dictionary representing Dynamo aten-only converters |
| 43 | +# Each converter maps to a sequence of at least one ConverterSupport object(s) |
| 44 | +DYNAMO_ATEN_CONVERTERS: Dict[Target, Sequence[ConverterSupport]] = {} |
7 | 45 |
|
8 | 46 |
|
9 | 47 | def dynamo_tensorrt_converter(
|
10 | 48 | key: Target,
|
11 | 49 | enabled: bool = True,
|
| 50 | + check_args: Optional[Callable[[NodeKinds], bool]] = None, |
12 | 51 | ) -> Callable[[Any], Any]:
|
| 52 | + """Decorator for Dynamo TensorRT Converter |
| 53 | +
|
| 54 | + Registers the decorated function in the DYNAMO_ATEN_CONVERTERS registry |
| 55 | +
|
| 56 | + Args: |
| 57 | + key: Node target for which the converter is implemented for |
| 58 | + (for example, torch.ops.add.Tensor) |
| 59 | + enabled: Whether the converter should be enabled/cached or not |
| 60 | + check_args: Function which evaluates whether a node is valid for conversion |
| 61 | + by the decorated converter. See ConverterSupport for more details. |
| 62 | + Defaults to None, implying the check_args function is always true - |
| 63 | + this means all nodes of "key" kind can be supported by this converter |
| 64 | + Returns: |
| 65 | + The converter being decorated |
| 66 | + """ |
| 67 | + |
13 | 68 | def register_converter(converter):
|
14 |
| - DYNAMO_CONVERTERS[key] = converter |
| 69 | + """Helper function to register the converter, then return it""" |
| 70 | + assert callable(converter), "Converter function must be callable" |
| 71 | + |
| 72 | + # If no check_args function is specified, use the default function - always return true |
| 73 | + if check_args is None: |
| 74 | + converter_support = ConverterSupport(converter_implementation=converter) |
| 75 | + else: |
| 76 | + assert callable(check_args), "Argument checking function must be callable" |
| 77 | + converter_support = ConverterSupport( |
| 78 | + check_args=check_args, converter_implementation=converter |
| 79 | + ) |
| 80 | + |
| 81 | + # If a converter for this operator already exists, append the new converter to the list |
| 82 | + # Otherwise, start a new list |
| 83 | + if key in DYNAMO_ATEN_CONVERTERS: |
| 84 | + DYNAMO_ATEN_CONVERTERS[key].append(converter_support) |
| 85 | + else: |
| 86 | + DYNAMO_ATEN_CONVERTERS[key] = [converter_support] |
| 87 | + |
15 | 88 | return converter
|
16 | 89 |
|
17 | 90 | def disable_converter(converter):
|
18 | 91 | return converter
|
19 | 92 |
|
| 93 | + # Select whether to cache/enable the converter |
20 | 94 | if enabled:
|
21 | 95 | return register_converter
|
22 | 96 | else:
|
23 | 97 | return disable_converter
|
| 98 | + |
| 99 | + |
| 100 | +class ConverterRegistry: |
| 101 | + """Registry for storing multiple converter dictionaries |
| 102 | +
|
| 103 | + Capable of storing dictionaries with the following signature: |
| 104 | + Dict[Target, Union[Callable, Sequence[ConverterSupport]]] |
| 105 | +
|
| 106 | + Also able to validate converter implementations against user-provided |
| 107 | + argument-checking functions |
| 108 | +
|
| 109 | + Args: |
| 110 | + registries: List of dictionaries representing converter registries. |
| 111 | + The order of the provided dictionaries is the order in which they |
| 112 | + will be traversed. This is only significant when using non-validated |
| 113 | + methods. |
| 114 | + """ |
| 115 | + |
| 116 | + def __init__( |
| 117 | + self, |
| 118 | + registries: Sequence[Dict[Target, Union[Callable, Sequence[ConverterSupport]]]], |
| 119 | + ): |
| 120 | + # Copy reference to each dictionary object into attribute list |
| 121 | + self.registries = [registry for registry in registries] |
| 122 | + self.validate_invariants() |
| 123 | + |
| 124 | + def validate_invariants(self): |
| 125 | + """Validates the invariants required of the dictionaries in the registries |
| 126 | +
|
| 127 | + Raises AssertionError if any invariants have been violated |
| 128 | + """ |
| 129 | + # All registries must be dictionaries |
| 130 | + assert all(isinstance(elt, dict) for elt in self.registries) |
| 131 | + |
| 132 | + # Every dictionary in the registry must have one of two signatures: |
| 133 | + # Dict[Target, Callable] or Dict[Target, Sequence[ConverterSupport]] |
| 134 | + # Where, for the latter, the sequence must be non-empty |
| 135 | + for registry in self.registries: |
| 136 | + for converters in registry.values(): |
| 137 | + if isinstance(converters, (list, tuple)): |
| 138 | + assert ( |
| 139 | + all(isinstance(c, ConverterSupport) for c in converters) |
| 140 | + and len(converters) > 0 |
| 141 | + ) |
| 142 | + else: |
| 143 | + assert callable(converters), "Converter function must be callable" |
| 144 | + |
| 145 | + def __getitem__(self, key: Target): |
| 146 | + """Get the first-found converter in any registry |
| 147 | +
|
| 148 | + Searches all registries in order and returns the first converter encountered |
| 149 | + """ |
| 150 | + self.validate_invariants() |
| 151 | + |
| 152 | + # Iterate over all registries and return the first converter found |
| 153 | + for registry in self.registries: |
| 154 | + if key in registry: |
| 155 | + converters = registry[key] |
| 156 | + |
| 157 | + if isinstance(converters, (list, tuple)): |
| 158 | + return converters[0].converter_implementation |
| 159 | + else: |
| 160 | + return converters |
| 161 | + |
| 162 | + raise KeyError(f"None of the converter registries have an entry for {key}") |
| 163 | + |
| 164 | + def __getitem_with_validation__(self, node: NodeKinds): |
| 165 | + """Get the first-found validated converter in any registry |
| 166 | +
|
| 167 | + Searches all registries in order and returns the first converter |
| 168 | + which passes validation on the input node |
| 169 | + """ |
| 170 | + self.validate_invariants() |
| 171 | + key = node.target |
| 172 | + |
| 173 | + # Iterate over all registries, validating the converter on the input node |
| 174 | + # If no check_args function is found, assume full coverage |
| 175 | + for registry in self.registries: |
| 176 | + if key in registry: |
| 177 | + converters = registry[key] |
| 178 | + |
| 179 | + if isinstance(converters, (list, tuple)): |
| 180 | + for candidate in converters: |
| 181 | + if candidate.check_args(node): |
| 182 | + return candidate.converter_implementation |
| 183 | + else: |
| 184 | + return converters |
| 185 | + |
| 186 | + raise KeyError( |
| 187 | + f"None of the converter registries have a validated entry for {key}, with node {node}" |
| 188 | + ) |
| 189 | + |
| 190 | + def keys(self): |
| 191 | + """Get all unique targets across all dictionaries""" |
| 192 | + return self.unique_targets() |
| 193 | + |
| 194 | + def get(self, key: Target, value=None): |
| 195 | + """Get converter for input target with a default return""" |
| 196 | + try: |
| 197 | + return self.__getitem__(key) |
| 198 | + except KeyError: |
| 199 | + return value |
| 200 | + |
| 201 | + def __contains__(self, key: Target): |
| 202 | + """Check whether a converter for input target exists""" |
| 203 | + return any(key in registry for registry in self.registries) |
| 204 | + |
| 205 | + def get_validated(self, node: NodeKinds, value=None): |
| 206 | + """Get validated converter for input node with a default return""" |
| 207 | + try: |
| 208 | + return self.__getitem_with_validation__(node) |
| 209 | + except KeyError: |
| 210 | + return value |
| 211 | + |
| 212 | + def contains_validated(self, node: NodeKinds): |
| 213 | + """Check whether a validated converter for input node exists""" |
| 214 | + try: |
| 215 | + self.__getitem_with_validation__(node) |
| 216 | + return True |
| 217 | + except KeyError: |
| 218 | + return False |
| 219 | + |
| 220 | + def get_all_converters_with_target(self, key: Target): |
| 221 | + """Get all converters across all registries for the target |
| 222 | +
|
| 223 | + Returns a list of all converterts having the specified target |
| 224 | + """ |
| 225 | + self.validate_invariants() |
| 226 | + converters_with_target = [] |
| 227 | + |
| 228 | + for registry in self.registries: |
| 229 | + if key in registry: |
| 230 | + converters = registry[key] |
| 231 | + |
| 232 | + if isinstance(converters, (list, tuple)): |
| 233 | + converters_with_target.extend( |
| 234 | + [c.converter_implementation for c in converters] |
| 235 | + ) |
| 236 | + else: |
| 237 | + converters_with_target.append(converters) |
| 238 | + |
| 239 | + return converters_with_target |
| 240 | + |
| 241 | + def __setitem__(self, key, value): |
| 242 | + raise AssertionError( |
| 243 | + f"Do not set registry members directly through the ConverterRegistry object. " |
| 244 | + + f"Attempted to set {key}: {value} via direct assignment to ConverterRegistry." |
| 245 | + ) |
| 246 | + |
| 247 | + def __delitem__(self, key): |
| 248 | + raise AssertionError( |
| 249 | + f"Do not delete registry members directly through the ConverterRegistry object. " |
| 250 | + + f"Attempted to delete {key} via direct del on ConverterRegistry." |
| 251 | + ) |
| 252 | + |
| 253 | + def __len__(self): |
| 254 | + """Returns the sum of lengths of all registries stored""" |
| 255 | + return sum(len(registry) for registry in self.registries) |
| 256 | + |
| 257 | + def unique_targets(self): |
| 258 | + """Returns the set of unique converter targets stored across all registries""" |
| 259 | + return set.union(*[set(registry.keys()) for registry in self.registries]) |
| 260 | + |
| 261 | + |
| 262 | +# Initialize dynamo converter registry with the FX and Dynamo aten registries |
| 263 | +# Note the Dynamo registry is listed first, for precedence |
| 264 | +DYNAMO_CONVERTERS: ConverterRegistry = ConverterRegistry( |
| 265 | + [DYNAMO_ATEN_CONVERTERS, CONVERTERS] |
| 266 | +) |
0 commit comments