code: def build_dataloader_and_sampler(
      dataset_instance: torch.utils.data.Dataset,
      datamodule_config: DictConfig,
  ) -> Tuple[
      torch.utils.data.DataLoader, Optional[torch.utils.data.Sampler]
  ]:
      """Builds and returns a dataloader along with its sample
  
      Args:
          dataset_instance (torch.utils.data.Dataset): Instance of dataset for which
              dataloader has to be created
          datamodule_config (omegaconf.DictConfig): Datamodule configuration; required
              for infering params for dataloader
  
      Returns:
          Tuple[torch.utils.data.DataLoader, Optional[torch.utils.data.Sampler]]:
              Tuple of Dataloader and Sampler instance
      """
      from mmf.common.batch_collator import BatchCollator
  
      training_config = get_global_config("training")
      # Support params coming in from dataloader params
      other_args = {
          "num_workers": datamodule_config.get(
              "num_workers", training_config.get("num_workers", 4)
          ),
          "pin_memory": datamodule_config.get(
              "pin_memory", training_config.get("pin_memory", False)
          ),
          "shuffle": datamodule_config.get("shuffle", None),
          "batch_size": datamodule_config.get("batch_size", None),
      }
      if version.parse(torch.__version__) >= version.parse("1.8"):
          # only use persistent workers in PyTorch 1.8 or higher
          # (PyTorch 1.7 also has this option but doesn't support it correctly due to
          # https://github.com/pytorch/pytorch/issues/48370)
          other_args["persistent_workers"] = (
              datamodule_config.get(
                  "persistent_workers",
                  training_config.get("persistent_workers", True),
              ),
          )
          if (
              other_args["persistent_workers"]
              and other_args["num_workers"] == 0
          ):
              logger.warning(
                  "persistent_workers cannot be used together with num_workers == 0; "
                  "setting persistent_workers to False"
              )
              other_args["persistent_workers"] = False
  
      # IterableDataset returns batches directly, so no need to add Sampler
      # or batch size as user is expected to control those. This is a fine
      # assumption for now to not support single item based IterableDataset
      # as it will add unnecessary complexity and config parameters
      # to the codebase
      if not isinstance(
          dataset_instance, torch.utils.data.IterableDataset
      ):
          other_args = _add_extra_args_for_dataloader(
              dataset_instance, other_args
          )
      else:
          other_args.pop("shuffle")
  
      # Set drop_last=True when using XLA to have constant batch size.
      # In this case we also need to set drop_last=True in DistributedSampler.
      loader = torch.utils.data.DataLoader(
          dataset=dataset_instance,
          collate_fn=BatchCollator(
              dataset_instance.dataset_name,
              dataset_instance.dataset_type,
          ),
          drop_last=is_xla(),  # see also MultiDatasetLoader.__len__
          **other_args,
      )
  
      if is_xla():
          device = xm.xla_device()
          loader = xla_pl.MpDeviceLoader(loader, device)
  
      if other_args["num_workers"] >= 0:
          # Suppress leaking semaphore warning
          os.environ[
              "PYTHONWARNINGS"
          ] = "ignore:semaphore_tracker:UserWarning"
  
      loader.dataset_type = dataset_instance.dataset_type
  
      return loader, other_args.get("sampler", None)
  
imports: import logging
  import os
  import warnings
  from enum import Enum
  from typing import Any, Dict, List, Optional, Tuple, Union
  import mmf
  import pytorch_lightning
  import torch
  from mmf.common.meter import Meter
  from mmf.common.registry import registry
  from mmf.datasets.iteration_strategies import ConstantIterationStrategy, IterationStrategy, SizeProportionalIterationStrategy
  from mmf.datasets.processors.processors import Processor
  from mmf.utils.configuration import Configuration, get_global_config
  from mmf.utils.distributed import is_dist_initialized, is_main, is_xla, synchronize
  from mmf.utils.general import get_optimizer_parameters
  from omegaconf import DictConfig, OmegaConf
  from packaging import version
label_desc: Use image node_id 9 for calling a global function with example usage: build_dataloader_and_sampler(dataset_instance, datamodule_config) and returns: loader,  other_args