code: def test_2(trainer_params: TrainerParameters, device):
      # 测试能否自动 aggregate 。
      for aggregate_when_get_metric, use_dist_sampler in product(
          [True, False], [True, False, None]
      ):
          metric = DistMetric(
              aggregate_when_get_metric=aggregate_when_get_metric
          )
  
          num_samples = len(trainer_params.evaluate_dataloaders.dataset)
  
          @Trainer.on(Event.on_sanity_check_end())
          def on_valid_end(trainer, results):
              if device is None:
                  assert results["count#c"] == num_samples
                  assert results["count2#c"] == num_samples
              else:
                  if (
                      aggregate_when_get_metric is True
                      and use_dist_sampler is True
                  ):
                      assert results["count#c"] == num_samples
                      assert results["count2#c"] == num_samples
                  elif (
                      aggregate_when_get_metric is True
                      and use_dist_sampler is False
                  ):
                      assert results["count#c"] == 2 * num_samples
                      assert results["count2#c"] == 2 * num_samples
                  elif (
                      aggregate_when_get_metric is False
                      and use_dist_sampler is True
                  ):
                      assert results["count#c"] in (49, 50)  # 不同卡，数量不同
                      assert results["count2#c"] in (49, 50)
                  else:
                      assert results["count#c"] == num_samples
                      assert results["count2#c"] == num_samples
  
          trainer = Trainer(
              model=trainer_params.model,
              driver="torch",
              device=device,
              optimizers=trainer_params.optimizers,
              train_dataloader=trainer_params.train_dataloader,
              evaluate_dataloaders=trainer_params.evaluate_dataloaders,
              metrics={"c": metric},
              evaluate_every=-1,
              n_epochs=0,
              output_from_new_proc="all",
              use_dist_sampler=use_dist_sampler,
              progress_bar="tqdm",
          )
  
          if use_dist_sampler is None:
              use_dist_sampler = device is not None
  
          trainer.run(num_eval_sanity_batch=-1)
  
          trainer = Trainer(
              model=trainer_params.model,
              driver="torch",
              device=device,
              optimizers=trainer_params.optimizers,
              train_dataloader=trainer_params.train_dataloader,
              evaluate_dataloaders=trainer_params.evaluate_dataloaders,
              metrics={
                  "c": DistMetric(
                      aggregate_when_get_metric=aggregate_when_get_metric
                  )
              },
              evaluate_every=-1,
              n_epochs=0,
              output_from_new_proc="all",
              use_dist_sampler=not (use_dist_sampler is True),  # 取相反的值
              evaluate_use_dist_sampler=use_dist_sampler,
              progress_bar="rich",  # 刚好测试一下可以替换 progress 么
          )
          trainer.run(num_eval_sanity_batch=-1)
  
      if dist.is_initialized():
          dist.destroy_process_group()
  
imports: import pytest
  from fastNLP import Metric, Evaluator
  from dataclasses import dataclass
  from typing import Any
  from itertools import product
  from fastNLP.core.controllers.trainer import Trainer
  from tests.helpers.models.torch_model import TorchNormalModel_Classification_1
  from tests.helpers.datasets.torch_data import TorchNormalDataset_Classification, TorchArgMaxDataset
  from tests.helpers.utils import magic_argv_env_context
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP import Event
label_desc: Use image node_id 3 for calling a global function with example usage: test_2(trainer_params, device) without return types