code: def freeze_training_mode(model):
      """
      A context manager that annotates the "training" attribute of every submodule
      to constant, so that the training codepath in these modules can be
      meta-compiled away. Upon exiting, the annotations are reverted.
      """
      classes = {type(x) for x in model.modules()}
      # __constants__ is the old way to annotate constants and not compatible
      # with __annotations__ .
      classes = {x for x in classes if not hasattr(x, "__constants__")}
      for cls in classes:
          cls.__annotations__["training"] = torch.jit.Final[bool]
      yield
      for cls in classes:
          cls.__annotations__["training"] = bool
  
file_path: detectron2/detectron2/export/torchscript_patch.py
imports: import os
  import sys
  import tempfile
  from contextlib import ExitStack, contextmanager
  from copy import deepcopy
  from unittest import mock
  import torch
  from torch import nn
  import detectron2
  from detectron2.structures import Boxes, Instances
  from detectron2.utils.env import _import_file
label_desc: Use image node_id 9 for calling a global function with example usage: freeze_training_mode(model) without return types