| 72 | |
| 73 | # Representation of an FX GraphModule as an OperatorGraph |
| 74 | class FXOperatorGraph(OperatorGraph): |
| 75 | @staticmethod |
| 76 | def _get_node_name(node: torch.fx.Node) -> str: |
| 77 | if node.target == operator.getitem: |
| 78 | # pyre-ignore[9]: Incompatible variable type |
| 79 | node = node.args[0] |
| 80 | assert isinstance( |
| 81 | node, torch.fx.Node |
| 82 | ), f"First argument of getitem must be a torch fx node. Got {node.args[0]}" |
| 83 | |
| 84 | # Adding the "_" to the node name prevents TensorBoard from collapsing |
| 85 | # nodes with similar names that only differ by an integer at the end of |
| 86 | # their name. |
| 87 | return node.name + "_" |
| 88 | |
| 89 | @staticmethod |
| 90 | def _get_op_name(node: torch.fx.Node) -> str: |
| 91 | # pyre-ignore[16]: has no attribute `__name__`. |
| 92 | return node.target.__name__ |
| 93 | |
| 94 | # Given a node and its metadata (if containing module stack), update the provided module mappings |
| 95 | @staticmethod |
| 96 | def _update_module_mapping( |
| 97 | node: Node, |
| 98 | module_mapping: Dict[Tuple[str, str], List[Node]], |
| 99 | metadata: Dict[str, Any], |
| 100 | ): |
| 101 | if ( |
| 102 | source_fn_stack := metadata.get("source_fn_stack") |
| 103 | ) is not None and "nn_module_stack" in metadata: |
| 104 | # (module name, module type) |
| 105 | source_fn = source_fn_stack[-1] |
| 106 | module_type = ( |
| 107 | source_fn[1] if isinstance(source_fn[1], str) else source_fn[1].__name__ |
| 108 | ) |
| 109 | module_mapping[(source_fn[0], module_type)].append(node) |
| 110 | |
| 111 | @staticmethod |
| 112 | def _parse_args( # noqa: C901 |
| 113 | node: torch.fx.Node, |
| 114 | nodes: Dict[str, Node], |
| 115 | const_count: int, |
| 116 | module_mapping: Dict[Tuple[str, str], List[Node]], |
| 117 | enable_module_hierarchy: bool, |
| 118 | ) -> Tuple[List[Node], int]: |
| 119 | inputs = [] |
| 120 | op = node.op |
| 121 | name = node.name |
| 122 | args = node.args |
| 123 | kwargs = node.kwargs |
| 124 | named_args = None |
| 125 | if node.op == "call_function" and hasattr(node.target, "_schema"): |
| 126 | # pyre-ignore |
| 127 | named_args = node.target._schema.arguments |
| 128 | |
| 129 | if node.op == "call_function" and ( |
| 130 | node.target == auto_functionalized or node.target == auto_functionalized_v2 |
| 131 | ): |