code: def requires_grad_ctx(
      module: Module, assignments: Dict[str, bool]
  ) -> Generator[None, None, None]:
      r"""Contextmanager for temporarily setting the requires_grad field of a module's
      parameters."""
      try:
          cache = {}
          for name, mode in assignments.items():
              parameter = module.get_parameter(name)
              cache[name] = parameter.requires_grad
              parameter.requires_grad_(mode)
          yield
      finally:
          for name, mode in cache.items():
              module.get_parameter(name).requires_grad_(mode)
  
file_path: botorch/botorch/utils/context_managers.py
imports: from __future__ import annotations
  from contextlib import contextmanager
  from typing import Any, Callable, Dict, Generator, Iterable, NamedTuple, Optional, Union
  from torch import device, dtype, Tensor
  from torch.nn import Module
label_desc: Use image node_id 2 for calling a global function with example usage: requires_grad_ctx(module, assignments) without return types