class_docstr: Implementation of a configurable command line tool for pytorch-lightning.
class_docstr_tok: ["Implementation","of","a","configurable","command","line","tool","for","pytorch-lightning","."]
docstr: Adds to the model an automatically generated configure_optimizers method.
  
  If a single optimizer and optionally a scheduler argument groups are added to the parser as 'AUTOMATIC', then a
  `configure_optimizers` method is automatically implemented in the model class.
code: def add_configure_optimizers_method_to_model(self) -> None:
      """Adds to the model an automatically generated configure_optimizers method.
  
      If a single optimizer and optionally a scheduler argument groups are added to the parser as 'AUTOMATIC', then a
      `configure_optimizers` method is automatically implemented in the model class.
  
      """
  
      def get_automatic(
          class_type: Union[Type, Tuple[Type, ...]]
      ) -> List[str]:
          automatic = []
          for key, (
              base_class,
              link_to,
          ) in self.parser.optimizers_and_lr_schedulers.items():
              if not isinstance(base_class, tuple):
                  base_class = (base_class,)
              if link_to == "AUTOMATIC" and any(
                  issubclass(c, class_type) for c in base_class
              ):
                  automatic.append(key)
          return automatic
  
      optimizers = get_automatic(Optimizer)
      lr_schedulers = get_automatic(LRSchedulerTypeTuple)
  
      if len(optimizers) == 0:
          return
  
      if len(optimizers) > 1 or len(lr_schedulers) > 1:
          raise ValueError(
              f"`{self.__class__.__name__}.add_configure_optimizers_method_to_model` expects at most one optimizer "
              f"and one lr_scheduler to be 'AUTOMATIC', but found {optimizers+lr_schedulers}. In this case the user "
              "is expected to link the argument groups and implement `configure_optimizers`, see "
              "https://pytorch-lightning.readthedocs.io/en/stable/common/lightning_cli.html"
              "#optimizers-and-learning-rate-schedulers"
          )
  
      if is_overridden("configure_optimizers", self.model):
          warnings.warn(
              f"`{self.model.__class__.__name__}.configure_optimizers` will be overridden by "
              f"`{self.__class__.__name__}.add_configure_optimizers_method_to_model`."
          )
  
      optimizer_class = self.parser.optimizers_and_lr_schedulers[
          optimizers[0]
      ][0]
      optimizer_init = self.config_init.get(optimizers[0], {})
      if not isinstance(optimizer_class, tuple):
          optimizer_init = _global_add_class_path(
              optimizer_class, optimizer_init
          )
      lr_scheduler_init = None
      if lr_schedulers:
          lr_scheduler_class = self.parser.optimizers_and_lr_schedulers[
              lr_schedulers[0]
          ][0]
          lr_scheduler_init = self.config_init.get(lr_schedulers[0], {})
          if not isinstance(lr_scheduler_class, tuple):
              lr_scheduler_init = _global_add_class_path(
                  lr_scheduler_class, lr_scheduler_init
              )
  
      def configure_optimizers(
          self: LightningModule,
      ) -> Union[
          Optimizer, Tuple[List[Optimizer], List[LRSchedulerType]]
      ]:
          optimizer = instantiate_class(
              self.parameters(), optimizer_init
          )
          if not lr_scheduler_init:
              return optimizer
          lr_scheduler = instantiate_class(optimizer, lr_scheduler_init)
          return [optimizer], [lr_scheduler]
  
      self.model.configure_optimizers = MethodType(
          configure_optimizers, self.model
      )
  
imports: import inspect
  import os
  import warnings
  from argparse import Namespace
  from types import MethodType
  from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union, cast
  import torch
  from jsonargparse import ActionConfigFile, ArgumentParser, class_from_function, set_config_read_mode
  from pytorch_lightning import LightningDataModule, LightningModule, Trainer
  from pytorch_lightning.callbacks import Callback
  from pytorch_lightning.utilities.cloud_io import get_filesystem
  from pytorch_lightning.utilities.model_helpers import is_overridden
  from pytorch_lightning.utilities.seed import seed_everything
  from torch.optim import Optimizer
label_desc: Use image node_id 10 for calling the LightningCLI obj's underlying member method code with example usage: obj.add_configure_optimizers_method_to_model() and returns: automatic, list,  list, optimizer