code: def freeze_module(
      module: torch.nn.Module, force_eval: bool = True
  ) -> torch.nn.Module:
      """
      Sets requires_grad = False on all the given module parameters, and put the module in eval mode.
      By default, it also overrides the module's `train` method to make sure that it always stays in eval mode
      (ie calling ``module.train(mode=True)`` executes ``module.train(mode=False)``)
  
      >>> module = nn.Linear(10, 20).train()
      >>> module.training
      True
      >>> module.weight.requires_grad
      True
      >>> freeze_module(module).train().training
      False
      >>> module.weight.requires_grad
      False
      """
  
      module.eval()
      for parameter in module.parameters():
          parameter.requires_grad = False
  
      if force_eval:
  
          def _force_train_in_eval(
              self: torch.nn.Module, mode: bool = True
          ) -> torch.nn.Module:
              # ignore train/eval calls: perpetually stays in eval
              return self
  
          module.train = MethodType(_force_train_in_eval, module)
  
      return module
  
file_path: ml-cvnets/cvnets/misc/common.py
imports: import argparse
  import os
  import re
  from types import MethodType
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  from torch import Tensor
  from utils import logger
  from utils.common_utils import unwrap_model_fn
  from utils.ddp_utils import is_master, is_start_rank_node
label_desc: Use image node_id 4 for calling a global function with example usage: freeze_module(module, force_eval) and returns: module, self