class_docstr: 专门测试 set_dist_repro_dataloader 函数的类
class_docstr_tok: ["\u4e13\u95e8\u6d4b\u8bd5","set_dist_repro_dataloader","\u51fd\u6570\u7684\u7c7b"]
docstr: 测试单卡下 set_dist_repro_dataloader 函数的执行结果是否正确
docstr_tok: ["\u6d4b\u8bd5\u5355\u5361\u4e0b","set_dist_repro_dataloader","\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u662f\u5426\u6b63\u786e"]
code: def check_set_dist_repro_dataloader(
      self, dataloader, replaced_loader, shuffle
  ):
      """
      测试单卡下 set_dist_repro_dataloader 函数的执行结果是否正确
      """
      # 迭代两个 batch
      num_consumed_batches = 2
      already_seen_idx = set()
      if isinstance(
          replaced_loader.batch_sampler, ReproduceBatchSampler
      ):
          sampler_states = replaced_loader.batch_sampler.set_epoch(5)
      else:
          sampler_states = (
              replaced_loader.batch_sampler.sampler.set_epoch(5)
          )
      for idx, batch in enumerate(replaced_loader):
          if idx >= num_consumed_batches:
              break
          already_seen_idx.update(batch.tolist())
      if isinstance(
          replaced_loader.batch_sampler, ReproduceBatchSampler
      ):
          sampler_states = replaced_loader.batch_sampler.state_dict()
      else:
          sampler_states = (
              replaced_loader.batch_sampler.sampler.state_dict()
          )
  
      # 重新加载，应该可以输出剩下的内容，且对于 PaddleNormalDataset 来说，排序后应该是一个 range
      left_idxes = set()
      if isinstance(
          replaced_loader.batch_sampler, ReproduceBatchSampler
      ):
          batch_size = replaced_loader.batch_sampler.batch_size
          sampler_states["num_consumed_samples"] = (
              num_consumed_batches * batch_size
          )
          # 重新改造 dataloader
          new_loader = DataLoader(
              dataset=replaced_loader.dataset,
              batch_sampler=ReproduceBatchSampler(
                  BatchSampler(
                      replaced_loader.dataset,
                      shuffle=shuffle,
                      batch_size=batch_size,
                  ),
                  batch_size=batch_size,
                  drop_last=False,
              ),
          )
          new_loader.batch_sampler.load_state_dict(sampler_states)
          new_loader.batch_sampler.set_epoch(5)
      else:
          batch_size = replaced_loader.batch_sampler.batch_size
          sampler_states["num_consumed_samples"] = (
              num_consumed_batches * batch_size
          )
          # 重新构造 dataloader
          batch_sampler = BatchSampler(
              replaced_loader.dataset,
              shuffle=shuffle,
              batch_size=batch_size,
          )
          batch_sampler.sampler = RandomSampler(
              replaced_loader.dataset, shuffle=shuffle
          )
          new_loader = DataLoader(
              replaced_loader.dataset, batch_sampler=batch_sampler
          )
          new_loader.batch_sampler.sampler.load_state_dict(
              sampler_states
          )
          new_loader.batch_sampler.sampler.set_epoch(5)
      for idx, batch in enumerate(new_loader):
          left_idxes.update(batch.tolist())
  
      assert len(left_idxes) + len(already_seen_idx) == len(
          self.dataset
      )
      assert len(left_idxes | already_seen_idx) == len(self.dataset)
  
imports: import pytest
  from pathlib import Path
  from fastNLP.core.drivers.paddle_driver.single_device import PaddleSingleDriver
  from fastNLP.core.samplers import ReproduceBatchSampler, RandomSampler
  from tests.helpers.models.paddle_model import PaddleNormalModel_Classification_1
  from tests.helpers.datasets.paddle_data import PaddleNormalDataset, PaddleNormalXYDataset
  from tests.helpers.datasets.torch_data import TorchNormalDataset
  from tests.helpers.models.torch_model import TorchNormalModel_Classification_1
  from fastNLP.envs.distributed import rank_zero_rm
  from fastNLP.envs.imports import _NEED_IMPORT_PADDLE, _NEED_IMPORT_TORCH
  from fastNLP import prepare_paddle_dataloader, BucketedBatchSampler
label_desc: Use image node_id 8 for calling the TestSetDistReproDataloader obj's underlying member method code with example usage: obj.check_set_dist_repro_dataloader(dataloader, replaced_loader, shuffle) without return types