class_docstr: 首先按照 ``sample`` 的长度排序，然后按照 *batch_size*num_batch_per_bucket* 为一个桶的大小，``sample`` 只会在这个桶内进行组
  合，这样每个 ``batch`` 中的 ``padding`` 数量会比较少 （因为桶内的数据的长度都接近）。
  
  :param dataset: 实现了 __len__ 方法的数据容器。
  :param length: 每条数据的长度。
  
      * 为 ``List[int]`` 时
        应当与 dataset 有一样的长度，表示 dataset 中每个元素的数量；
      * 为 ``str`` 时
        仅当传入的 ``dataset`` 是 :class:`~fastNLP.DataSet` 时，允许传入 `str` ，该 `str` 将被认为是 ``dataset`` 中的
        ``field`` 。若 field 中的元素为 ``int``，则认为该值是 sample 的长度；若不为 ``int`` ，则尝试使用 ``len`` 方法
        获取该 ``field`` 中每个元素的长度。
  
  :param batch_size: 每个 batch 的大小
  :param num_batch_per_bucket: 多少个 ``batch`` 组成一个桶，数据只会在一个桶内进行 ``shuffle`` 。
  :param shuffle: 如果为 True，将不进行 ``shuffle``，实际上数据会以从长到短的方式输出。
  :param drop_last: 如果最后一个 `batch` 的 ``sample`` 数量无法凑齐 ``batch_size`` 这么多，是否需要丢掉。
  :param seed: 设置的随机数种子
  :param kwargs: fastNLP 保留使用
class_docstr_tok: ["\u9996\u5148\u6309\u7167","``","sample","``","\u7684\u957f\u5ea6\u6392\u5e8f\uff0c\u7136\u540e\u6309\u7167","*","batch_size","*","num_batch_per_bucket","*","\u4e3a\u4e00\u4e2a\u6876\u7684\u5927\u5c0f\uff0c","``","sample","``","\u53ea\u4f1a\u5728\u8fd9\u4e2a\u6876\u5185\u8fdb\u884c\u7ec4","\u5408\uff0c\u8fd9\u6837\u6bcf\u4e2a","``","batch","``","\u4e2d\u7684","``","padding","``","\u6570\u91cf\u4f1a\u6bd4\u8f83\u5c11","\uff08\u56e0\u4e3a\u6876\u5185\u7684\u6570\u636e\u7684\u957f\u5ea6\u90fd\u63a5\u8fd1\uff09\u3002",":","param","dataset",":","\u5b9e\u73b0\u4e86","__len__","\u65b9\u6cd5\u7684\u6570\u636e\u5bb9\u5668\u3002",":","param","length",":","\u6bcf\u6761\u6570\u636e\u7684\u957f\u5ea6\u3002","*","\u4e3a","``","List","[","int","]","``","\u65f6","\u5e94\u5f53\u4e0e","dataset","\u6709\u4e00\u6837\u7684\u957f\u5ea6\uff0c\u8868\u793a","dataset","\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u6570\u91cf\uff1b","*","\u4e3a","``","str","``","\u65f6","\u4ec5\u5f53\u4f20\u5165\u7684","``","dataset","``","\u662f",":","class",":","`","~fastNLP.DataSet","`","\u65f6\uff0c\u5141\u8bb8\u4f20\u5165","`","str","`","\uff0c\u8be5","`","str","`","\u5c06\u88ab\u8ba4\u4e3a\u662f","``","dataset","``","\u4e2d\u7684","``","field","``","\u3002\u82e5","field","\u4e2d\u7684\u5143\u7d20\u4e3a","``","int","``","\uff0c\u5219\u8ba4\u4e3a\u8be5\u503c\u662f","sample","\u7684\u957f\u5ea6\uff1b\u82e5\u4e0d\u4e3a","``","int","``","\uff0c\u5219\u5c1d\u8bd5\u4f7f\u7528","``","len","``","\u65b9\u6cd5","\u83b7\u53d6\u8be5","``","field","``","\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u957f\u5ea6\u3002",":","param","batch_size",":","\u6bcf\u4e2a","batch","\u7684\u5927\u5c0f",":","param","num_batch_per_bucket",":","\u591a\u5c11\u4e2a","``","batch","``","\u7ec4\u6210\u4e00\u4e2a\u6876\uff0c\u6570\u636e\u53ea\u4f1a\u5728\u4e00\u4e2a\u6876\u5185\u8fdb\u884c","``","shuffle","``","\u3002",":","param","shuffle",":","\u5982\u679c\u4e3a","True\uff0c\u5c06\u4e0d\u8fdb\u884c","``","shuffle","``","\uff0c\u5b9e\u9645\u4e0a\u6570\u636e\u4f1a\u4ee5\u4ece\u957f\u5230\u77ed\u7684\u65b9\u5f0f\u8f93\u51fa\u3002",":","param","drop_last",":","\u5982\u679c\u6700\u540e\u4e00\u4e2a","`","batch","`","\u7684","``","sample","``","\u6570\u91cf\u65e0\u6cd5\u51d1\u9f50","``","batch_size","``","\u8fd9\u4e48\u591a\uff0c\u662f\u5426\u9700\u8981\u4e22\u6389\u3002",":","param","seed",":","\u8bbe\u7f6e\u7684\u968f\u673a\u6570\u79cd\u5b50",":","param","kwargs",":","fastNLP","\u4fdd\u7559\u4f7f\u7528"]
code: def __init__(
      self,
      dataset,
      length: Union[List[int], str],
      batch_size: int = 32,
      num_batch_per_bucket: int = 10,
      shuffle: bool = True,
      drop_last: bool = False,
      seed: int = 0,
      **kwargs,
  ):
      super().__init__()
      if isinstance(dataset, DataSet) and isinstance(length, str):
          length = dataset.get_field(length).content
          if not isinstance(length[0], int):
              length = list(map(len, length))
          self.length = np.array(length, dtype=int)
          self.sorted_indices = np.argsort(self.length)[
              ::-1
          ]  # 按长度从高到低排序的
      else:
          try:
              self.length = np.array(length, dtype=int)
              self.sorted_indices = np.argsort(length)[::-1]
          except BaseException as e:
              logger.error(
                  f"Cannot use {self.__class__.__name__} as length, since it is not sortable."
              )
  
      assert len(length) == len(dataset), (
          f"The length of `dataset`({len(dataset)}) and "
          f"`length`({len(length)}) should be equal."
      )
      assert len(self.sorted_indices) == len(
          dataset
      ), "The indices and dataset should have equal length."
  
      self.dataset = dataset
  
      self.batch_size = batch_size
      self.num_batch_per_bucket = num_batch_per_bucket
      self.shuffle = shuffle
      self.drop_last = drop_last
      self.seed = int(seed)
  
      self.num_consumed_samples = kwargs.get(
          "num_consumed_samples", 0
      )  # 总共迭代了多少数据了，包括多卡情况下的其它卡上的输出的数量
  
      # 多卡的相关的参数
      self.num_replicas = kwargs.get("num_replicas", 1)
      self.rank = kwargs.get("rank", 0)
      self.epoch = kwargs.get("epoch", -1)
      self.pad = kwargs.get("pad", False)  # 该参数在单卡上不具有任何意义；
  
      # 是否处于iteration之间，为True不允许调用 set_distributed()和load_state_dict()
      self.during_iter = kwargs.get("during_iter", False)
  
      # 以下变量为内部使用恢复状态的变量。
      self.old_batch_size = kwargs.get(
          "old_batch_size", self.batch_size
      )
      self.old_num_batch_per_bucket = kwargs.get(
          "old_num_batch_per_bucket", self.num_batch_per_bucket
      )
  
imports: import os
  import math
  from copy import deepcopy
  from typing import Dict, Union, List
  from itertools import chain
  import numpy
  from fastNLP.core.dataset import DataSet
  from fastNLP.core.log import logger
  from .utils import create_array
  from abc import abstractmethod
label_desc: Use image node_id 1 to create a new BucketedBatchSampler object from inherited base classes: ReproducibleBatchSampler with example: obj = BucketedBatchSampler(dataset, length, batch_size, num_batch_per_bucket, shuffle, drop_last, seed)