code: def smart_truncate(
      dataset: torch_utils.ListDataset,
      max_seq_length: int,
      verbose: bool = False,
  ):
      """Truncate data to the length of the longest example in the dataset.
  
      Args:
          dataset (torch_utils.ListDataset): ListDataset to truncate if possible.
          max_seq_length (int): The maximum total input sequence length.
          verbose (bool): If True, display progress bar tracking truncation progress.
  
      Returns:
          Tuple[torch_utils.ListDataset, int]: truncated dataset, and length of the longest sequence.
  
      """
      if "input_mask" not in dataset.data[0]["data_row"].get_fields():
          raise RuntimeError("Smart truncate not supported")
      valid_length_ls = []
      range_idx = np.arange(max_seq_length)
      for datum in dataset.data:
          # TODO: document why reshape and max happen here (for cola this isn't necessary).
          #       (issue #1185)
          indexer = (
              datum["data_row"]
              .input_mask.reshape(-1, max_seq_length)
              .max(-2)
          )
          valid_length_ls.append(
              range_idx[indexer.astype(bool)].max() + 1
          )
      max_valid_length = max(valid_length_ls)
  
      if max_valid_length == max_seq_length:
          return dataset, max_seq_length
  
      new_datum_ls = []
      for datum in maybe_tqdm(
          dataset.data, desc="Smart truncate data", verbose=verbose
      ):
          new_datum_ls.append(
              smart_truncate_datum(
                  datum=datum,
                  max_seq_length=max_seq_length,
                  max_valid_length=max_valid_length,
              )
          )
      new_dataset = torch_utils.ListDataset(new_datum_ls)
      return new_dataset, max_valid_length
  
file_path: jiant/jiant/proj/main/preprocessing.py
imports: import numpy
  import torch
  import jiant.shared.caching
  import jiant.utils.torch_utils
  from jiant.tasks.core import FeaturizationSpec, TaskTypes
  from jiant.utils.display import maybe_tqdm, maybe_trange
label_desc: Use image node_id 1 for calling a global function with example usage: smart_truncate(dataset, max_seq_length, verbose) and returns: new_dataset,  max_valid_length, dataset,  max_seq_length