feature_extraction.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import inspect
  2. import math
  3. import re
  4. import warnings
  5. from collections import OrderedDict
  6. from copy import deepcopy
  7. from itertools import chain
  8. from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  9. import torch
  10. import torchvision
  11. from torch import fx, nn
  12. from torch.fx.graph_module import _copy_attr
  13. __all__ = ["create_feature_extractor", "get_graph_node_names"]
  14. class LeafModuleAwareTracer(fx.Tracer):
  15. """
  16. An fx.Tracer that allows the user to specify a set of leaf modules, i.e.
  17. modules that are not to be traced through. The resulting graph ends up
  18. having single nodes referencing calls to the leaf modules' forward methods.
  19. """
  20. def __init__(self, *args, **kwargs):
  21. self.leaf_modules = {}
  22. if "leaf_modules" in kwargs:
  23. leaf_modules = kwargs.pop("leaf_modules")
  24. self.leaf_modules = leaf_modules
  25. super().__init__(*args, **kwargs)
  26. def is_leaf_module(self, m: nn.Module, module_qualname: str) -> bool:
  27. if isinstance(m, tuple(self.leaf_modules)):
  28. return True
  29. return super().is_leaf_module(m, module_qualname)
  30. class NodePathTracer(LeafModuleAwareTracer):
  31. """
  32. NodePathTracer is an FX tracer that, for each operation, also records the
  33. name of the Node from which the operation originated. A node name here is
  34. a `.` separated path walking the hierarchy from top level module down to
  35. leaf operation or leaf module. The name of the top level module is not
  36. included as part of the node name. For example, if we trace a module whose
  37. forward method applies a ReLU module, the name for that node will simply
  38. be 'relu'.
  39. Some notes on the specifics:
  40. - Nodes are recorded to `self.node_to_qualname` which is a dictionary
  41. mapping a given Node object to its node name.
  42. - Nodes are recorded in the order which they are executed during
  43. tracing.
  44. - When a duplicate node name is encountered, a suffix of the form
  45. _{int} is added. The counter starts from 1.
  46. """
  47. def __init__(self, *args, **kwargs):
  48. super().__init__(*args, **kwargs)
  49. # Track the qualified name of the Node being traced
  50. self.current_module_qualname = ""
  51. # A map from FX Node to the qualified name\#
  52. # NOTE: This is loosely like the "qualified name" mentioned in the
  53. # torch.fx docs https://pytorch.org/docs/stable/fx.html but adapted
  54. # for the purposes of the torchvision feature extractor
  55. self.node_to_qualname = OrderedDict()
  56. def call_module(self, m: torch.nn.Module, forward: Callable, args, kwargs):
  57. """
  58. Override of `fx.Tracer.call_module`
  59. This override:
  60. 1) Stores away the qualified name of the caller for restoration later
  61. 2) Adds the qualified name of the caller to
  62. `current_module_qualname` for retrieval by `create_proxy`
  63. 3) Once a leaf module is reached, calls `create_proxy`
  64. 4) Restores the caller's qualified name into current_module_qualname
  65. """
  66. old_qualname = self.current_module_qualname
  67. try:
  68. module_qualname = self.path_of_module(m)
  69. self.current_module_qualname = module_qualname
  70. if not self.is_leaf_module(m, module_qualname):
  71. out = forward(*args, **kwargs)
  72. return out
  73. return self.create_proxy("call_module", module_qualname, args, kwargs)
  74. finally:
  75. self.current_module_qualname = old_qualname
  76. def create_proxy(
  77. self, kind: str, target: fx.node.Target, args, kwargs, name=None, type_expr=None, *_
  78. ) -> fx.proxy.Proxy:
  79. """
  80. Override of `Tracer.create_proxy`. This override intercepts the recording
  81. of every operation and stores away the current traced module's qualified
  82. name in `node_to_qualname`
  83. """
  84. proxy = super().create_proxy(kind, target, args, kwargs, name, type_expr)
  85. self.node_to_qualname[proxy.node] = self._get_node_qualname(self.current_module_qualname, proxy.node)
  86. return proxy
  87. def _get_node_qualname(self, module_qualname: str, node: fx.node.Node) -> str:
  88. node_qualname = module_qualname
  89. if node.op != "call_module":
  90. # In this case module_qualname from torch.fx doesn't go all the
  91. # way to the leaf function/op, so we need to append it
  92. if len(node_qualname) > 0:
  93. # Only append '.' if we are deeper than the top level module
  94. node_qualname += "."
  95. node_qualname += str(node)
  96. # Now we need to add an _{index} postfix on any repeated node names
  97. # For modules we do this from scratch
  98. # But for anything else, torch.fx already has a globally scoped
  99. # _{index} postfix. But we want it locally (relative to direct parent)
  100. # scoped. So first we need to undo the torch.fx postfix
  101. if re.match(r".+_[0-9]+$", node_qualname) is not None:
  102. node_qualname = node_qualname.rsplit("_", 1)[0]
  103. # ... and now we add on our own postfix
  104. for existing_qualname in reversed(self.node_to_qualname.values()):
  105. # Check to see if existing_qualname is of the form
  106. # {node_qualname} or {node_qualname}_{int}
  107. if re.match(rf"{node_qualname}(_[0-9]+)?$", existing_qualname) is not None:
  108. postfix = existing_qualname.replace(node_qualname, "")
  109. if len(postfix):
  110. # existing_qualname is of the form {node_qualname}_{int}
  111. next_index = int(postfix[1:]) + 1
  112. else:
  113. # existing_qualname is of the form {node_qualname}
  114. next_index = 1
  115. node_qualname += f"_{next_index}"
  116. break
  117. return node_qualname
  118. def _is_subseq(x, y):
  119. """Check if y is a subsequence of x
  120. https://stackoverflow.com/a/24017747/4391249
  121. """
  122. iter_x = iter(x)
  123. return all(any(x_item == y_item for x_item in iter_x) for y_item in y)
  124. def _warn_graph_differences(train_tracer: NodePathTracer, eval_tracer: NodePathTracer):
  125. """
  126. Utility function for warning the user if there are differences between
  127. the train graph nodes and the eval graph nodes.
  128. """
  129. train_nodes = list(train_tracer.node_to_qualname.values())
  130. eval_nodes = list(eval_tracer.node_to_qualname.values())
  131. if len(train_nodes) == len(eval_nodes) and all(t == e for t, e in zip(train_nodes, eval_nodes)):
  132. return
  133. suggestion_msg = (
  134. "When choosing nodes for feature extraction, you may need to specify "
  135. "output nodes for train and eval mode separately."
  136. )
  137. if _is_subseq(train_nodes, eval_nodes):
  138. msg = (
  139. "NOTE: The nodes obtained by tracing the model in eval mode "
  140. "are a subsequence of those obtained in train mode. "
  141. )
  142. elif _is_subseq(eval_nodes, train_nodes):
  143. msg = (
  144. "NOTE: The nodes obtained by tracing the model in train mode "
  145. "are a subsequence of those obtained in eval mode. "
  146. )
  147. else:
  148. msg = "The nodes obtained by tracing the model in train mode are different to those obtained in eval mode. "
  149. warnings.warn(msg + suggestion_msg)
  150. def _get_leaf_modules_for_ops() -> List[type]:
  151. members = inspect.getmembers(torchvision.ops)
  152. result = []
  153. for _, obj in members:
  154. if inspect.isclass(obj) and issubclass(obj, torch.nn.Module):
  155. result.append(obj)
  156. return result
  157. def _set_default_tracer_kwargs(original_tr_kwargs: Optional[Dict[str, Any]]) -> Dict[str, Any]:
  158. default_autowrap_modules = (math, torchvision.ops)
  159. default_leaf_modules = _get_leaf_modules_for_ops()
  160. result_tracer_kwargs = {} if original_tr_kwargs is None else original_tr_kwargs
  161. result_tracer_kwargs["autowrap_modules"] = (
  162. tuple(set(result_tracer_kwargs["autowrap_modules"] + default_autowrap_modules))
  163. if "autowrap_modules" in result_tracer_kwargs
  164. else default_autowrap_modules
  165. )
  166. result_tracer_kwargs["leaf_modules"] = (
  167. list(set(result_tracer_kwargs["leaf_modules"] + default_leaf_modules))
  168. if "leaf_modules" in result_tracer_kwargs
  169. else default_leaf_modules
  170. )
  171. return result_tracer_kwargs
  172. def get_graph_node_names(
  173. model: nn.Module,
  174. tracer_kwargs: Optional[Dict[str, Any]] = None,
  175. suppress_diff_warning: bool = False,
  176. ) -> Tuple[List[str], List[str]]:
  177. """
  178. Dev utility to return node names in order of execution. See note on node
  179. names under :func:`create_feature_extractor`. Useful for seeing which node
  180. names are available for feature extraction. There are two reasons that
  181. node names can't easily be read directly from the code for a model:
  182. 1. Not all submodules are traced through. Modules from ``torch.nn`` all
  183. fall within this category.
  184. 2. Nodes representing the repeated application of the same operation
  185. or leaf module get a ``_{counter}`` postfix.
  186. The model is traced twice: once in train mode, and once in eval mode. Both
  187. sets of node names are returned.
  188. For more details on the node naming conventions used here, please see the
  189. :ref:`relevant subheading <about-node-names>` in the
  190. `documentation <https://pytorch.org/vision/stable/feature_extraction.html>`_.
  191. Args:
  192. model (nn.Module): model for which we'd like to print node names
  193. tracer_kwargs (dict, optional): a dictionary of keyword arguments for
  194. ``NodePathTracer`` (they are eventually passed onto
  195. `torch.fx.Tracer <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer>`_).
  196. By default, it will be set to wrap and make leaf nodes all torchvision ops:
  197. {"autowrap_modules": (math, torchvision.ops,),"leaf_modules": _get_leaf_modules_for_ops(),}
  198. WARNING: In case the user provides tracer_kwargs, above default arguments will be appended to the user
  199. provided dictionary.
  200. suppress_diff_warning (bool, optional): whether to suppress a warning
  201. when there are discrepancies between the train and eval version of
  202. the graph. Defaults to False.
  203. Returns:
  204. tuple(list, list): a list of node names from tracing the model in
  205. train mode, and another from tracing the model in eval mode.
  206. Examples::
  207. >>> model = torchvision.models.resnet18()
  208. >>> train_nodes, eval_nodes = get_graph_node_names(model)
  209. """
  210. tracer_kwargs = _set_default_tracer_kwargs(tracer_kwargs)
  211. is_training = model.training
  212. train_tracer = NodePathTracer(**tracer_kwargs)
  213. train_tracer.trace(model.train())
  214. eval_tracer = NodePathTracer(**tracer_kwargs)
  215. eval_tracer.trace(model.eval())
  216. train_nodes = list(train_tracer.node_to_qualname.values())
  217. eval_nodes = list(eval_tracer.node_to_qualname.values())
  218. if not suppress_diff_warning:
  219. _warn_graph_differences(train_tracer, eval_tracer)
  220. # Restore training state
  221. model.train(is_training)
  222. return train_nodes, eval_nodes
  223. class DualGraphModule(fx.GraphModule):
  224. """
  225. A derivative of `fx.GraphModule`. Differs in the following ways:
  226. - Requires a train and eval version of the underlying graph
  227. - Copies submodules according to the nodes of both train and eval graphs.
  228. - Calling train(mode) switches between train graph and eval graph.
  229. """
  230. def __init__(
  231. self, root: torch.nn.Module, train_graph: fx.Graph, eval_graph: fx.Graph, class_name: str = "GraphModule"
  232. ):
  233. """
  234. Args:
  235. root (nn.Module): module from which the copied module hierarchy is
  236. built
  237. train_graph (fx.Graph): the graph that should be used in train mode
  238. eval_graph (fx.Graph): the graph that should be used in eval mode
  239. """
  240. super(fx.GraphModule, self).__init__()
  241. self.__class__.__name__ = class_name
  242. self.train_graph = train_graph
  243. self.eval_graph = eval_graph
  244. # Copy all get_attr and call_module ops (indicated by BOTH train and
  245. # eval graphs)
  246. for node in chain(iter(train_graph.nodes), iter(eval_graph.nodes)):
  247. if node.op in ["get_attr", "call_module"]:
  248. if not isinstance(node.target, str):
  249. raise TypeError(f"node.target should be of type str instead of {type(node.target)}")
  250. _copy_attr(root, self, node.target)
  251. # train mode by default
  252. self.train()
  253. self.graph = train_graph
  254. # (borrowed from fx.GraphModule):
  255. # Store the Tracer class responsible for creating a Graph separately as part of the
  256. # GraphModule state, except when the Tracer is defined in a local namespace.
  257. # Locally defined Tracers are not pickleable. This is needed because torch.package will
  258. # serialize a GraphModule without retaining the Graph, and needs to use the correct Tracer
  259. # to re-create the Graph during deserialization.
  260. if self.eval_graph._tracer_cls != self.train_graph._tracer_cls:
  261. raise TypeError(
  262. f"Train mode and eval mode should use the same tracer class. Instead got {self.eval_graph._tracer_cls} for eval vs {self.train_graph._tracer_cls} for train"
  263. )
  264. self._tracer_cls = None
  265. if self.graph._tracer_cls and "<locals>" not in self.graph._tracer_cls.__qualname__:
  266. self._tracer_cls = self.graph._tracer_cls
  267. def train(self, mode=True):
  268. """
  269. Swap out the graph depending on the selected training mode.
  270. NOTE this should be safe when calling model.eval() because that just
  271. calls this with mode == False.
  272. """
  273. # NOTE: Only set self.graph if the current graph is not the desired
  274. # one. This saves us from recompiling the graph where not necessary.
  275. if mode and not self.training:
  276. self.graph = self.train_graph
  277. elif not mode and self.training:
  278. self.graph = self.eval_graph
  279. return super().train(mode=mode)
  280. def create_feature_extractor(
  281. model: nn.Module,
  282. return_nodes: Optional[Union[List[str], Dict[str, str]]] = None,
  283. train_return_nodes: Optional[Union[List[str], Dict[str, str]]] = None,
  284. eval_return_nodes: Optional[Union[List[str], Dict[str, str]]] = None,
  285. tracer_kwargs: Optional[Dict[str, Any]] = None,
  286. suppress_diff_warning: bool = False,
  287. ) -> fx.GraphModule:
  288. """
  289. Creates a new graph module that returns intermediate nodes from a given
  290. model as dictionary with user specified keys as strings, and the requested
  291. outputs as values. This is achieved by re-writing the computation graph of
  292. the model via FX to return the desired nodes as outputs. All unused nodes
  293. are removed, together with their corresponding parameters.
  294. Desired output nodes must be specified as a ``.`` separated
  295. path walking the module hierarchy from top level module down to leaf
  296. operation or leaf module. For more details on the node naming conventions
  297. used here, please see the :ref:`relevant subheading <about-node-names>`
  298. in the `documentation <https://pytorch.org/vision/stable/feature_extraction.html>`_.
  299. Not all models will be FX traceable, although with some massaging they can
  300. be made to cooperate. Here's a (not exhaustive) list of tips:
  301. - If you don't need to trace through a particular, problematic
  302. sub-module, turn it into a "leaf module" by passing a list of
  303. ``leaf_modules`` as one of the ``tracer_kwargs`` (see example below).
  304. It will not be traced through, but rather, the resulting graph will
  305. hold a reference to that module's forward method.
  306. - Likewise, you may turn functions into leaf functions by passing a
  307. list of ``autowrap_functions`` as one of the ``tracer_kwargs`` (see
  308. example below).
  309. - Some inbuilt Python functions can be problematic. For instance,
  310. ``int`` will raise an error during tracing. You may wrap them in your
  311. own function and then pass that in ``autowrap_functions`` as one of
  312. the ``tracer_kwargs``.
  313. For further information on FX see the
  314. `torch.fx documentation <https://pytorch.org/docs/stable/fx.html>`_.
  315. Args:
  316. model (nn.Module): model on which we will extract the features
  317. return_nodes (list or dict, optional): either a ``List`` or a ``Dict``
  318. containing the names (or partial names - see note above)
  319. of the nodes for which the activations will be returned. If it is
  320. a ``Dict``, the keys are the node names, and the values
  321. are the user-specified keys for the graph module's returned
  322. dictionary. If it is a ``List``, it is treated as a ``Dict`` mapping
  323. node specification strings directly to output names. In the case
  324. that ``train_return_nodes`` and ``eval_return_nodes`` are specified,
  325. this should not be specified.
  326. train_return_nodes (list or dict, optional): similar to
  327. ``return_nodes``. This can be used if the return nodes
  328. for train mode are different than those from eval mode.
  329. If this is specified, ``eval_return_nodes`` must also be specified,
  330. and ``return_nodes`` should not be specified.
  331. eval_return_nodes (list or dict, optional): similar to
  332. ``return_nodes``. This can be used if the return nodes
  333. for train mode are different than those from eval mode.
  334. If this is specified, ``train_return_nodes`` must also be specified,
  335. and `return_nodes` should not be specified.
  336. tracer_kwargs (dict, optional): a dictionary of keyword arguments for
  337. ``NodePathTracer`` (which passes them onto it's parent class
  338. `torch.fx.Tracer <https://pytorch.org/docs/stable/fx.html#torch.fx.Tracer>`_).
  339. By default, it will be set to wrap and make leaf nodes all torchvision ops:
  340. {"autowrap_modules": (math, torchvision.ops,),"leaf_modules": _get_leaf_modules_for_ops(),}
  341. WARNING: In case the user provides tracer_kwargs, above default arguments will be appended to the user
  342. provided dictionary.
  343. suppress_diff_warning (bool, optional): whether to suppress a warning
  344. when there are discrepancies between the train and eval version of
  345. the graph. Defaults to False.
  346. Examples::
  347. >>> # Feature extraction with resnet
  348. >>> model = torchvision.models.resnet18()
  349. >>> # extract layer1 and layer3, giving as names `feat1` and feat2`
  350. >>> model = create_feature_extractor(
  351. >>> model, {'layer1': 'feat1', 'layer3': 'feat2'})
  352. >>> out = model(torch.rand(1, 3, 224, 224))
  353. >>> print([(k, v.shape) for k, v in out.items()])
  354. >>> [('feat1', torch.Size([1, 64, 56, 56])),
  355. >>> ('feat2', torch.Size([1, 256, 14, 14]))]
  356. >>> # Specifying leaf modules and leaf functions
  357. >>> def leaf_function(x):
  358. >>> # This would raise a TypeError if traced through
  359. >>> return int(x)
  360. >>>
  361. >>> class LeafModule(torch.nn.Module):
  362. >>> def forward(self, x):
  363. >>> # This would raise a TypeError if traced through
  364. >>> int(x.shape[0])
  365. >>> return torch.nn.functional.relu(x + 4)
  366. >>>
  367. >>> class MyModule(torch.nn.Module):
  368. >>> def __init__(self):
  369. >>> super().__init__()
  370. >>> self.conv = torch.nn.Conv2d(3, 1, 3)
  371. >>> self.leaf_module = LeafModule()
  372. >>>
  373. >>> def forward(self, x):
  374. >>> leaf_function(x.shape[0])
  375. >>> x = self.conv(x)
  376. >>> return self.leaf_module(x)
  377. >>>
  378. >>> model = create_feature_extractor(
  379. >>> MyModule(), return_nodes=['leaf_module'],
  380. >>> tracer_kwargs={'leaf_modules': [LeafModule],
  381. >>> 'autowrap_functions': [leaf_function]})
  382. """
  383. tracer_kwargs = _set_default_tracer_kwargs(tracer_kwargs)
  384. is_training = model.training
  385. if all(arg is None for arg in [return_nodes, train_return_nodes, eval_return_nodes]):
  386. raise ValueError(
  387. "Either `return_nodes` or `train_return_nodes` and `eval_return_nodes` together, should be specified"
  388. )
  389. if (train_return_nodes is None) ^ (eval_return_nodes is None):
  390. raise ValueError(
  391. "If any of `train_return_nodes` and `eval_return_nodes` are specified, then both should be specified"
  392. )
  393. if not ((return_nodes is None) ^ (train_return_nodes is None)):
  394. raise ValueError("If `train_return_nodes` and `eval_return_nodes` are specified, then both should be specified")
  395. # Put *_return_nodes into Dict[str, str] format
  396. def to_strdict(n) -> Dict[str, str]:
  397. if isinstance(n, list):
  398. return {str(i): str(i) for i in n}
  399. return {str(k): str(v) for k, v in n.items()}
  400. if train_return_nodes is None:
  401. return_nodes = to_strdict(return_nodes)
  402. train_return_nodes = deepcopy(return_nodes)
  403. eval_return_nodes = deepcopy(return_nodes)
  404. else:
  405. train_return_nodes = to_strdict(train_return_nodes)
  406. eval_return_nodes = to_strdict(eval_return_nodes)
  407. # Repeat the tracing and graph rewriting for train and eval mode
  408. tracers = {}
  409. graphs = {}
  410. mode_return_nodes: Dict[str, Dict[str, str]] = {"train": train_return_nodes, "eval": eval_return_nodes}
  411. for mode in ["train", "eval"]:
  412. if mode == "train":
  413. model.train()
  414. elif mode == "eval":
  415. model.eval()
  416. # Instantiate our NodePathTracer and use that to trace the model
  417. tracer = NodePathTracer(**tracer_kwargs)
  418. graph = tracer.trace(model)
  419. name = model.__class__.__name__ if isinstance(model, nn.Module) else model.__name__
  420. graph_module = fx.GraphModule(tracer.root, graph, name)
  421. available_nodes = list(tracer.node_to_qualname.values())
  422. # FIXME We don't know if we should expect this to happen
  423. if len(set(available_nodes)) != len(available_nodes):
  424. raise ValueError(
  425. "There are duplicate nodes! Please raise an issue https://github.com/pytorch/vision/issues"
  426. )
  427. # Check that all outputs in return_nodes are present in the model
  428. for query in mode_return_nodes[mode].keys():
  429. # To check if a query is available we need to check that at least
  430. # one of the available names starts with it up to a .
  431. if not any([re.match(rf"^{query}(\.|$)", n) is not None for n in available_nodes]):
  432. raise ValueError(
  433. f"node: '{query}' is not present in model. Hint: use "
  434. "`get_graph_node_names` to make sure the "
  435. "`return_nodes` you specified are present. It may even "
  436. "be that you need to specify `train_return_nodes` and "
  437. "`eval_return_nodes` separately."
  438. )
  439. # Remove existing output nodes (train mode)
  440. orig_output_nodes = []
  441. for n in reversed(graph_module.graph.nodes):
  442. if n.op == "output":
  443. orig_output_nodes.append(n)
  444. if not orig_output_nodes:
  445. raise ValueError("No output nodes found in graph_module.graph.nodes")
  446. for n in orig_output_nodes:
  447. graph_module.graph.erase_node(n)
  448. # Find nodes corresponding to return_nodes and make them into output_nodes
  449. nodes = [n for n in graph_module.graph.nodes]
  450. output_nodes = OrderedDict()
  451. for n in reversed(nodes):
  452. module_qualname = tracer.node_to_qualname.get(n)
  453. if module_qualname is None:
  454. # NOTE - Know cases where this happens:
  455. # - Node representing creation of a tensor constant - probably
  456. # not interesting as a return node
  457. # - When packing outputs into a named tuple like in InceptionV3
  458. continue
  459. for query in mode_return_nodes[mode]:
  460. depth = query.count(".")
  461. if ".".join(module_qualname.split(".")[: depth + 1]) == query:
  462. output_nodes[mode_return_nodes[mode][query]] = n
  463. mode_return_nodes[mode].pop(query)
  464. break
  465. output_nodes = OrderedDict(reversed(list(output_nodes.items())))
  466. # And add them in the end of the graph
  467. with graph_module.graph.inserting_after(nodes[-1]):
  468. graph_module.graph.output(output_nodes)
  469. # Remove unused modules / parameters
  470. graph_module.graph.eliminate_dead_code()
  471. graph_module.recompile()
  472. # Keep track of the tracer and graph, so we can choose the main one
  473. tracers[mode] = tracer
  474. graphs[mode] = graph
  475. # Warn user if there are any discrepancies between the graphs of the
  476. # train and eval modes
  477. if not suppress_diff_warning:
  478. _warn_graph_differences(tracers["train"], tracers["eval"])
  479. # Build the final graph module
  480. graph_module = DualGraphModule(model, graphs["train"], graphs["eval"], class_name=name)
  481. # Restore original training mode
  482. model.train(is_training)
  483. graph_module.train(is_training)
  484. return graph_module