# Buggy code
def fit(
    self,
    model: LightningModule,
    train_dataloader: Optional[DataLoader] = None,
    val_dataloaders: Optional[DataLoader] = None,
):
    r"""
    Runs the full optimization routine.

    Args:
        model: Model to fit.

        train_dataloader: A Pytorch
            DataLoader with training samples. If the model has
            a predefined train_dataloader method this will be skipped.

        val_dataloaders: Either a single
            Pytorch Dataloader or a list of them, specifying validation samples.
            If the model has a predefined val_dataloaders method this will be skipped

    Example::

        # Option 1,
        # Define the train_dataloader() and val_dataloader() fxs
        # in the lightningModule
        # RECOMMENDED FOR MOST RESEARCH AND APPLICATIONS TO MAINTAIN READABILITY
        trainer = Trainer()
        model = LightningModule()
        trainer.fit(model)

        # Option 2
        # in production cases we might want to pass different datasets to the same model
        # Recommended for PRODUCTION SYSTEMS
        train, val = DataLoader(...), DataLoader(...)
        trainer = Trainer()
        model = LightningModule()
        trainer.fit(model, train_dataloader=train, val_dataloader=val)

        # Option 1 & 2 can be mixed, for example the training set can be
        # defined as part of the model, and validation can then be feed to .fit()

    """
    # bind logger and other properties
    model.logger = self.logger
    self.copy_trainer_model_properties(model)

    # clean hparams
    if hasattr(model, "hparams"):
        parsing.clean_namespace(model.hparams)

    # set up the passed in dataloaders (if needed)
    self.__attach_dataloaders(model, train_dataloader, val_dataloaders)

    # check that model is configured correctly
    self.check_model_configuration(model)

    # download the data and do whatever transforms we need
    # do before any spawn calls so that the model can assign properties
    # only on proc 0 because no spawn has happened yet
    model.prepare_data()

    # Run learning rate finder:
    if self.auto_lr_find:
        self._run_lr_finder_internally(model)

    # route to appropriate start method
    # when using multi-node or DDP within a node start each module in a separate process
    if self.use_ddp2:
        task = int(os.environ["SLURM_LOCALID"])
        self.ddp_train(task, model)

    elif self.use_ddp:
        if self.is_slurm_managing_tasks:
            task = int(os.environ["SLURM_LOCALID"])
            self.ddp_train(task, model)
        else:
            self.__set_random_port()
            # track for predict
            self.model = model
            # train
            mp.spawn(self.ddp_train, nprocs=self.num_processes, args=(model,))
            # load weights if not interrupted
            self.load_spawn_weights(model)
            self.model = model

    # 1 gpu or dp option triggers training using DP module
    # easier to avoid NCCL issues
    elif self.use_dp:
        self.dp_train(model)

    elif self.use_horovod:
        self.horovod_train(model)

    elif self.single_gpu:
        self.single_gpu_train(model)

    elif self.use_tpu:  # pragma: no-cover
        log.info(f"training on {self.num_tpu_cores} TPU cores")

        #  COLAB_GPU is an env var available by default in Colab environments.
        start_method = (
            "fork"
            if os.getenv("COLAB_GPU") or os.getenv("KAGGLE_URL_BASE")
            else "spawn"
        )

        # track for predict
        self.model = model

        # train
        xmp.spawn(
            self.tpu_train,
            args=(model,),
            nprocs=self.num_tpu_cores,
            start_method=start_method,
        )

        # load weights if not interrupted
        self.load_spawn_weights(model)
        self.model = model

    # ON CPU
    else:
        # run through amp wrapper
        if self.use_amp:
            raise MisconfigurationException(
                "amp + cpu is not supported.  Please use a GPU option"
            )

        # CHOOSE OPTIMIZER
        # allow for lr schedulers as well
        self.optimizers, self.lr_schedulers, self.optimizer_frequencies = (
            self.init_optimizers(model)
        )

        self.run_pretrain_routine(model)

    # return 1 when finished
    # used for testing or when we need to know that training succeeded
    return 1


# Traceback (FileNotFoundError)
INFO:lightning:GPU available: True, used: True
INFO:lightning:VISIBLE GPUS: 0,1
INFO:lightning:GPU available: True, used: True
INFO:lightning:VISIBLE GPUS: 0,1
INFO:lightning:GPU available: True, used: True
INFO:lightning:VISIBLE GPUS: 0,1
INFO:lightning:GPU available: True, used: True
INFO:lightning:VISIBLE GPUS: 0,1
/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/warnings.py:18: RuntimeWarning: You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to define `test_epoch_end()` for accumulating stats.
warnings.warn(*args, **kwargs)
/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/warnings.py:18: RuntimeWarning: You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to define `test_epoch_end()` for accumulating stats.
warnings.warn(*args, **kwargs)
/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/warnings.py:18: RuntimeWarning: You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to define `test_epoch_end()` for accumulating stats.
warnings.warn(*args, **kwargs)
/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/warnings.py:18: RuntimeWarning: You have defined a `test_dataloader()` and have defined a `test_step()`, you may also want to define `test_epoch_end()` for accumulating stats.
warnings.warn(*args, **kwargs)
d-12-3-1:47016:47016 [0] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.132<0>
d-12-3-1:47016:47016 [0] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-3-1:47016:47016 [0] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.132<0>
NCCL version 2.4.8+cuda10.1
d-12-3-1:47022:47022 [1] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.132<0>
d-12-3-1:47022:47022 [1] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-4-1:51815:51815 [0] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.134<0>
d-12-4-1:51820:51820 [1] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.134<0>
d-12-4-1:51820:51820 [1] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-4-1:51815:51815 [0] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-4-2:51993:51993 [0] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.135<0>
d-12-4-2:51997:51997 [1] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.135<0>
d-12-3-2:43991:43991 [1] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.133<0>
d-12-3-2:43985:43985 [0] NCCL INFO Bootstrap : Using [0]ens2f0:172.31.130.133<0>
d-12-4-2:51993:51993 [0] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-4-2:51997:51997 [1] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-3-2:43991:43991 [1] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-3-2:43985:43985 [0] NCCL INFO NET/Plugin : No plugin found (libnccl-net.so).
d-12-3-1:47022:47022 [1] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.132<0>
d-12-3-1:47022:47036 [1] NCCL INFO Setting affinity for GPU 1 to ffff,f00000ff,fff00000
d-12-4-2:51993:51993 [0] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.135<0>
d-12-4-2:51997:51997 [1] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.135<0>
d-12-4-1:51820:51820 [1] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.134<0>
d-12-3-2:43991:43991 [1] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.133<0>
d-12-3-2:43985:43985 [0] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.133<0>
d-12-4-1:51815:51815 [0] NCCL INFO NET/IB : Using [0]mlx5_0:1/RoCE ; OOB ens2f0:172.31.130.134<0>
d-12-4-2:51993:52008 [0] NCCL INFO Setting affinity for GPU 0 to ffff,f00000ff,fff00000
d-12-4-2:51997:52010 [1] NCCL INFO Setting affinity for GPU 1 to ffff,f00000ff,fff00000
d-12-4-1:51820:51830 [1] NCCL INFO Setting affinity for GPU 1 to ffff,f00000ff,fff00000
d-12-3-2:43991:44003 [1] NCCL INFO Setting affinity for GPU 1 to ffff,f00000ff,fff00000
d-12-3-2:43985:44002 [0] NCCL INFO Setting affinity for GPU 0 to ffff,f00000ff,fff00000
d-12-4-1:51815:51832 [0] NCCL INFO Setting affinity for GPU 0 to ffff,f00000ff,fff00000
d-12-3-1:47016:47034 [0] NCCL INFO Setting affinity for GPU 0 to ffff,f00000ff,fff00000
d-12-4-2:51997:52010 [1] NCCL INFO CUDA Dev 1[1], IB NIC distance :  NODE
d-12-4-2:51993:52008 [0] NCCL INFO CUDA Dev 0[0], IB NIC distance :  NODE
d-12-4-1:51815:51832 [0] NCCL INFO CUDA Dev 0[0], IB NIC distance :  NODE
d-12-4-1:51820:51830 [1] NCCL INFO CUDA Dev 1[1], IB NIC distance :  NODE
d-12-3-2:43991:44003 [1] NCCL INFO CUDA Dev 1[1], IB NIC distance :  NODE
d-12-3-2:43985:44002 [0] NCCL INFO CUDA Dev 0[0], IB NIC distance :  NODE
d-12-3-1:47022:47036 [1] NCCL INFO CUDA Dev 1[1], IB NIC distance :  NODE
d-12-3-1:47016:47034 [0] NCCL INFO CUDA Dev 0[0], IB NIC distance :  NODE
d-12-3-1:47016:47034 [0] NCCL INFO Channel 00 :    0   1   2   3   4   5   6   7
d-12-3-1:47016:47034 [0] NCCL INFO Ring 00 : 7 -> 0 [receive] via NET/IB/0
d-12-4-2:51993:52008 [0] NCCL INFO Ring 00 : 5 -> 6 [receive] via NET/IB/0
d-12-3-2:43985:44002 [0] NCCL INFO Ring 00 : 1 -> 2 [receive] via NET/IB/0
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 3 -> 4 [receive] via NET/IB/0
d-12-3-2:43985:44002 [0] NCCL INFO Ring 00 : 2[0] -> 3[1] via direct shared memory
d-12-4-2:51993:52008 [0] NCCL INFO Ring 00 : 6[0] -> 7[1] via direct shared memory
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 4[0] -> 5[1] via direct shared memory
d-12-3-1:47016:47034 [0] NCCL INFO Ring 00 : 0[0] -> 1[1] via direct shared memory
d-12-4-2:51997:52010 [1] NCCL INFO Ring 00 : 7 -> 0 [send] via NET/IB/0
d-12-3-2:43991:44003 [1] NCCL INFO Ring 00 : 3 -> 4 [send] via NET/IB/0
d-12-4-1:51820:51830 [1] NCCL INFO Ring 00 : 5 -> 6 [send] via NET/IB/0
d-12-3-1:47022:47036 [1] NCCL INFO Ring 00 : 1 -> 2 [send] via NET/IB/0
d-12-3-2:43991:44003 [1] NCCL INFO Ring 00 : 3[1] -> 2[0] via direct shared memory
d-12-4-1:51820:51830 [1] NCCL INFO Ring 00 : 5[1] -> 4[0] via direct shared memory
d-12-4-2:51997:52010 [1] NCCL INFO Ring 00 : 7[1] -> 6[0] via direct shared memory
d-12-3-1:47022:47036 [1] NCCL INFO Ring 00 : 1[1] -> 0[0] via direct shared memory
d-12-4-1:51820:51830 [1] NCCL INFO Trees [0] 4->5->-1/-1/-1
d-12-4-2:51997:52010 [1] NCCL INFO Trees [0] 6->7->-1/-1/-1
d-12-3-2:43991:44003 [1] NCCL INFO Trees [0] 2->3->-1/-1/-1
d-12-3-1:47022:47036 [1] NCCL INFO Trees [0] 0->1->-1/-1/-1
d-12-4-1:51820:51830 [1] NCCL INFO comm 0x7fa5380022f0 rank 5 nranks 8 cudaDev 1 nvmlDev 1 - Init COMPLETE
d-12-3-2:43991:44003 [1] NCCL INFO comm 0x7ff3f40022f0 rank 3 nranks 8 cudaDev 1 nvmlDev 1 - Init COMPLETE
d-12-4-2:51997:52010 [1] NCCL INFO comm 0x7f84600022f0 rank 7 nranks 8 cudaDev 1 nvmlDev 1 - Init COMPLETE
d-12-3-1:47022:47036 [1] NCCL INFO comm 0x7f1f780022f0 rank 1 nranks 8 cudaDev 1 nvmlDev 1 - Init COMPLETE
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 2 -> 4 [receive] via NET/IB/0
d-12-3-2:43985:44002 [0] NCCL INFO Ring 00 : 2 -> 4 [send] via NET/IB/0
d-12-4-2:51993:52008 [0] NCCL INFO Ring 00 : 6 -> 4 [send] via NET/IB/0
d-12-3-1:47016:47034 [0] NCCL INFO Ring 00 : 4 -> 0 [receive] via NET/IB/0
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 6 -> 4 [receive] via NET/IB/0
d-12-3-2:43985:44002 [0] NCCL INFO Ring 00 : 4 -> 2 [receive] via NET/IB/0
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 4 -> 0 [send] via NET/IB/0
d-12-4-2:51993:52008 [0] NCCL INFO Ring 00 : 4 -> 6 [receive] via NET/IB/0
d-12-3-1:47016:47034 [0] NCCL INFO Ring 00 : 0 -> 4 [send] via NET/IB/0
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 0 -> 4 [receive] via NET/IB/0
d-12-3-1:47016:47034 [0] NCCL INFO Trees [0] -1->0->1/4/-1
d-12-3-1:47016:47034 [0] NCCL INFO Using 256 threads, Min Comp Cap 7, Trees enabled up to size 79999
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 4 -> 2 [send] via NET/IB/0
d-12-3-1:47016:47034 [0] NCCL INFO comm 0x7fa36c0022f0 rank 0 nranks 8 cudaDev 0 nvmlDev 0 - Init COMPLETE
d-12-3-1:47016:47016 [0] NCCL INFO Launch mode Parallel
d-12-4-1:51815:51832 [0] NCCL INFO Ring 00 : 4 -> 6 [send] via NET/IB/0
d-12-3-2:43985:44002 [0] NCCL INFO Trees [0] 4->2->3/-1/-1
d-12-3-2:43985:44002 [0] NCCL INFO comm 0x7febb00022f0 rank 2 nranks 8 cudaDev 0 nvmlDev 0 - Init COMPLETE
d-12-4-1:51815:51832 [0] NCCL INFO Trees [0] 0->4->5/2/6
d-12-4-2:51993:52008 [0] NCCL INFO Trees [0] 4->6->7/-1/-1
d-12-4-1:51815:51832 [0] NCCL INFO comm 0x7fab7c0022f0 rank 4 nranks 8 cudaDev 0 nvmlDev 0 - Init COMPLETE
d-12-4-2:51993:52008 [0] NCCL INFO comm 0x7f2bec0022f0 rank 6 nranks 8 cudaDev 0 nvmlDev 0 - Init COMPLETE
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
INFO:lightning:
| Name    | Type   | Params
-------------------------------
0 | layer_1 | Linear | 100 K
1 | layer_2 | Linear | 33 K
2 | layer_3 | Linear | 2 K
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
INFO:lightning:Set SLURM handle signals.
spr= 5 snr= 2 sng= 2 gpu_idx= 1
rank= 5  world_size= 8
Root node= d-12-3-1
spr= 6 snr= 3 sng= 2 gpu_idx= 0
rank= 6  world_size= 8
Root node= d-12-3-1
spr= 0 snr= 0 sng= 2 gpu_idx= 0
rank= 0  world_size= 8
Root node= d-12-3-1
spr= 3 snr= 1 sng= 2 gpu_idx= 1
rank= 3  world_size= 8
Root node= d-12-3-1
spr= 4 snr= 2 sng= 2 gpu_idx= 0
rank= 4  world_size= 8
Root node= d-12-3-1
spr= 1 snr= 0 sng= 2 gpu_idx= 1
rank= 1  world_size= 8
Root node= d-12-3-1
spr= 2 snr= 1 sng= 2 gpu_idx= 0
rank= 2  world_size= 8
Root node= d-12-3-1
spr= 7 snr= 3 sng= 2 gpu_idx= 1
rank= 7  world_size= 8
Root node= d-12-3-1
/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/utilities/warnings.py:18: RuntimeWarning: Displayed epoch numbers in the progress bar start from "1" until v0.6.x, but will start from "0" in v0.8.0.
warnings.warn(*args, **kwargs)
Epoch 2: 100%|##########| 118/118 [00:19<00:00,  5.98it/s, loss=0.197, v_num=436408]
before lsw: self.proc_rank= 0
load_spawn_weights called for self.proc_rank= 0
before lsw: self.proc_rank= 0
load_spawn_weights called for self.proc_rank= 0
Traceback (most recent call last):
File "pl.py", line 119, in <module>
main()
File "pl.py", line 111, in main
trainer.fit(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 697, in fit
self.load_spawn_weights(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/distrib_data_parallel.py", line 376, in load_spawn_weights
loaded_model = original_model.__class__.load_from_checkpoint(path)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1504, in load_from_checkpoint
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 525, in load
with _open_file_like(f, 'rb') as opened_file:
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 212, in _open_file_like
return _open_file(name_or_buffer, mode)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 193, in __init__
super(_open_file, self).__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: '/home/gridsan/groups/anaconda/dpc/lightning/__temp_weight_ddp_end.ckpt'
before lsw: self.proc_rank= 0
load_spawn_weights called for self.proc_rank= 0
Traceback (most recent call last):
File "pl.py", line 119, in <module>
main()
File "pl.py", line 111, in main
trainer.fit(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 697, in fit
self.load_spawn_weights(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/distrib_data_parallel.py", line 376, in load_spawn_weights
loaded_model = original_model.__class__.load_from_checkpoint(path)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1504, in load_from_checkpoint
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 525, in load
with _open_file_like(f, 'rb') as opened_file:
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 212, in _open_file_like
return _open_file(name_or_buffer, mode)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 193, in __init__
super(_open_file, self).__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: '/home/gridsan/groups/anaconda/dpc/lightning/__temp_weight_ddp_end.ckpt'
before lsw: self.proc_rank= 0
load_spawn_weights called for self.proc_rank= 0
Traceback (most recent call last):
File "pl.py", line 119, in <module>
main()
File "pl.py", line 111, in main
trainer.fit(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 697, in fit
self.load_spawn_weights(model)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/trainer/distrib_data_parallel.py", line 376, in load_spawn_weights
loaded_model = original_model.__class__.load_from_checkpoint(path)
File "/home/gridsan/dcampbell/.local/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1504, in load_from_checkpoint
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 525, in load
with _open_file_like(f, 'rb') as opened_file:
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 212, in _open_file_like
return _open_file(name_or_buffer, mode)
File "/state/partition1/llgrid/pkg/anaconda/anaconda3-2020b/lib/python3.6/site-packages/torch/serialization.py", line 193, in __init__
super(_open_file, self).__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: '/home/gridsan/groups/anaconda/dpc/lightning/__temp_weight_ddp_end.ckpt'
srun: error: d-12-4-1: task 2: Exited with exit code 1
srun: error: d-12-4-2: task 3: Exited with exit code 1
srun: error: d-12-3-1: task 0: Exited with exit code 1

# Fixed code
def fit(
    self,
    model: LightningModule,
    train_dataloader: Optional[DataLoader] = None,
    val_dataloaders: Optional[DataLoader] = None,
):
    r"""
    Runs the full optimization routine.

    Args:
        model: Model to fit.

        train_dataloader: A Pytorch
            DataLoader with training samples. If the model has
            a predefined train_dataloader method this will be skipped.

        val_dataloaders: Either a single
            Pytorch Dataloader or a list of them, specifying validation samples.
            If the model has a predefined val_dataloaders method this will be skipped

    Example::

        # Option 1,
        # Define the train_dataloader() and val_dataloader() fxs
        # in the lightningModule
        # RECOMMENDED FOR MOST RESEARCH AND APPLICATIONS TO MAINTAIN READABILITY
        trainer = Trainer()
        model = LightningModule()
        trainer.fit(model)

        # Option 2
        # in production cases we might want to pass different datasets to the same model
        # Recommended for PRODUCTION SYSTEMS
        train, val = DataLoader(...), DataLoader(...)
        trainer = Trainer()
        model = LightningModule()
        trainer.fit(model, train_dataloader=train, val_dataloader=val)

        # Option 1 & 2 can be mixed, for example the training set can be
        # defined as part of the model, and validation can then be feed to .fit()

    """
    # bind logger and other properties
    model.logger = self.logger
    self.copy_trainer_model_properties(model)

    # clean hparams
    if hasattr(model, "hparams"):
        parsing.clean_namespace(model.hparams)

    # set up the passed in dataloaders (if needed)
    self.__attach_dataloaders(model, train_dataloader, val_dataloaders)

    # check that model is configured correctly
    self.check_model_configuration(model)

    # download the data and do whatever transforms we need
    # do before any spawn calls so that the model can assign properties
    # only on proc 0 because no spawn has happened yet
    model.prepare_data()

    # Run learning rate finder:
    if self.auto_lr_find:
        self._run_lr_finder_internally(model)

    # route to appropriate start method
    # when using multi-node or DDP within a node start each module in a separate process
    if self.use_ddp2:
        task = int(os.environ["SLURM_LOCALID"])
        self.ddp_train(task, model)

    elif self.use_ddp:
        if self.is_slurm_managing_tasks:
            task = int(os.environ["SLURM_LOCALID"])
            self.ddp_train(task, model)
        else:
            self.__set_random_port()
            # track for predict
            self.model = model
            # train
            mp.spawn(self.ddp_train, nprocs=self.num_processes, args=(model,))
            # load weights if not interrupted
            if os.getenv("COLAB_GPU") or os.getenv("KAGGLE_URL_BASE"):
                self.load_spawn_weights(model)
                self.model = model

    # 1 gpu or dp option triggers training using DP module
    # easier to avoid NCCL issues
    elif self.use_dp:
        self.dp_train(model)

    elif self.use_horovod:
        self.horovod_train(model)

    elif self.single_gpu:
        self.single_gpu_train(model)

    elif self.use_tpu:  # pragma: no-cover
        log.info(f"training on {self.num_tpu_cores} TPU cores")

        #  COLAB_GPU is an env var available by default in Colab environments.
        start_method = (
            "fork"
            if os.getenv("COLAB_GPU") or os.getenv("KAGGLE_URL_BASE")
            else "spawn"
        )

        # track for predict
        self.model = model

        # train
        xmp.spawn(
            self.tpu_train,
            args=(model,),
            nprocs=self.num_tpu_cores,
            start_method=start_method,
        )

        # load weights if not interrupted
        self.load_spawn_weights(model)
        self.model = model

    # ON CPU
    else:
        # run through amp wrapper
        if self.use_amp:
            raise MisconfigurationException(
                "amp + cpu is not supported.  Please use a GPU option"
            )

        # CHOOSE OPTIMIZER
        # allow for lr schedulers as well
        self.optimizers, self.lr_schedulers, self.optimizer_frequencies = (
            self.init_optimizers(model)
        )

        self.run_pretrain_routine(model)

    # return 1 when finished
    # used for testing or when we need to know that training succeeded
    return 1