code: def test_num_consumed_samples_array(
      self, shuffle, pad, num_samples, num_replicas
  ):
      # def test_num_consumed_samples_array(self, shuffle=True, pad=True, num_samples=100, num_replicas=2):
      # 测试在 sampler 多生成的时候，可以仍然可以恢复
      dataset = DatasetWithVaryLength(num_of_data=num_samples)
      samplers = []
      num_consumed_samples_array = list(
          range(0, len(dataset) + num_replicas, num_replicas)
      )
      for i in range(num_replicas):
          sampler = RandomSampler(dataset, shuffle=shuffle)
          sampler.set_epoch(0)
          sampler.set_distributed(
              num_replicas=num_replicas, rank=i, pad=pad
          )
          samplers.append(sampler)
      count = 0
      already_seen_sets = [set()]
      already_seen_set = set()
      for idxes in zip(*samplers):
          already_seen_set.update(idxes)
          already_seen_sets.append(deepcopy(already_seen_set))
          count += 1
          if count > 3:
              break
      states = samplers[0].state_dict()
      for i in range(len(already_seen_sets)):
          states["num_consumed_samples"] = num_consumed_samples_array[i]
          sampler = RandomSampler(dataset, shuffle=shuffle)
          already_seen_set = deepcopy(already_seen_sets[i])
          for batch in sampler:
              already_seen_set.add(batch)
          assert len(already_seen_set) == len(dataset)
      # 测试保存之后再次保存
      sampler = RandomSampler(dataset, shuffle=shuffle)
      sampler.set_epoch(0)
      if len(already_seen_sets) < 3:
          return
      already_seen_set = already_seen_sets[2]
      count = 0
      num_consumed_samples_array = list(range(0, num_samples))
      for idx in sampler:
          already_seen_set.add(idx)
          count += 1
          if count > 6:
              break
  
      states = sampler.state_dict()
      states["num_consumed_samples"] = num_consumed_samples_array[count]
      sampler = RandomSampler(dataset, shuffle=shuffle)
      sampler.load_state_dict(states)
      sampler.set_epoch(0)
      for idx in sampler:
          already_seen_set.add(idx)
  
      assert len(already_seen_set) == len(dataset)
  
imports: import numpy
  import pytest
  from functools import partial
  from itertools import chain
  from copy import deepcopy
  from fastNLP.core.samplers.reproducible_sampler import RandomSampler, SortedSampler, SequentialSampler
  from tests.helpers.datasets.torch_data import TorchNormalDataset
label_desc: Use image node_id 7 for calling the TestRandomSamplerYh obj's underlying member method code with example usage: obj.test_num_consumed_samples_array(shuffle, pad, num_samples, num_replicas) without return types