code: def get_rec_iter(args, kv=None, batch_fn=None, dali_cpu=False):
      devices = [0] if dali_cpu else args.gpus
      num_devices = len(devices)
      pad_output = args.image_shape[0] == 4
  
      # the input_layout w.r.t. the model is the output_layout of the image pipeline
      output_layout = (
          types.NHWC if args.input_layout == "NHWC" else types.NCHW
      )
  
      if "horovod" in args.kv_store:
          rank = hvd.rank()
          nWrk = hvd.size()
      else:
          rank = kv.rank if kv else 0
          nWrk = kv.num_workers if kv else 1
  
      batch_size = args.batch_size // nWrk * num_devices
  
      trainpipes = [
          HybridTrainPipe(
              args=args,
              batch_size=batch_size,
              num_threads=args.dali_threads,
              device_id=dev_id,
              rec_path=args.rec_train,
              idx_path=args.rec_train_idx,
              shard_id=devices.index(dev_id) + num_devices * rank,
              num_shards=num_devices * nWrk,
              crop_shape=args.image_shape[1:],
              output_layout=output_layout,
              dtype=args.dtype,
              pad_output=pad_output,
              dali_cpu=dali_cpu,
              nvjpeg_padding=args.dali_nvjpeg_memory_padding
              * 1024
              * 1024,
              prefetch_queue=args.dali_prefetch_queue,
          )
          for dev_id in devices
      ]
      trainpipes[0].build()
      num_examples = trainpipes[0].epoch_size("Reader")
      if args.num_examples < num_examples:
          warnings.warn(
              "{} training examples will be used, although full training set contains {} examples".format(
                  args.num_examples, num_examples
              )
          )
  
      train_examples = args.num_examples // nWrk
      dali_train_iter = DALIClassificationIterator(
          trainpipes, train_examples
      )
      if not args.rec_val:
          return dali_train_iter, None, batch_fn
  
      valpipes = [
          HybridValPipe(
              args=args,
              batch_size=batch_size,
              num_threads=args.dali_validation_threads,
              device_id=dev_id,
              rec_path=args.rec_val,
              idx_path=args.rec_val_idx,
              shard_id=0
              if args.dali_separ_val
              else devices.index(dev_id) + num_devices * rank,
              num_shards=1
              if args.dali_separ_val
              else num_devices * nWrk,
              crop_shape=args.image_shape[1:],
              resize_shp=args.data_val_resize,
              output_layout=output_layout,
              dtype=args.dtype,
              pad_output=pad_output,
              dali_cpu=dali_cpu,
              nvjpeg_padding=args.dali_nvjpeg_memory_padding
              * 1024
              * 1024,
              prefetch_queue=args.dali_prefetch_queue,
          )
          for dev_id in devices
      ]
      valpipes[0].build()
      worker_val_examples = valpipes[0].epoch_size("Reader")
      if not args.dali_separ_val:
          adj = 1 if rank < worker_val_examples % nWrk else 0
          worker_val_examples = adj + worker_val_examples // nWrk
  
      dali_val_iter = DALIClassificationIterator(
          valpipes, worker_val_examples
      )
      return dali_train_iter, dali_val_iter, batch_fn
  
file_path: gluon-cv/scripts/classification/imagenet/dali.py
imports: import warnings
  from nvidia.dali.pipeline import Pipeline
  import nvidia.dali.ops
  import nvidia.dali.types
  from nvidia.dali.plugin.mxnet import DALIClassificationIterator, LastBatchPolicy
  import horovod.mxnet
label_desc: Use image node_id 4 for calling a global function with example usage: get_rec_iter(args, kv, batch_fn, dali_cpu) and returns: dali_train_iter,  dali_val_iter,  batch_fn, dali_train_iter,  None,  batch_fn