class_docstr: PyTorch Lightning callback to prune unpromising trials.
  
  See `the example <https://github.com/optuna/optuna-examples/blob/
  main/pytorch/pytorch_lightning_simple.py>`__
  if you want to add a pruning callback which observes accuracy.
  
  Args:
      trial:
          A :class:`~optuna.trial.Trial` corresponding to the current evaluation of the
          objective function.
      monitor:
          An evaluation metric for pruning, e.g., ``val_loss`` or
          ``val_acc``. The metrics are obtained from the returned dictionaries from e.g.
          ``lightning.pytorch.LightningModule.training_step`` or
          ``lightning.pytorch.LightningModule.validation_epoch_end`` and the names thus depend on
          how this dictionary is formatted.
  
  
  .. note::
      For the distributed data parallel training, the version of PyTorchLightning needs to be
      higher than or equal to v1.6.0. In addition, :class:`~optuna.study.Study` should be
      instantiated with RDB storage.
  
  
  .. note::
      If you would like to use PyTorchLightningPruningCallback in a distributed training
      environment, you need to evoke `PyTorchLightningPruningCallback.check_pruned()`
      manually so that :class:`~optuna.exceptions.TrialPruned` is properly handled.
code: def on_validation_end(
      self, trainer: Trainer, pl_module: LightningModule
  ) -> None:
      # Trainer calls `on_validation_end` for sanity check. Therefore, it is necessary to avoid
      # calling `trial.report` multiple times at epoch 0. For more details, see
      # https://github.com/PyTorchLightning/pytorch-lightning/issues/1391.
      if trainer.sanity_checking:
          return
  
      current_score = trainer.callback_metrics.get(self.monitor)
      if current_score is None:
          message = (
              f"The metric '{self.monitor}' is not in the evaluation logs for pruning. "
              "Please make sure you set the correct metric name."
          )
          warnings.warn(message)
          return
  
      epoch = pl_module.current_epoch
      should_stop = False
  
      # Determine if the trial should be terminated in a single process.
      if not self.is_ddp_backend:
          self._trial.report(current_score.item(), step=epoch)
          if not self._trial.should_prune():
              return
          raise optuna.TrialPruned(
              f"Trial was pruned at epoch {epoch}."
          )
  
      # Determine if the trial should be terminated in a DDP.
      if trainer.is_global_zero:
          self._trial.report(current_score.item(), step=epoch)
          should_stop = self._trial.should_prune()
  
          # Update intermediate value in the storage.
          _trial_id = self._trial._trial_id
          _study = self._trial.study
          _trial_system_attrs = _study._storage.get_trial_system_attrs(
              _trial_id
          )
          intermediate_values = _trial_system_attrs.get(
              _INTERMEDIATE_VALUE
          )
          intermediate_values[epoch] = current_score.item()  # type: ignore[index]
          self._trial.storage.set_trial_system_attr(
              self._trial._trial_id,
              _INTERMEDIATE_VALUE,
              intermediate_values,
          )
  
      # Terminate every process if any world process decides to stop.
      should_stop = trainer.strategy.broadcast(should_stop)
      trainer.should_stop = trainer.should_stop or should_stop
      if not should_stop:
          return
  
      if trainer.is_global_zero:
          # Update system_attr from global zero process.
          self._trial.storage.set_trial_system_attr(
              self._trial._trial_id, _PRUNED_KEY, True
          )
          self._trial.storage.set_trial_system_attr(
              self._trial._trial_id, _EPOCH_KEY, epoch
          )
  
file_path: optuna/optuna/integration/pytorch_lightning.py
imports: import warnings
  from packaging import version
  import optuna
  from optuna.storages._cached_storage import _CachedStorage
  from optuna.storages._rdb.storage import RDBStorage
label_desc: Use image node_id 3 for calling the PyTorchLightningPruningCallback obj's underlying member method code with example usage: obj.on_validation_end(trainer, pl_module) without return types