class_docstr: A DataModule standardizes the training, val, test splits, data preparation and transforms. The main advantage is
  consistent data splits, data preparation and transforms across models.
  
  Example::
  
      import lightning as L
      import torch.utils.data as data
      from lightning.pytorch.demos.boring_classes import RandomDataset
  
      class MyDataModule(L.LightningDataModule):
          def prepare_data(self):
              # download, IO, etc. Useful with shared filesystems
              # only called on 1 GPU/TPU in distributed
              ...
  
          def setup(self, stage):
              # make assignments here (val/train/test split)
              # called on every process in DDP
              dataset = RandomDataset(1, 100)
              self.train, self.val, self.test = data.random_split(
                  dataset, [80, 10, 10], generator=torch.Generator().manual_seed(42)
              )
  
          def train_dataloader(self):
              return data.DataLoader(self.train)
  
          def val_dataloader(self):
              return data.DataLoader(self.val)
  
          def test_dataloader(self):
              return data.DataLoader(self.test)
  
          def teardown(self):
              # clean up state after the trainer stops, delete files...
              # called on every process in DDP
              ...
class_docstr_tok: ["A","DataModule","standardizes","the","training",",","val",",","test","splits",",","data","preparation","and","transforms",".","The","main","advantage","is","consistent","data","splits",",","data","preparation","and","transforms","across","models",".","Example",":",":","import","lightning","as","L","import","torch.utils.data","as","data","from","lightning.pytorch.demos.boring_classes","import","RandomDataset","class","MyDataModule","(","L.LightningDataModule",")",":","def","prepare_data","(","self",")",":","#","download",",","IO",",","etc",".","Useful","with","shared","filesystems","#","only","called","on","1","GPU\/TPU","in","distributed","...","def","setup","(","self",",","stage",")",":","#","make","assignments","here","(","val\/train\/test","split",")","#","called","on","every","process","in","DDP","dataset","=","RandomDataset","(","1",",","100",")","self.train",",","self.val",",","self.test","=","data.random_split","(","dataset",",","[","80",",","10",",","10","]",",","generator=torch.Generator","(",")",".manual_seed","(","42",")",")","def","train_dataloader","(","self",")",":","return","data.DataLoader","(","self.train",")","def","val_dataloader","(","self",")",":","return","data.DataLoader","(","self.val",")","def","test_dataloader","(","self",")",":","return","data.DataLoader","(","self.test",")","def","teardown","(","self",")",":","#","clean","up","state","after","the","trainer","stops",",","delete","files","...","#","called","on","every","process","in","DDP","..."]
docstr: Create an instance from torch.utils.data.Dataset.
  
  Args:
      train_dataset: Optional dataset or iterable of datasets to be used for train_dataloader()
      val_dataset: Optional dataset or iterable of datasets to be used for val_dataloader()
      test_dataset: Optional dataset or iterable of datasets to be used for test_dataloader()
      predict_dataset: Optional dataset or iterable of datasets to be used for predict_dataloader()
      batch_size: Batch size to use for each dataloader. Default is 1. This parameter gets forwarded to the
          ``__init__`` if the datamodule has such a name defined in its signature.
      num_workers: Number of subprocesses to use for data loading. 0 means that the
          data will be loaded in the main process. Number of CPUs available. This parameter gets forwarded to the
          ``__init__`` if the datamodule has such a name defined in its signature.
      **datamodule_kwargs: Additional parameters that get passed down to the datamodule's ``__init__``.
docstr_tok: ["Create","an","instance","from","torch.utils.data.Dataset",".","Args",":","train_dataset",":","Optional","dataset","or","iterable","of","datasets","to","be","used","for","train_dataloader","(",")","val_dataset",":","Optional","dataset","or","iterable","of","datasets","to","be","used","for","val_dataloader","(",")","test_dataset",":","Optional","dataset","or","iterable","of","datasets","to","be","used","for","test_dataloader","(",")","predict_dataset",":","Optional","dataset","or","iterable","of","datasets","to","be","used","for","predict_dataloader","(",")","batch_size",":","Batch","size","to","use","for","each","dataloader",".","Default","is","1",".","This","parameter","gets","forwarded","to","the","``","__init__","``","if","the","datamodule","has","such","a","name","defined","in","its","signature",".","num_workers",":","Number","of","subprocesses","to","use","for","data","loading",".","0","means","that","the","data","will","be","loaded","in","the","main","process",".","Number","of","CPUs","available",".","This","parameter","gets","forwarded","to","the","``","__init__","``","if","the","datamodule","has","such","a","name","defined","in","its","signature",".","*","*","datamodule_kwargs",":","Additional","parameters","that","get","passed","down","to","the","datamodule","'s","``","__init__","``","."]
code: def from_datasets(
      cls,
      train_dataset: Optional[Union[Dataset, Iterable[Dataset]]] = None,
      val_dataset: Optional[Union[Dataset, Iterable[Dataset]]] = None,
      test_dataset: Optional[Union[Dataset, Iterable[Dataset]]] = None,
      predict_dataset: Optional[
          Union[Dataset, Iterable[Dataset]]
      ] = None,
      batch_size: int = 1,
      num_workers: int = 0,
      **datamodule_kwargs: Any,
  ) -> "LightningDataModule":
      r"""Create an instance from torch.utils.data.Dataset.
  
      Args:
          train_dataset: Optional dataset or iterable of datasets to be used for train_dataloader()
          val_dataset: Optional dataset or iterable of datasets to be used for val_dataloader()
          test_dataset: Optional dataset or iterable of datasets to be used for test_dataloader()
          predict_dataset: Optional dataset or iterable of datasets to be used for predict_dataloader()
          batch_size: Batch size to use for each dataloader. Default is 1. This parameter gets forwarded to the
              ``__init__`` if the datamodule has such a name defined in its signature.
          num_workers: Number of subprocesses to use for data loading. 0 means that the
              data will be loaded in the main process. Number of CPUs available. This parameter gets forwarded to the
              ``__init__`` if the datamodule has such a name defined in its signature.
          **datamodule_kwargs: Additional parameters that get passed down to the datamodule's ``__init__``.
  
      """
  
      def dataloader(ds: Dataset, shuffle: bool = False) -> DataLoader:
          shuffle &= not isinstance(ds, IterableDataset)
          return DataLoader(
              ds,
              batch_size=batch_size,
              shuffle=shuffle,
              num_workers=num_workers,
              pin_memory=True,
          )
  
      def train_dataloader() -> TRAIN_DATALOADERS:
          return apply_to_collection(
              train_dataset, Dataset, dataloader, shuffle=True
          )
  
      def val_dataloader() -> EVAL_DATALOADERS:
          return apply_to_collection(val_dataset, Dataset, dataloader)
  
      def test_dataloader() -> EVAL_DATALOADERS:
          return apply_to_collection(test_dataset, Dataset, dataloader)
  
      def predict_dataloader() -> EVAL_DATALOADERS:
          return apply_to_collection(
              predict_dataset, Dataset, dataloader
          )
  
      candidate_kwargs = {
          "batch_size": batch_size,
          "num_workers": num_workers,
      }
      accepted_params = inspect.signature(cls.__init__).parameters
      accepts_kwargs = any(
          param.kind == param.VAR_KEYWORD
          for param in accepted_params.values()
      )
      if accepts_kwargs:
          special_kwargs = candidate_kwargs
      else:
          accepted_param_names = set(accepted_params)
          accepted_param_names.discard("self")
          special_kwargs = {
              k: v
              for k, v in candidate_kwargs.items()
              if k in accepted_param_names
          }
  
      datamodule = cls(**datamodule_kwargs, **special_kwargs)
      if train_dataset is not None:
          datamodule.train_dataloader = train_dataloader  # type: ignore[method-assign]
      if val_dataset is not None:
          datamodule.val_dataloader = val_dataloader  # type: ignore[method-assign]
      if test_dataset is not None:
          datamodule.test_dataloader = test_dataloader  # type: ignore[method-assign]
      if predict_dataset is not None:
          datamodule.predict_dataloader = predict_dataloader  # type: ignore[method-assign]
      return datamodule
  
file_path: pytorch-lightning/src/lightning/pytorch/core/datamodule.py
imports: import inspect
  from typing import IO, Any, Dict, Iterable, Optional, Union, cast
  from lightning_utilities import apply_to_collection
  from torch.utils.data import DataLoader, Dataset, IterableDataset
  from typing_extensions import Self
  import lightning.pytorch
  from lightning.fabric.utilities.types import _MAP_LOCATION_TYPE, _PATH
  from lightning.pytorch.core.hooks import DataHooks
  from lightning.pytorch.core.mixins import HyperparametersMixin
  from lightning.pytorch.core.saving import _load_from_checkpoint
  from lightning.pytorch.utilities.model_helpers import _restricted_classmethod
  from lightning.pytorch.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
label_desc: Use image node_id 2 for calling the LightningDataModule obj's underlying member method code with example usage: obj.from_datasets(cls, train_dataset, val_dataset, test_dataset, predict_dataset, batch_size, num_workers) and returns: datamodule, DataLoader, apply_to_collection, apply_to_collection, apply_to_collection, apply_to_collection