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
          train_dataset = MNIST(
              DATASETS_PATH,
              download=fabric.is_global_zero,
              train=True,
              transform=transform,
          )
          test_dataset = MNIST(
              DATASETS_PATH,
              download=fabric.is_global_zero,
              train=False,
              transform=transform,
          )
  
      train_loader = torch.utils.data.DataLoader(
          train_dataset,
          batch_size=hparams.batch_size,
      )
      test_loader = torch.utils.data.DataLoader(
          test_dataset, batch_size=hparams.batch_size
      )
  
      # don't forget to call `setup_dataloaders` to prepare for dataloaders for distributed training.
      train_loader, test_loader = fabric.setup_dataloaders(
          train_loader, test_loader
      )
  
      model = Net()  # remove call to .to(device)
      optimizer = optim.Adadelta(model.parameters(), lr=hparams.lr)
  
      # don't forget to call `setup` to prepare for model / optimizer for distributed training.
      # the model is moved automatically to the right device.
      model, optimizer = fabric.setup(model, optimizer)
  
      scheduler = StepLR(optimizer, step_size=1, gamma=hparams.gamma)
  
      # use torchmetrics instead of manually computing the accuracy
      test_acc = Accuracy(task="multiclass", num_classes=10).to(
          fabric.device
      )
  
      # EPOCH LOOP
      for epoch in range(1, hparams.epochs + 1):
          # TRAINING LOOP
          model.train()
          for batch_idx, (data, target) in enumerate(train_loader):
              # NOTE: no need to call `.to(device)` on the data, target
              optimizer.zero_grad()
              output = model(data)
              loss = F.nll_loss(output, target)
              fabric.backward(loss)  # instead of loss.backward()
  
              optimizer.step()
              if (batch_idx == 0) or (
                  (batch_idx + 1) % hparams.log_interval == 0
              ):
                  print(
                      "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format(
                          epoch,
                          batch_idx * len(data),
                          len(train_loader.dataset),
                          100.0 * batch_idx / len(train_loader),
                          loss.item(),
                      )
                  )
                  if hparams.dry_run:
                      break
  
          scheduler.step()
  
          # TESTING LOOP
          model.eval()
          test_loss = 0
          with torch.no_grad():
              for data, target in test_loader:
                  # NOTE: no need to call `.to(device)` on the data, target
                  output = model(data)
                  test_loss += F.nll_loss(
                      output, target, reduction="sum"
                  ).item()
  
                  # WITHOUT TorchMetrics
                  # pred = output.argmax(dim=1, keepdim=True)  # get the index of the max log-probability
                  # correct += pred.eq(target.view_as(pred)).sum().item()
  
                  # WITH TorchMetrics
                  test_acc(output, target)
  
                  if hparams.dry_run:
                      break
  
          # all_gather is used to aggregated the value across processes
          test_loss = fabric.all_gather(test_loss).sum() / len(
              test_loader.dataset
          )
  
          print(
              f"\nTest set: Average loss: {test_loss:.4f}, Accuracy: ({100 * test_acc.compute():.0f}%)\n"
          )
          test_acc.reset()
  
          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/image_classifier/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 torch.optim.lr_scheduler import StepLR
  from torchmetrics.classification import Accuracy
  from torchvision.datasets import MNIST
label_desc: Use image node_id 1 for calling a global function with example usage: run(hparams) without return types