Inserts a ``get_attr`` node into the Graph.
(self, qualified_name: str, type_expr: Optional[Any] = None)
| 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 |
| 75 | if hasattr(submod, name): |
| 76 | attr = getattr(submod, name) |
| 77 | if isinstance(attr, torch.nn.Parameter): |
| 78 | return attr |
| 79 | |
| 80 | return None |
| 81 | |
| 82 | buffer = _maybe_get_attr_value(self.owning_module, qualified_name) |
| 83 | if buffer is not None: |
| 84 | node.meta["spec"] = TensorSpec.from_tensor(buffer, True) |
| 85 | |
| 86 | return node |