|
| 1 | +import collections |
1 | 2 | import functools |
2 | 3 | import logging |
3 | 4 | import re |
|
8 | 9 | import torch |
9 | 10 | import torch_tensorrt.dynamo.conversion.impl as impl |
10 | 11 | from torch.fx.node import Argument, Target |
| 12 | +from torch.fx.passes.shape_prop import TensorMetadata |
11 | 13 | from torch_tensorrt import _enums |
12 | 14 | from torch_tensorrt.dynamo._SourceIR import SourceIR |
13 | 15 | from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext |
@@ -44,6 +46,75 @@ def get_node_name(node: torch.fx.Node) -> str: |
44 | 46 | return node_name |
45 | 47 |
|
46 | 48 |
|
| 49 | +def get_node_io( |
| 50 | + node: torch.fx.Node, constant_mapping: Dict[str, Tuple[Sequence[int], str]] |
| 51 | +) -> str: |
| 52 | + """Gets a string representing the node inputs and outputs including tensor shapes and dtypes""" |
| 53 | + |
| 54 | + def format_tensor_metadata(metadata: Union[Any, Sequence[Any]]) -> str: |
| 55 | + """Formats the metadata for a single node""" |
| 56 | + # If the provided data is a simple TensorMetadata object, parse it |
| 57 | + if isinstance(metadata, TensorMetadata) or issubclass( |
| 58 | + type(metadata), torch.Tensor |
| 59 | + ): |
| 60 | + return f"{tuple(metadata.shape)}@{metadata.dtype}" # type: ignore |
| 61 | + # If the provided data is a scalar, return it as is |
| 62 | + elif isinstance(metadata, (int, float, bool)): |
| 63 | + return f"{metadata}@Python-{type(metadata)}" |
| 64 | + # If the provided data is a sequence, recursively parse it |
| 65 | + elif isinstance(metadata, collections.abc.Sequence): |
| 66 | + formatted_str = "(" |
| 67 | + for meta in metadata: |
| 68 | + formatted_str += format_tensor_metadata(meta) + ", " |
| 69 | + |
| 70 | + return formatted_str[:-2] + ")" |
| 71 | + else: |
| 72 | + _LOGGER.warning( |
| 73 | + f"Detected unparseable type in node formatting: {type(metadata)}" |
| 74 | + ) |
| 75 | + return "" |
| 76 | + |
| 77 | + # Format input tensors |
| 78 | + metadata_string = "Inputs: (" |
| 79 | + |
| 80 | + # For each input argument, format it accordingly |
| 81 | + for arg in node.args: |
| 82 | + if isinstance(arg, torch.fx.Node): |
| 83 | + if arg.op == "get_attr": |
| 84 | + shape, dtype = constant_mapping[str(arg)] |
| 85 | + arg_repr = f"{shape}@{dtype}" |
| 86 | + elif arg.meta.get("tensor_meta") is not None: |
| 87 | + arg_repr = format_tensor_metadata(arg.meta["tensor_meta"]) |
| 88 | + elif arg.meta.get("val") is not None: |
| 89 | + arg_repr = format_tensor_metadata(arg.meta["val"]) |
| 90 | + else: |
| 91 | + arg_repr = "" |
| 92 | + |
| 93 | + metadata_string += f"{arg}: {arg_repr}, " |
| 94 | + else: |
| 95 | + metadata_string += f"{arg}, " |
| 96 | + |
| 97 | + metadata_string = ( |
| 98 | + metadata_string[:-2] if metadata_string[-1] != "(" else metadata_string |
| 99 | + ) + ")" |
| 100 | + |
| 101 | + # Format output tensors and arguments |
| 102 | + metadata_string += " | Outputs: (" |
| 103 | + if node.op == "get_attr": |
| 104 | + shape, dtype = constant_mapping[str(node)] |
| 105 | + node_repr = f"{shape}@{dtype}" |
| 106 | + elif node.meta.get("tensor_meta") is not None: |
| 107 | + node_repr = format_tensor_metadata(node.meta["tensor_meta"]) |
| 108 | + elif node.meta.get("val") is not None: |
| 109 | + node_repr = format_tensor_metadata(node.meta["val"]) |
| 110 | + else: |
| 111 | + node_repr = "" |
| 112 | + metadata_string += f"{node}: {node_repr}, " |
| 113 | + metadata_string = metadata_string[:-2] + ")" |
| 114 | + |
| 115 | + return metadata_string |
| 116 | + |
| 117 | + |
47 | 118 | def is_only_operator_on_placeholder(node: torch.fx.Node) -> bool: |
48 | 119 | """Detects whether a call_function node is the only operator on a placeholder""" |
49 | 120 | # Returns true if the node operates on a placeholder and is a direct output |
|
0 commit comments