code: def batch_by_size(
      indices,
      num_tokens_fn,
      max_tokens=None,
      max_sentences=None,
      required_batch_size_multiple=1,
      distributed=False,
  ):
      """
      Yield mini-batches of indices bucketed by size. Batches may contain
      sequences of different lengths.
  
      Args:
          indices (List[int]): ordered list of dataset indices
          num_tokens_fn (callable): function that returns the number of tokens at
              a given index
          max_tokens (int, optional): max number of tokens in each batch
              (default: None).
          max_sentences (int, optional): max number of sentences in each
              batch (default: None).
          required_batch_size_multiple (int, optional): require batch size to
              be a multiple of N (default: 1).
      """
      max_tokens = max_tokens if max_tokens is not None else sys.maxsize
      max_sentences = (
          max_sentences if max_sentences is not None else sys.maxsize
      )
      bsz_mult = required_batch_size_multiple
  
      if isinstance(indices, types.GeneratorType):
          indices = np.fromiter(indices, dtype=np.int64, count=-1)
  
      sample_len = 0
      sample_lens = []
      batch = []
      batches = []
      for i in range(len(indices)):
          idx = indices[i]
          num_tokens = num_tokens_fn(idx)
          sample_lens.append(num_tokens)
          sample_len = max(sample_len, num_tokens)
          assert sample_len <= max_tokens, (
              "sentence at index {} of size {} exceeds max_tokens "
              "limit of {}!".format(idx, sample_len, max_tokens)
          )
          num_tokens = (len(batch) + 1) * sample_len
  
          if _is_batch_full(
              batch, num_tokens, max_tokens, max_sentences
          ):
              mod_len = max(
                  bsz_mult * (len(batch) // bsz_mult),
                  len(batch) % bsz_mult,
              )
              batches.append(batch[:mod_len])
              batch = batch[mod_len:]
              sample_lens = sample_lens[mod_len:]
              sample_len = (
                  max(sample_lens) if len(sample_lens) > 0 else 0
              )
          batch.append(idx)
      if len(batch) > 0:
          batches.append(batch)
      return batches
  
file_path: PaddleHub/modules/audio/svs/diffsinger/utils/__init__.py
imports: import sys
  import time
  import types
  import numpy
label_desc: Use image node_id 4 for calling a global function with example usage: batch_by_size(indices, num_tokens_fn, max_tokens, max_sentences, required_batch_size_multiple, distributed) and returns: batches