code: def data_to_batches(
      data,
      batch_size,
      eval_mode,
      sort_during_eval,
      min_length_to_batch_separately,
  ):
      """
      Given a list of lists, where the first element of each sublist
      represents the sentence, group the sentences into batches.
  
      During training mode (not eval_mode) the sentences are sorted by
      length with a bit of random shuffling.  During eval mode, the
      sentences are sorted by length if sort_during_eval is true.
  
      Refactored from the data structure in case other models could use
      it and for ease of testing.
  
      Returns (batches, original_order), where original_order is None
      when in train mode or when unsorted and represents the original
      location of each sentence in the sort
      """
      res = []
  
      if not eval_mode:
          # sort sentences (roughly) by length for better memory utilization
          data = sorted(
              data,
              key=lambda x: len(x[0]),
              reverse=random.random() > 0.5,
          )
          data_orig_idx = None
      elif sort_during_eval:
          (data,), data_orig_idx = sort_all(
              [data], [len(x[0]) for x in data]
          )
      else:
          data_orig_idx = None
  
      current = []
      currentlen = 0
      for x in data:
          if (
              min_length_to_batch_separately is not None
              and len(x[0]) > min_length_to_batch_separately
          ):
              if currentlen > 0:
                  res.append(current)
                  current = []
                  currentlen = 0
              res.append([x])
          else:
              if len(x[0]) + currentlen > batch_size and currentlen > 0:
                  res.append(current)
                  current = []
                  currentlen = 0
              current.append(x)
              currentlen += len(x[0])
  
      if currentlen > 0:
          res.append(current)
  
      return res, data_orig_idx
  
imports: import random
  import logging
  import torch
  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, ROOT_ID, CompositeVocab, 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 1 for calling a global function with example usage: data_to_batches(data, batch_size, eval_mode, sort_during_eval, min_length_to_batch_separately) and returns: res,  data_orig_idx