code: def collate(data, tokenizer, block_size, device):
      """Collate formats the data passed to the data loader.
      In particular we tokenize the data batch after batch to avoid keeping them
      all in memory. We output the data as a namedtuple to fit the original BertAbs's
      API.
      """
      data = [
          x for x in data if not len(x[1]) == 0
      ]  # remove empty_files
      names = [name for name, _, _ in data]
      summaries = [
          " ".join(summary_list) for _, _, summary_list in data
      ]
  
      if type(tokenizer) == BertWordPieceTokenizer:
          encoded_text = [
              encode_for_summarization_new_tokenizer(
                  story, summary, tokenizer
              )
              for _, story, summary in data
          ]
      else:
          encoded_text = [
              encode_for_summarization(story, summary, tokenizer)
              for _, story, summary in data
          ]
      encoded_stories = torch.tensor(
          [
              fit_to_block_size(
                  story, block_size, tokenizer.pad_token_id
              )
              for story, _ in encoded_text
          ]
      )
      encoder_token_type_ids = compute_token_type_ids(
          encoded_stories, tokenizer.cls_token_id
      )
      encoder_mask = build_mask(encoded_stories, tokenizer.pad_token_id)
  
      batch = Batch(
          document_names=names,
          batch_size=len(encoded_stories),
          src=encoded_stories.to(device),
          segs=encoder_token_type_ids.to(device),
          mask_src=encoder_mask.to(device),
          tgt_str=summaries,
      )
  
      return batch
  
imports: import re
  import html
  import logging
  import pandas
  import os
  import random
  import torch
  from pathlib import Path
  import pickle
  import shutil
  from collections import deque, namedtuple
  from torch.utils.data import Dataset, DataLoader, SequentialSampler
  from tokenizers import BertWordPieceTokenizer
  from transformers import BertTokenizer
label_desc: Use image node_id 3 for calling a global function with example usage: collate(data, tokenizer, block_size, device) and returns: batch