code: def dump_features(
      tsv_file: Union[str, Path],
      out_dir: Union[str, Path],
      split: str,
      rank: int,
      num_rank: int,
      device: torch.device,
      feature_type: str = "mfcc",
      layer_index: Optional[int] = None,
      checkpoint_path: Optional[Path] = None,
      sample_rate: int = 16_000,
  ) -> None:
      r"""Dump the feature tensors given a ``.tsv`` file list. The feature and lengths tensors
          will be stored under ``out_dir`` directory.
      Args:
          tsv_file (str or Path): The path of the tsv file.
          out_dir (str or Path): The directory to store the feature tensors.
          split (str): The split of data. Options: [``train``, ``valid``].
          rank (int): The rank in the multi-processing.
          num_rank (int): The number of ranks for multi-processing in feature extraction.
          device (torch.device): The location to allocate for PyTorch Tensors.
              Options: [``torch.device('cpu')``, torch.device('cuda')``].
          feature_type (str, optional): The type of the desired feature. Options: [``mfcc``, ``hubert``].
              (Default: ``mfcc``)
          layer_index (int or None, optional): The index of transformer layers in
              ``torchaudio.models.HuBERTPretrainModel`` for extracting features.
              (``1`` means the first layer output). Only active when ``feature_type``
              is set to ``hubert``. (Default: ``None``)
          checkpoint_path(Path or None, optional): The checkpoint path of ``torchaudio.models.HuBERTPretrainModel``.
              Only active when ``feature_type`` is set to ``hubert``. (Default: ``None``)
          sample_rate (int, optional): The sample rate of the audio. (Default: ``16000``)
  
      Returns:
          None
      """
      if feature_type not in ["mfcc", "hubert"]:
          raise ValueError(
              f"Expected feature type to be 'mfcc' or 'hubert'. Found {feature_type}."
          )
      if feature_type == "hubert" and layer_index is None:
          assert ValueError(
              "Please set the layer_index for HuBERT feature."
          )
      features = []
      lens = []
      out_dir = Path(out_dir)
  
      feat_path, len_path = _get_feat_lens_paths(
          out_dir, split, rank, num_rank
      )
  
      if feature_type == "hubert":
          from torchaudio.models import hubert_pretrain_base
  
          model = hubert_pretrain_base()
          model.to(device)
          model = _load_state(model, checkpoint_path, device)
  
      with open(tsv_file, "r") as f:
          root = f.readline().rstrip()
          lines = [line.rstrip() for line in f]
          start, end = get_shard_range(len(lines), num_rank, rank)
          lines = lines[start:end]
          for line in lines:
              path, nsample = line.split("\t")
              path = f"{root}/{path}"
              nsample = int(nsample)
              if feature_type == "mfcc":
                  feature = extract_feature_mfcc(
                      path, device, sample_rate
                  )
              else:
                  feature = extract_feature_hubert(
                      path, device, sample_rate, model, layer_index
                  )
              features.append(feature.cpu())
              lens.append(feature.shape[0])
      features = torch.cat(features)
      lens = torch.Tensor(lens)
      torch.save(features, feat_path)
      torch.save(lens, len_path)
      _LG.info(
          f"Finished dumping features for rank {rank} of {num_rank} successfully"
      )
  
file_path: audio/examples/hubert/utils/feature_utils.py
imports: import logging
  from pathlib import Path
  from typing import Optional, Tuple, Union
  import torch
  import torchaudio
  from torch import Tensor
  from torch.nn import Module
  from .common_utils import _get_feat_lens_paths
label_desc: Use image node_id 5 for calling a global function with example usage: dump_features(tsv_file, out_dir, split, rank, num_rank, device, feature_type, layer_index, checkpoint_path, sample_rate) without return types