docstr: 聚集结果，并根据 method 计算后，返回结果
  
  :param tensor: 需要聚合的张量
  :param method: 聚合的方法， 目前支持 ``['sum', 'mean', 'max', 'min']``:
  
      * method 为 ``'sum'`` 时， 会将多张卡上聚合结果在维度为 `0` 上 累加起来。
      * method 为 ``'mean'`` 时，会将多张卡上聚合结果在维度为 `0` 上取平均值。
      * method 为 ``'max'`` 时，会将多张卡上聚合结果在维度为 `0` 上取最大值。
      * method 为 ``'min'`` 时，会将多张卡上聚合结果在维度为 `0` 上取最小值。
docstr_tok: ["\u805a\u96c6\u7ed3\u679c\uff0c\u5e76\u6839\u636e","method","\u8ba1\u7b97\u540e\uff0c\u8fd4\u56de\u7ed3\u679c",":","param","tensor",":","\u9700\u8981\u805a\u5408\u7684\u5f20\u91cf",":","param","method",":","\u805a\u5408\u7684\u65b9\u6cd5\uff0c","\u76ee\u524d\u652f\u6301","``","[","'sum","'",",","'mean","'",",","'max","'",",","'min","'","]","``",":","*","method","\u4e3a","``","'sum","'","``","\u65f6\uff0c","\u4f1a\u5c06\u591a\u5f20\u5361\u4e0a\u805a\u5408\u7ed3\u679c\u5728\u7ef4\u5ea6\u4e3a","`","0","`","\u4e0a","\u7d2f\u52a0\u8d77\u6765\u3002","*","method","\u4e3a","``","'mean","'","``","\u65f6\uff0c\u4f1a\u5c06\u591a\u5f20\u5361\u4e0a\u805a\u5408\u7ed3\u679c\u5728\u7ef4\u5ea6\u4e3a","`","0","`","\u4e0a\u53d6\u5e73\u5747\u503c\u3002","*","method","\u4e3a","``","'max","'","``","\u65f6\uff0c\u4f1a\u5c06\u591a\u5f20\u5361\u4e0a\u805a\u5408\u7ed3\u679c\u5728\u7ef4\u5ea6\u4e3a","`","0","`","\u4e0a\u53d6\u6700\u5927\u503c\u3002","*","method","\u4e3a","``","'min","'","``","\u65f6\uff0c\u4f1a\u5c06\u591a\u5f20\u5361\u4e0a\u805a\u5408\u7ed3\u679c\u5728\u7ef4\u5ea6\u4e3a","`","0","`","\u4e0a\u53d6\u6700\u5c0f\u503c\u3002"]
code: def aggregate(self, tensor, method: str):
      """
      聚集结果，并根据 method 计算后，返回结果
  
      :param tensor: 需要聚合的张量
      :param method: 聚合的方法， 目前支持 ``['sum', 'mean', 'max', 'min']``:
  
          * method 为 ``'sum'`` 时， 会将多张卡上聚合结果在维度为 `0` 上 累加起来。
          * method 为 ``'mean'`` 时，会将多张卡上聚合结果在维度为 `0` 上取平均值。
          * method 为 ``'max'`` 时，会将多张卡上聚合结果在维度为 `0` 上取最大值。
          * method 为 ``'min'`` 时，会将多张卡上聚合结果在维度为 `0` 上取最小值。
  
      """
      if isinstance(tensor, torch.Tensor):
          if dist.is_initialized():
              if method is None:
                  raise AggregateMethodError(
                      should_have_aggregate_method=True
                  )
              tensor = self.all_gather_object(tensor)
              if isinstance(tensor[0], torch.Tensor):
                  tensor = torch.stack(tensor)
              # 第一步, aggregate结果
              if method == "sum":
                  tensor = torch.sum(tensor, dim=0)
              elif method == "mean":
                  tensor = torch.mean(tensor, dim=0)
              elif method == "max":
                  tensor, _ = torch.max(tensor, dim=0)
              elif method == "min":
                  tensor, _ = torch.min(tensor, dim=0)
              else:
                  raise AggregateMethodError(
                      should_have_aggregate_method=False
                  )
  
      return tensor
  
file_path: fastNLP/fastNLP/core/metrics/backend/torch_backend/backend.py
imports: from typing import Any, List, Optional
  import numpy
  from fastNLP.core.metrics.backend import Backend
  from fastNLP.core.metrics.utils import AggregateMethodError
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.core.drivers.torch_driver.dist_utils import fastnlp_torch_all_gather
label_desc: Use image node_id 2 for calling the TorchBackend obj's underlying member method code with example usage: obj.aggregate(tensor, method) and returns: tensor