code: def get_tarred_dataset(
      config: dict,
      shuffle_n: int,
      global_rank: int,
      world_size: int,
      tokenizer: Optional["TokenizerSpec"] = None,
      augmentor: Optional["AudioAugmentor"] = None,
  ) -> Union[
      audio_to_text.TarredAudioToBPEDataset,
      audio_to_text.TarredAudioToCharDataset,
  ]:
      """
      Instantiates a Word Piece/BPE Encoding based TarredAudioToBPEDataset or a char based TarredAudioToCharDataset.
  
      Args:
          config: Config of the TarredAudioToBPEDataset or TarredAudioToCharDataset.
          shuffle_n: How many samples to look ahead and load to be shuffled.
              See WebDataset documentation for more details.
          tokenizer: An instance of a TokenizerSpec object if BPE dataset is needed.
          global_rank: Global rank of this device.
          world_size: Global world size in the training method.
              Passsing None would return a char-based dataset.
          augmentor: Optional AudioAugmentor object for augmentations on audio data.
  
      Returns:
          An instance of TarredAudioToBPEDataset or TarredAudioToCharDataset.
      """
      tarred_audio_filepaths = config["tarred_audio_filepaths"]
      manifest_filepaths = config["manifest_filepath"]
      datasets = []
      tarred_audio_filepaths = convert_to_config_list(
          tarred_audio_filepaths
      )
      manifest_filepaths = convert_to_config_list(manifest_filepaths)
  
      bucketing_weights = config.get(
          "bucketing_weights", None
      )  # For upsampling buckets
      if bucketing_weights:
          for idx, weight in enumerate(bucketing_weights):
              if not isinstance(weight, int) or weight <= 0:
                  raise ValueError(
                      f"bucket weights must be positive integers"
                  )
  
      if len(manifest_filepaths) != len(tarred_audio_filepaths):
          raise ValueError(
              f"manifest_filepaths (length={len(manifest_filepaths)}) and tarred_audio_filepaths (length={len(tarred_audio_filepaths)}) need to have the same number of buckets."
          )
  
      if "labels" not in config:
          logging.warning(
              f"dataset does not have explicitly defined labels"
          )
  
      if "max_utts" in config:
          raise ValueError(
              '"max_utts" parameter is not supported for tarred datasets'
          )
  
      for dataset_idx, (
          tarred_audio_filepath,
          manifest_filepath,
      ) in enumerate(zip(tarred_audio_filepaths, manifest_filepaths)):
          if len(tarred_audio_filepath) == 1:
              tarred_audio_filepath = tarred_audio_filepath[0]
          if len(manifest_filepath) == 1:
              manifest_filepath = manifest_filepath[0]
  
          if tokenizer is None:
              dataset = audio_to_text.TarredAudioToCharDataset(
                  audio_tar_filepaths=tarred_audio_filepath,
                  manifest_filepath=manifest_filepath,
                  labels=config.get("labels", None),
                  sample_rate=config["sample_rate"],
                  int_values=config.get("int_values", False),
                  augmentor=augmentor,
                  shuffle_n=shuffle_n,
                  max_duration=config.get("max_duration", None),
                  min_duration=config.get("min_duration", None),
                  blank_index=config.get("blank_index", -1),
                  unk_index=config.get("unk_index", -1),
                  normalize=config.get("normalize_transcripts", False),
                  trim=config.get("trim_silence", False),
                  parser=config.get("parser", "en"),
                  shard_strategy=config.get(
                      "tarred_shard_strategy", "scatter"
                  ),
                  shard_manifests=config.get("shard_manifests", False),
                  global_rank=global_rank,
                  world_size=world_size,
                  return_sample_id=config.get(
                      "return_sample_id", False
                  ),
              )
          else:
              dataset = audio_to_text.TarredAudioToBPEDataset(
                  audio_tar_filepaths=tarred_audio_filepath,
                  manifest_filepath=manifest_filepath,
                  tokenizer=tokenizer,
                  sample_rate=config["sample_rate"],
                  int_values=config.get("int_values", False),
                  augmentor=augmentor,
                  shuffle_n=shuffle_n,
                  max_duration=config.get("max_duration", None),
                  min_duration=config.get("min_duration", None),
                  trim=config.get("trim_silence", False),
                  use_start_end_token=config.get(
                      "use_start_end_token", True
                  ),
                  shard_strategy=config.get(
                      "tarred_shard_strategy", "scatter"
                  ),
                  shard_manifests=config.get("shard_manifests", False),
                  global_rank=global_rank,
                  world_size=world_size,
                  return_sample_id=config.get(
                      "return_sample_id", False
                  ),
              )
          if bucketing_weights:
              [
                  datasets.append(dataset)
                  for _ in range(bucketing_weights[dataset_idx])
              ]
          else:
              datasets.append(dataset)
  
      return get_chain_dataset(
          datasets=datasets, ds_config=config, rank=global_rank
      )
  
imports: import copy
  import json
  import random
  from math import isclose
  from typing import Any, List, Optional, Union
  import torch
  from omegaconf import DictConfig, OmegaConf, open_dict
  from omegaconf.listconfig import ListConfig
  from pytorch_lightning.callbacks import BasePredictionWriter
  from torch.utils.data import ChainDataset
  from nemo.collections.asr.data import audio_to_text, audio_to_text_dali
  from nemo.collections.asr.data.huggingface.hf_audio_to_text_dataset import get_hf_audio_to_text_bpe_dataset, get_hf_audio_to_text_char_dataset
  from nemo.collections.asr.parts.preprocessing.perturb import process_augmentations
  from nemo.collections.common.data.dataset import CodeSwitchedDataset, ConcatDataset
  from nemo.utils import logging
label_desc: Use image node_id 7 for calling a global function with example usage: get_tarred_dataset(config, shuffle_n, global_rank, world_size, tokenizer, augmentor) and returns: get_chain_dataset