class_docstr: Assign each data sample to bounded budget batches.
  We will ensure that within the batch,
  the total number of tokens is smaller than the provided max_num_tokens,
  and the total number of sentences is smaller than the provided max_num_sentences.
  
  Samples will be sorted by length before batchify
  See Also https://github.com/pytorch/fairseq/blob/master/fairseq/data/data_utils_fast.pyx
  
  Parameters
  ----------
  lengths
      The length of the sequences in the input data sample.
      For example, can be [500, 100, 200], or [[100, 102], [200, 100], [500, 12]]
  max_num_tokens
      Max number of tokens of each batch
  max_num_sentences
      Max number of sentences of each batch
  required_batch_size_multiple
      Require batch size to be a multiple of N (default: 1).
      This will generally have better throughput in GPU.
  sort_type
      The way we sort the sample.
      - sequential:
          We sort the indices based on the individual lengths.
          For example, each sample has (src_length, tgt_length).
          We will sort them as
          indices = indices[argsort(lengths[:, 1])]
          indices = indices[argsort(lengths[:, 0])]
      - max:
          Take the max of the lengths and sort by the max value.
          indices = indices[argsort(lengths.max(axis=-1)]
      - first:
          Just sort with the first length
          indices = indices[argsort(lengths[:, 0]]
  shuffle
      Whether to shuffle the batches.
  seed
      The seed of the sampler
class_docstr_tok: ["Assign","each","data","sample","to","bounded","budget","batches",".","We","will","ensure","that","within","the","batch",",","the","total","number","of","tokens","is","smaller","than","the","provided","max_num_tokens",",","and","the","total","number","of","sentences","is","smaller","than","the","provided","max_num_sentences",".","Samples","will","be","sorted","by","length","before","batchify","See","Also","https",":","\/\/github.com\/pytorch\/fairseq\/blob\/master\/fairseq\/data\/data_utils_fast.pyx","Parameters","--","--","--","--","--","lengths","The","length","of","the","sequences","in","the","input","data","sample",".","For","example",",","can","be","[","500",",","100",",","200","]",",","or","[","[","100",",","102","]",",","[","200",",","100","]",",","[","500",",","12","]","]","max_num_tokens","Max","number","of","tokens","of","each","batch","max_num_sentences","Max","number","of","sentences","of","each","batch","required_batch_size_multiple","Require","batch","size","to","be","a","multiple","of","N","(","default",":","1",")",".","This","will","generally","have","better","throughput","in","GPU",".","sort_type","The","way","we","sort","the","sample",".","-","sequential",":","We","sort","the","indices","based","on","the","individual","lengths",".","For","example",",","each","sample","has","(","src_length",",","tgt_length",")",".","We","will","sort","them","as","indices","=","indices","[","argsort","(","lengths","[",":",",","1","]",")","]","indices","=","indices","[","argsort","(","lengths","[",":",",","0","]",")","]","-","max",":","Take","the","max","of","the","lengths","and","sort","by","the","max","value",".","indices","=","indices","[","argsort","(","lengths.max","(","axis=-1",")","]","-","first",":","Just","sort","with","the","first","length","indices","=","indices","[","argsort","(","lengths","[",":",",","0","]","]","shuffle","Whether","to","shuffle","the","batches",".","seed","The","seed","of","the","sampler"]
code: def __init__(
      self,
      lengths: Union[Sequence[int], Sequence[Sequence[int]]],
      max_num_tokens: int = -1,
      max_num_sentences: int = -1,
      required_batch_size_multiple: int = 1,
      sort_type: str = "sequential",
      shuffle: bool = True,
      seed: Optional[int] = None,
  ):
      assert (
          len(lengths) > 0
      ), "BoundedBudgetSampler does not support empty lengths."
      assert (
          max_num_tokens > 0 or max_num_sentences > 0
      ), "One of max_num_tokens and max_num_sentences must be larger than 0"
      self._lengths = np.array(lengths)
      self._max_num_tokens = max_num_tokens
      self._max_num_sentences = max_num_sentences
      self._batches = []
      self._shuffle = shuffle
      self._rng = np.random.RandomState(seed)
      self._sort_type = sort_type
      # sort
      if self._shuffle:
          self._indices = self._rng.permutation(len(lengths))
      else:
          self._indices = np.arange(len(lengths))
      if self._lengths.ndim == 2:
          if self._sort_type == "sequential":
              for i in range(self._lengths.shape[1] - 1, -1, -1):
                  self._indices = self._indices[
                      np.argsort(
                          self._lengths[self._indices, i],
                          kind="mergesort",
                      )
                  ]
          elif self._sort_type == "max":
              for i in range(self._lengths.shape[1] - 1, -1, -1):
                  self._indices = self._indices[
                      np.argsort(
                          self._lengths.max(axis=-1)[self._indices],
                          kind="mergesort",
                      )
                  ]
          else:
              raise NotImplementedError
  
      else:
          self._indices = self._indices[
              np.argsort(self._lengths[self._indices], kind="mergesort")
          ]
      if self._lengths.ndim == 2:
          self._max_lengths = self._lengths.max(axis=1)
      else:
          self._max_lengths = self._lengths
      batch = []
      # max len in a batch
      batch_max_sample_len = 0
      for index in self._indices:
          batch_max_sample_len = max(
              batch_max_sample_len, self._max_lengths[index]
          )
          # try to insert new sample to the batch
          batch_num_sentences = len(batch) + 1
          batch_num_tokens = batch_num_sentences * batch_max_sample_len
          if (
              self._max_num_sentences > 0
              and batch_num_sentences > self._max_num_sentences
          ) or (
              self._max_num_tokens > 0
              and batch_num_tokens > self._max_num_tokens
          ):
              if len(batch) < required_batch_size_multiple:
                  moded_bs = len(batch)
              else:
                  moded_bs = required_batch_size_multiple * (
                      len(batch) // required_batch_size_multiple
                  )
              self._batches.append(np.array(batch[:moded_bs]))
              batch = batch[moded_bs:]
              batch_max_sample_len = max(
                  self._max_lengths[batch].max()
                  if len(batch) > 0
                  else 0,
                  self._max_lengths[index],
              )
          batch.append(index)
      if len(batch) > 0:
          self._batches.append(np.array(batch))
  
imports: import math
  import random
  import warnings
  import numpy
  import abc
  import logging
  from typing import Union, Sequence, Optional, List
  from ..base import INT_TYPES
label_desc: Use image node_id 1 to create a new BoundedBudgetSampler object from inherited base classes: BaseSampler with example: obj = BoundedBudgetSampler(lengths, max_num_tokens, max_num_sentences, required_batch_size_multiple, sort_type, shuffle, seed)