docstr: Function used by DataLoader to pack data
docstr_tok: ["Function","used","by","DataLoader","to","pack","data"]
code: def __collate_fn(data):
      """Function used by DataLoader to pack data"""
      (data, idx) = zip(*data)
      (words, wordchars, upos, xpos, ufeats, pretrained, text) = zip(
          *data
      )
  
      # collate_fn is given a list of length batch size
      batch_size = len(data)
  
      # sort sentences by lens for easy RNN operations
      lens = [torch.sum(x != PAD_ID) for x in words]
      (
          words,
          wordchars,
          upos,
          xpos,
          ufeats,
          pretrained,
          text,
      ), orig_idx = sort_all(
          (words, wordchars, upos, xpos, ufeats, pretrained, text), lens
      )
      lens = [
          torch.sum(x != PAD_ID) for x in words
      ]  # we need to reinterpret lengths for the RNN
  
      # combine all words into one large list, and sort for easy charRNN ops
      wordchars = [w for sent in wordchars for w in sent]
      word_lens = [len(x) for x in wordchars]
      (wordchars,), word_orig_idx = sort_all([wordchars], word_lens)
      word_lens = [
          len(x) for x in wordchars
      ]  # we need to reinterpret lengths for the RNN
  
      # We now pad everything
      words = pad_sequence(words, True, PAD_ID)
      if None not in upos:
          upos = pad_sequence(upos, True, PAD_ID)
      else:
          upos = None
      if None not in xpos:
          xpos = pad_sequence(xpos, True, PAD_ID)
      else:
          xpos = None
      if None not in ufeats:
          ufeats = pad_sequence(ufeats, True, PAD_ID)
      else:
          ufeats = None
      pretrained = pad_sequence(pretrained, True, PAD_ID)
      wordchars = get_long_tensor(wordchars, len(word_lens))
  
      # and finally create masks for the padding indices
      words_mask = torch.eq(words, PAD_ID)
      wordchars_mask = torch.eq(wordchars, PAD_ID)
  
      return DataBatch(
          words,
          words_mask,
          wordchars,
          wordchars_mask,
          upos,
          xpos,
          ufeats,
          pretrained,
          orig_idx,
          word_orig_idx,
          lens,
          word_lens,
          text,
          idx,
      )
  
imports: import random
  import logging
  import copy
  import torch
  from collections import namedtuple
  from torch.utils.data import DataLoader
  from torch.nn.utils.rnn import pad_sequence
  from stanza.models.common.bert_embedding import filter_data
  from stanza.models.common.data import map_to_ids, get_long_tensor, get_float_tensor, sort_all
  from stanza.models.common.vocab import PAD_ID, VOCAB_PREFIX, CharVocab
  from stanza.models.pos.vocab import WordVocab, XPOSVocab, FeatureVocab, MultiVocab
  from stanza.models.pos.xpos_vocab_factory import xpos_vocab_factory
  from stanza.models.common.doc import *
label_desc: Use image node_id 9 for calling the Dataset obj's underlying member method code with example usage: obj.__collate_fn(data) and returns: DataBatch