class_docstr: A training loop.
class_docstr_tok: ["A","training","loop","."]
docstr: Save the state of the training loop.
  
  :param path:
      Path of the file where to store the state in.
  :param stopper:
      An instance of :class:`pykeen.stopper.EarlyStopper` with settings for checking
      if training should stop early
  :param best_epoch_model_checkpoint_file_path:
      The file path for the checkpoint of the best epoch model when using early stopping.
  :param triples_factory:
      The triples factory being used in the current training loop.
  :raises ValueError: if the internal optimizer is not set
docstr_tok: ["Save","the","state","of","the","training","loop",".",":","param","path",":","Path","of","the","file","where","to","store","the","state","in",".",":","param","stopper",":","An","instance","of",":","class",":","`","pykeen.stopper.EarlyStopper","`","with","settings","for","checking","if","training","should","stop","early",":","param","best_epoch_model_checkpoint_file_path",":","The","file","path","for","the","checkpoint","of","the","best","epoch","model","when","using","early","stopping",".",":","param","triples_factory",":","The","triples","factory","being","used","in","the","current","training","loop",".",":","raises","ValueError",":","if","the","internal","optimizer","is","not","set"]
code: def _save_state(
      self,
      path: Union[IO[bytes], str, pathlib.Path],
      stopper: Optional[Stopper] = None,
      best_epoch_model_checkpoint_file_path: Optional[
          pathlib.Path
      ] = None,
      triples_factory: Optional[CoreTriplesFactory] = None,
  ) -> None:
      """Save the state of the training loop.
  
      :param path:
          Path of the file where to store the state in.
      :param stopper:
          An instance of :class:`pykeen.stopper.EarlyStopper` with settings for checking
          if training should stop early
      :param best_epoch_model_checkpoint_file_path:
          The file path for the checkpoint of the best epoch model when using early stopping.
      :param triples_factory:
          The triples factory being used in the current training loop.
      :raises ValueError: if the internal optimizer is not set
      """
      if self.optimizer is None:
          raise ValueError
  
      logger.debug("=> Saving checkpoint.")
  
      if stopper is None:
          stopper_dict: Mapping[str, Any] = dict()
      else:
          stopper_dict = stopper.get_summary_dict()
  
      # Only if a cuda device is available, the random state is accessed
      if torch.cuda.is_available():
          torch_cuda_random_state = torch.cuda.get_rng_state()
      else:
          torch_cuda_random_state = None
  
      if best_epoch_model_checkpoint_file_path is not None:
          best_epoch_model_checkpoint = torch.load(
              best_epoch_model_checkpoint_file_path
          )
      else:
          best_epoch_model_checkpoint = None
  
      if self.lr_scheduler is None:
          lr_scheduler_state_dict = None
      else:
          lr_scheduler_state_dict = self.lr_scheduler.state_dict()
  
      relation_to_id_dict = None
      entity_to_id_dict = None
      if triples_factory is not None and isinstance(
          triples_factory, TriplesFactory
      ):
          relation_to_id_dict = triples_factory.relation_to_id
          entity_to_id_dict = triples_factory.entity_to_id
  
      torch.save(
          {
              "epoch": self._epoch,
              "loss": self.losses_per_epochs,
              "model_state_dict": self.model.state_dict(),
              "optimizer_state_dict": self.optimizer.state_dict(),
              "lr_scheduler_state_dict": lr_scheduler_state_dict,
              "checksum": self.checksum,
              "random_seed": self.model._random_seed,
              "stopper_dict": stopper_dict,
              "random_state": random.getstate(),
              "np_random_state": np.random.get_state(),
              "torch_random_state": torch.random.get_rng_state(),
              "torch_cuda_random_state": torch_cuda_random_state,
              # This is an entire checkpoint for the optional best model when using early stopping
              "best_epoch_model_checkpoint": best_epoch_model_checkpoint,
              # Saving triples factory related states
              "relation_to_id_dict": relation_to_id_dict,
              "entity_to_id_dict": entity_to_id_dict,
          },
          path,
          pickle_protocol=pickle.HIGHEST_PROTOCOL,
      )
      logger.info(
          f"=> Saved checkpoint after having finished epoch {self._epoch}."
      )
  
imports: import gc
  import logging
  import os
  import pathlib
  import pickle
  import random
  import time
  from abc import ABC, abstractmethod
  from contextlib import ExitStack
  from datetime import datetime
  from hashlib import md5
  from tempfile import NamedTemporaryFile, TemporaryDirectory
  from typing import IO, Any, ClassVar, Generic, List, Mapping, Optional, Tuple, TypeVar, Union
  import numpy
  import torch
  from class_resolver import HintOrType, OptionalKwargs
  from class_resolver.contrib.torch import lr_scheduler_resolver, optimizer_resolver
  from torch.optim.optimizer import Optimizer
  from torch.utils.data import DataLoader
  from tqdm.autonotebook import tqdm, trange
  from .callbacks import GradientAbsClippingTrainingCallback, GradientNormClippingTrainingCallback, LearningRateSchedulerTrainingCallback, MultiTrainingCallback, OptimizerTrainingCallback, StopperTrainingCallback, TrackerTrainingCallback, TrainingCallback, TrainingCallbackHint, TrainingCallbackKwargsHint
  from ..constants import PYKEEN_CHECKPOINTS, PYKEEN_DEFAULT_CHECKPOINT
  from ..lr_schedulers import LRScheduler
  from ..models import RGCN, Model
  from ..stoppers import Stopper
  from ..trackers import ResultTracker, tracker_resolver
  from ..triples import CoreTriplesFactory, TriplesFactory
  from ..typing import InductiveMode
  from ..utils import format_relative_comparison, get_batchnorm_modules, get_preferred_device, is_cuda_oom_error, is_cudnn_error, normalize_string
label_desc: Use image node_id 18 for calling the TrainingLoop obj's underlying member method code with example usage: obj._save_state(path, stopper, best_epoch_model_checkpoint_file_path, triples_factory) without return types