GraphModule is an nn.Module generated from an fx.Graph. Graphmodule has a ``graph`` attribute, as well as ``code`` and ``forward`` attributes generated from that ``graph``. .. warning:: When ``graph`` is reassigned, ``code`` and ``forward`` will be automatically re
| 319 | |
| 320 | @compatibility(is_backward_compatible=True) |
| 321 | class GraphModule(torch.nn.Module): |
| 322 | """ |
| 323 | GraphModule is an nn.Module generated from an fx.Graph. Graphmodule has a |
| 324 | ``graph`` attribute, as well as ``code`` and ``forward`` attributes generated |
| 325 | from that ``graph``. |
| 326 | |
| 327 | .. warning:: |
| 328 | |
| 329 | When ``graph`` is reassigned, ``code`` and ``forward`` will be automatically |
| 330 | regenerated. However, if you edit the contents of the ``graph`` without reassigning |
| 331 | the ``graph`` attribute itself, you must call ``recompile()`` to update the generated |
| 332 | code. |
| 333 | """ |
| 334 | |
| 335 | def __new__(cls: "Type[GraphModule]", *args, **kwargs): |
| 336 | # each instance of a graph module needs its own forward method |
| 337 | # so create a new singleton class for each instance. |
| 338 | # it is a subclass of the user-defined class, the only difference |
| 339 | # is an extra layer to install the forward method |
| 340 | |
| 341 | # address issue described at https://github.com/pytorch/pytorch/issues/63883 |
| 342 | # in other words, traverse class hierarchy to fix the redundant class definition problem |
| 343 | for t in cls.__mro__: |
| 344 | c = t.__qualname__.split(".")[-1] |
| 345 | if c != "GraphModuleImpl": |
| 346 | cls = t |
| 347 | break |
| 348 | |
| 349 | class GraphModuleImpl(cls): # type: ignore[misc, valid-type] |
| 350 | pass |
| 351 | |
| 352 | return super().__new__(GraphModuleImpl) |
| 353 | |
| 354 | @compatibility(is_backward_compatible=True) |
| 355 | def __init__( |
| 356 | self, |
| 357 | root: Union[torch.nn.Module, Dict[str, Any]], |
| 358 | graph: Graph, |
| 359 | class_name: str = "GraphModule", |
| 360 | ): |
| 361 | """ |
| 362 | Construct a GraphModule. |
| 363 | |
| 364 | Args: |
| 365 | |
| 366 | root (Union[torch.nn.Module, Dict[str, Any]): |
| 367 | ``root`` can either be an nn.Module instance or a Dict mapping strings to any attribute type. |
| 368 | In the case that ``root`` is a Module, any references to Module-based objects (via qualified |
| 369 | name) in the Graph's Nodes' ``target`` field will be copied over from the respective place |
| 370 | within ``root``'s Module hierarchy into the GraphModule's module hierarchy. |
| 371 | In the case that ``root`` is a dict, the qualified name found in a Node's ``target`` will be |
| 372 | looked up directly in the dict's keys. The object mapped to by the Dict will be copied |
| 373 | over into the appropriate place within the GraphModule's module hierarchy. |
| 374 | |
| 375 | graph (Graph): ``graph`` contains the nodes this GraphModule should use for code generation |
| 376 | |
| 377 | class_name (str): ``name`` denotes the name of this GraphModule for debugging purposes. If it's unset, all |
| 378 | error messages will report as originating from ``GraphModule``. It may be helpful to set this |
no outgoing calls
searching dependent graphs…