code: def test_state_dict_2(self, pad, num_consumed_samples):
      # 测试一下从多卡切换到单卡，或者切换到不同卡数量的多卡
      num_samples = 100
      dataset = DatasetWithVaryLength(num_of_data=num_samples)
      # 测试使用 前后shuffle不一致的load操作
      # lst = [30]
      already_numbers = set()
      sampler = SortedSampler(dataset, length=dataset.data)
      sampler.set_distributed(num_replicas=2, rank=0)
      sampler.set_epoch(0)
      if num_consumed_samples > 0:
          for i, j in enumerate(sampler, start=1):
              if already_numbers:
                  assert j <= max(already_numbers)
              already_numbers.add(j)
              if i == num_consumed_samples:
                  break
      sampler = SortedSampler(dataset, length=dataset.data)
      sampler.set_epoch(0)
      sampler.set_distributed(num_replicas=2, rank=1)
      if num_consumed_samples > 0:
          for i, j in enumerate(sampler, start=1):
              already_numbers.add(j)
              if i == num_consumed_samples:
                  break
      assert len(already_numbers) == num_consumed_samples * 2
  
      states = sampler.state_dict()
  
      new_sampler = SortedSampler(dataset, length=dataset.data)
      new_sampler.load_state_dict(states)
      new_sampler.set_epoch(0)
      for i in new_sampler:
          if already_numbers:
              assert i < max(already_numbers)
          assert i not in already_numbers
  
      # 测试切换成多卡也没有问题
      other_rank_number = set()
      for rank in range(3):
          new_sampler = SortedSampler(dataset, length=dataset.data)
          new_sampler.load_state_dict(states)
          new_sampler.set_epoch(0)
          new_sampler.set_distributed(
              num_replicas=3, rank=rank, pad=pad
          )
          count = 0
          seen = 0
          seen_in_other_rank = 0
          smaller = 0
          for i in new_sampler:
              if already_numbers:
                  smaller += int(i >= max(already_numbers))
              seen_in_other_rank += int(i in other_rank_number)
              other_rank_number.add(i)
              seen += int(i in already_numbers)
              count += 1
          assert seen <= 1 if pad else seen == 0
          assert seen_in_other_rank <= 1  # 因为pad可能重复
          assert smaller <= 1 if pad else smaller == 0
  
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 4 for calling the TestSortedSampler obj's underlying member method code with example usage: obj.test_state_dict_2(pad, num_consumed_samples) without return types