code: def test_multi(self, shuffle, drop_last, pad):
      # def test_multi(self, shuffle=True, drop_last=False, pad=False):
  
      # no shuffle
      num_replica = 2
      dataset = DatasetWithVaryLength(num_of_data=1000)
      batch_size = 5
      num_batch_per_bucket = 10
      lengths = []
      rank0_already_seen_indexes = None
      max_diff = num_batch_per_bucket * batch_size * num_replica
      for rank in range(num_replica):
          sampler = RandomBatchSampler(
              dataset,
              length=dataset.data,
              batch_size=batch_size,
              shuffle=shuffle,
              drop_last=drop_last,
          )
          sampler.set_epoch(0)
          sampler.set_distributed(num_replica, rank=rank, pad=pad)
          lengths.append(len(sampler))
          already_seen_indexes = set()
          repeat_count = 0
          for batch in sampler:
              for b in batch:
                  repeat_count += int(b in already_seen_indexes)
                  if rank0_already_seen_indexes:  # 不能交叉出现
                      assert b not in rank0_already_seen_indexes
              already_seen_indexes.update(batch)
          if rank0_already_seen_indexes is None:
              rank0_already_seen_indexes = already_seen_indexes
          if pad:  # 应该允许重复一次
              assert repeat_count <= 1
          else:
              assert repeat_count == 0
  
      assert len(set(lengths)) == 1, lengths  # 每个进程的batch数量一致
  
      # 多进程的保存
      already_seen_indexes = set()
      for rank in range(num_replica):
          sampler = RandomBatchSampler(
              dataset,
              length=dataset.data,
              batch_size=batch_size,
              shuffle=shuffle,
              drop_last=drop_last,
          )
          sampler.set_epoch(0)
          sampler.set_distributed(num_replica, rank=rank, pad=pad)
          lengths.append(len(sampler))
          count = 0
          for batch in sampler:
              already_seen_indexes.update(batch)
              if count > 5:
                  break
              count += 1
          state = sampler.state_dict()
  
      # 切换成单机
      new_batch_size = 6
      num_batch_per_bucket = 3
      new_sampler = RandomBatchSampler(
          dataset,
          length=dataset.data,
          batch_size=new_batch_size,
          shuffle=shuffle,
          drop_last=drop_last,
      )
      new_sampler.load_state_dict(state)
      repeat_count = 0
      new_already_seen_indexes = set(list(already_seen_indexes))
  
      mask = np.ones(len(dataset), dtype=bool)
      mask[list(already_seen_indexes)] = 0
      indices = np.arange(len(dataset))[mask]
  
      for batch in new_sampler:
          for b in batch:
              repeat_count += int(b in new_already_seen_indexes)
          new_already_seen_indexes.update(batch)
      if pad:  # 应该允许重复一次
          assert repeat_count <= 1
      else:
          assert repeat_count == 0
      if drop_last is False:  # 如果没有drop应该相等
          assert len(new_already_seen_indexes) == len(dataset)
  
      # 测试替换卡的数量。
      num_replica = 3
      new_sampler = RandomBatchSampler(
          dataset,
          length=dataset.data,
          batch_size=new_batch_size,
          shuffle=shuffle,
          drop_last=drop_last,
      )
      new_sampler.set_epoch(0)
      new_sampler.load_state_dict(state)
      new_sampler.set_distributed(
          num_replicas=num_replica, rank=1, pad=pad
      )
      repeat_count = 0
  
      mask = np.ones(len(dataset), dtype=bool)
      mask[list(already_seen_indexes)] = 0
      indices = np.arange(len(dataset))[mask]
  
      for batch in new_sampler:
          for b in batch:
              repeat_count += int(b in already_seen_indexes)
      if pad:  # 应该允许重复一次
          assert repeat_count <= 1
      else:
          assert repeat_count == 0
  
imports: import numpy
  import pytest
  from itertools import chain
  from copy import deepcopy
  from array import array
  from tests.helpers.datasets.normal_data import NormalSampler, NormalBatchSampler
  from fastNLP.core.samplers import ReproduceBatchSampler, BucketedBatchSampler, RandomBatchSampler
label_desc: Use image node_id 3 for calling the TestRandomBatchSampler obj's underlying member method code with example usage: obj.test_multi(shuffle, drop_last, pad) without return types