ExportGraph serves as a layer between EXIR and FX Graph API. It enforces EXIR-specific invariants (ex. having nodes contain specs)
| 15 | |
| 16 | |
| 17 | class ExportGraph: |
| 18 | """ |
| 19 | ExportGraph serves as a layer between EXIR and FX Graph API. |
| 20 | It enforces EXIR-specific invariants (ex. having nodes contain specs) |
| 21 | """ |
| 22 | |
| 23 | owning_module: fx.GraphModule |
| 24 | _graph: fx.Graph |
| 25 | |
| 26 | def __init__(self, owning_module: fx.GraphModule, graph: fx.Graph) -> None: |
| 27 | self.owning_module = owning_module |
| 28 | self._graph = graph |
| 29 | |
| 30 | @property |
| 31 | def nodes(self) -> fx.graph._node_list: |
| 32 | """ |
| 33 | Get the list of Nodes that constitute this Graph. |
| 34 | """ |
| 35 | return self._graph.nodes |
| 36 | |
| 37 | def erase_node(self, to_erase: fx.Node) -> None: |
| 38 | """ |
| 39 | Erases a ``Node`` from the ``Graph``. Throws an exception if |
| 40 | there are still users of that node in the ``Graph``. |
| 41 | """ |
| 42 | return self._graph.erase_node(to_erase) |
| 43 | |
| 44 | def inserting_before(self, n: Optional[fx.Node] = None) -> fx.graph._InsertPoint: |
| 45 | """ |
| 46 | Sets the point at which we will insert the graph. |
| 47 | """ |
| 48 | return self._graph.inserting_before(n) |
| 49 | |
| 50 | # pyre-ignore |
| 51 | def get_attr(self, qualified_name: str, type_expr: Optional[Any] = None) -> fx.Node: |
| 52 | """ |
| 53 | Inserts a ``get_attr`` node into the Graph. |
| 54 | """ |
| 55 | node = self._graph.get_attr(qualified_name, type_expr) |
| 56 | |
| 57 | # Gets the actual value of the attribute if it exists so that we can use |
| 58 | # it to set the 'spec' metadata |
| 59 | def _maybe_get_attr_value( |
| 60 | mod: torch.nn.Module, qualified_name: str |
| 61 | ) -> Optional[torch.Tensor]: |
| 62 | module_path, _, name = qualified_name.rpartition(".") |
| 63 | |
| 64 | try: |
| 65 | submod: torch.nn.Module = mod.get_submodule(module_path) |
| 66 | except AttributeError: |
| 67 | warnings.warn(f"Failed to fetch module {module_path}!", stacklevel=1) |
| 68 | return None |
| 69 | |
| 70 | # See if the value is a buffer |
| 71 | if name in submod._buffers: |
| 72 | return submod._buffers[name] |
| 73 | |
| 74 | # See if the value is a parameter |
nothing calls this directly
no outgoing calls
no test coverage detected