code: def run(hparams):
      # Create the Lightning Fabric object. The parameters like accelerator, strategy, devices etc. will be proided
      # by the command line. See all options: `lightning run model --help`
      fabric = Fabric()
  
      seed_everything(hparams.seed)  # instead of torch.manual_seed(...)
  
      transform = T.Compose(
          [T.ToTensor(), T.Normalize((0.1307,), (0.3081,))]
      )
  
      # Let rank 0 download the data first, then everyone will load MNIST
      with fabric.rank_zero_first(
          local=False
      ):  # set `local=True` if your filesystem is not shared between machines
          dataset = MNIST(
              DATASETS_PATH,
              train=True,
              download=True,
              transform=transform,
          )
  
      # Loop over different folds (shuffle = False by default so reproducible)
      folds = hparams.folds
      kfold = model_selection.KFold(n_splits=folds)
  
      # initialize n_splits models and optimizers
      models = [Net() for _ in range(kfold.n_splits)]
      optimizers = [
          optim.Adadelta(model.parameters(), lr=hparams.lr)
          for model in models
      ]
  
      # fabric setup for models and optimizers
      for i in range(kfold.n_splits):
          models[i], optimizers[i] = fabric.setup(
              models[i], optimizers[i]
          )
  
      # Accuracy using torchmetrics
      acc_metric = Accuracy(task="multiclass", num_classes=10).to(
          fabric.device
      )
  
      # loop over epochs
      for epoch in range(1, hparams.epochs + 1):
          # loop over folds
          epoch_acc = 0
          for fold, (train_ids, val_ids) in enumerate(
              kfold.split(dataset)
          ):
              print(f"Working on fold {fold}")
  
              # initialize dataloaders based on folds
              batch_size = hparams.batch_size
              train_loader = DataLoader(
                  dataset,
                  batch_size=batch_size,
                  sampler=SubsetRandomSampler(train_ids),
              )
              val_loader = DataLoader(
                  dataset,
                  batch_size=batch_size,
                  sampler=SubsetRandomSampler(val_ids),
              )
  
              # set up dataloaders to move data to the correct device
              train_loader, val_loader = fabric.setup_dataloaders(
                  train_loader, val_loader
              )
  
              # get model and optimizer for the current fold
              model, optimizer = models[fold], optimizers[fold]
  
              # train and validate
              train_dataloader(
                  model,
                  train_loader,
                  optimizer,
                  fabric,
                  epoch,
                  hparams,
                  fold,
              )
              epoch_acc += validate_dataloader(
                  model, val_loader, fabric, hparams, fold, acc_metric
              )
              acc_metric.reset()
  
          # log epoch metrics
          print(
              f"Epoch {epoch} - Average acc: {epoch_acc / kfold.n_splits}"
          )
  
          if hparams.dry_run:
              break
  
      # When using distributed training, use `fabric.save`
      # to ensure the current process is allowed to save a checkpoint
      if hparams.save_model:
          fabric.save(model.state_dict(), "mnist_cnn.pt")
  
file_path: pytorch-lightning/examples/fabric/kfold_cv/train_fabric.py
imports: import argparse
  from os import path
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.optim
  import torchvision.transforms
  from lightning.fabric import Fabric, seed_everything
  from sklearn import model_selection
  from torch.utils.data import DataLoader, SubsetRandomSampler
  from torchmetrics.classification import Accuracy
  from torchvision.datasets import MNIST
label_desc: Use image node_id 3 for calling a global function with example usage: run(hparams) without return types