code: def _test(
      local_rank: int,
      world_size: int,
      device: "torch.device",
      dataset: DataSet,
      metric_class: Type[Metric],
      metric_kwargs: Dict[str, Any],
      sklearn_metric: Callable,
      atol: float = 1e-8,
  ) -> None:
      # metric 应该是每个进程有自己的一个 instance，所以在 _test 里面实例化
      metric = metric_class(**metric_kwargs)
      # dataset 也类似（每个进程有自己的一个）
      dataset = copy.deepcopy(dataset)
      metric.to(device)
      # 把数据拆到每个 GPU 上，有点模仿 DistributedSampler 的感觉，但这里数据单位是一个 batch（即每个 i 取了一个 batch 到自己的 GPU 上）
      for i in range(local_rank, len(dataset), world_size):
          pred, tg = torch.tensor(dataset[i]["pred"]).to(
              device
          ), torch.tensor(dataset[i]["target"]).to(device)
          metric.update(pred, tg)
  
          # my_result = metric.get_metric()
          # using_predict, using_target = dataset[: i + world_size]['pred'], dataset[: i + world_size]['target']
          # sklearn_result = sklearn_metric(using_predict, using_target)
          # _assert_allclose(my_result, sklearn_result, atol=atol)
  
      my_result = metric.get_metric()
      my_result = my_result["acc"]
      using_predict, using_target = [], []
      for i in range(len(dataset)):
          using_predict.append(dataset[i]["pred"])
          using_target.append(dataset[i]["target"])
      using_target, using_predict = np.array(using_target), np.array(
          using_predict
      )
      sklearn_result = sklearn_metric(using_predict, using_target)
      _assert_allclose(my_result, sklearn_result, atol=atol)
  
file_path: fastNLP/tests/core/metrics/test_accuracy_torch.py
imports: import os
  import sys
  from typing import Dict, List, Any, Callable, Type, Union
  from functools import partial
  import copy
  import socket
  import pytest
  import numpy
  from fastNLP.core.dataset import DataSet
  from fastNLP.core.metrics.accuracy import Accuracy
  from fastNLP.core.metrics.metric import Metric
  from .utils import find_free_network_port, setup_ddp, _assert_allclose
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
label_desc: Use image node_id 1 for calling a global function with example usage: _test(local_rank, world_size, device, dataset, metric_class, metric_kwargs, sklearn_metric, atol) without return types