class_docstr: **fastNLP** 中 :class:`Metric` 的基类，自定义 :class:`Metric` 时，请继承该对象。使用该对象，将有助于减少在分布式状态下的 Metric 计算。
  
  .. note::
  
      在多卡情况下，所有 **fastNLP** 提供的 :class:`Metric` 默认情况下都会最终将所有设备上的评估结果集中到同一张卡上，并以此为基础输出最终的
      评测分数。如果您不需要这一功能，请将 ``aggregate_when_get_metric`` 置为 ``False`` 。
  
  .. note::
  
      如果您需要自定义自己的 :class:`Metric` ，并且有分布式训练的需求，请确保：
  
          1. 调用 :meth:`~Metric.register_element` 函数来注册需要 gather 的张量 
          2. 或在 :meth:`~Metric.get_metric` 函数中调用 :meth:`~Metric.all_gather_object` 函数来手动收集不同设备上的数据。
  
  :param backend: 目前支持五种类型的 backend, ``['torch', 'paddle', 'jittor', 'oneflow', 'auto']``。其中 ``'auto'`` 表示根据实际调用 :meth:`update`
      函数时传入的参数决定具体的 backend ，大部分情况下直接使用 ``'auto'`` 即可。
  :param aggregate_when_get_metric: 在计算 metric 的时候是否自动将各个进程上的相同的 element 的数字聚合后再得到 metric，
      当 backend 不支持分布式时，该参数无意义。如果为 ``None`` ，将在 :class:`~fastNLP.core.controllers.Evaluator` 中根据
      sampler 是否使用分布式进行自动设置。
class_docstr_tok: ["*","*","fastNLP","*","*","\u4e2d",":","class",":","`","Metric","`","\u7684\u57fa\u7c7b\uff0c\u81ea\u5b9a\u4e49",":","class",":","`","Metric","`","\u65f6\uff0c\u8bf7\u7ee7\u627f\u8be5\u5bf9\u8c61\u3002\u4f7f\u7528\u8be5\u5bf9\u8c61\uff0c\u5c06\u6709\u52a9\u4e8e\u51cf\u5c11\u5728\u5206\u5e03\u5f0f\u72b6\u6001\u4e0b\u7684","Metric","\u8ba1\u7b97\u3002","..","note",":",":","\u5728\u591a\u5361\u60c5\u51b5\u4e0b\uff0c\u6240\u6709","*","*","fastNLP","*","*","\u63d0\u4f9b\u7684",":","class",":","`","Metric","`","\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u4f1a\u6700\u7ec8\u5c06\u6240\u6709\u8bbe\u5907\u4e0a\u7684\u8bc4\u4f30\u7ed3\u679c\u96c6\u4e2d\u5230\u540c\u4e00\u5f20\u5361\u4e0a\uff0c\u5e76\u4ee5\u6b64\u4e3a\u57fa\u7840\u8f93\u51fa\u6700\u7ec8\u7684","\u8bc4\u6d4b\u5206\u6570\u3002\u5982\u679c\u60a8\u4e0d\u9700\u8981\u8fd9\u4e00\u529f\u80fd\uff0c\u8bf7\u5c06","``","aggregate_when_get_metric","``","\u7f6e\u4e3a","``","False","``","\u3002","..","note",":",":","\u5982\u679c\u60a8\u9700\u8981\u81ea\u5b9a\u4e49\u81ea\u5df1\u7684",":","class",":","`","Metric","`","\uff0c\u5e76\u4e14\u6709\u5206\u5e03\u5f0f\u8bad\u7ec3\u7684\u9700\u6c42\uff0c\u8bf7\u786e\u4fdd\uff1a","1",".","\u8c03\u7528",":","meth",":","`","~Metric.register_element","`","\u51fd\u6570\u6765\u6ce8\u518c\u9700\u8981","gather","\u7684\u5f20\u91cf","2",".","\u6216\u5728",":","meth",":","`","~Metric.get_metric","`","\u51fd\u6570\u4e2d\u8c03\u7528",":","meth",":","`","~Metric.all_gather_object","`","\u51fd\u6570\u6765\u624b\u52a8\u6536\u96c6\u4e0d\u540c\u8bbe\u5907\u4e0a\u7684\u6570\u636e\u3002",":","param","backend",":","\u76ee\u524d\u652f\u6301\u4e94\u79cd\u7c7b\u578b\u7684","backend",",","``","[","'torch","'",",","'paddle","'",",","'jittor","'",",","'oneflow","'",",","'auto","'","]","``","\u3002\u5176\u4e2d","``","'auto","'","``","\u8868\u793a\u6839\u636e\u5b9e\u9645\u8c03\u7528",":","meth",":","`","update","`","\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53c2\u6570\u51b3\u5b9a\u5177\u4f53\u7684","backend","\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528","``","'auto","'","``","\u5373\u53ef\u3002",":","param","aggregate_when_get_metric",":","\u5728\u8ba1\u7b97","metric","\u7684\u65f6\u5019\u662f\u5426\u81ea\u52a8\u5c06\u5404\u4e2a\u8fdb\u7a0b\u4e0a\u7684\u76f8\u540c\u7684","element","\u7684\u6570\u5b57\u805a\u5408\u540e\u518d\u5f97\u5230","metric\uff0c","\u5f53","backend","\u4e0d\u652f\u6301\u5206\u5e03\u5f0f\u65f6\uff0c\u8be5\u53c2\u6570\u65e0\u610f\u4e49\u3002\u5982\u679c\u4e3a","``","None","``","\uff0c\u5c06\u5728",":","class",":","`","~fastNLP.core.controllers.Evaluator","`","\u4e2d\u6839\u636e","sampler","\u662f\u5426\u4f7f\u7528\u5206\u5e03\u5f0f\u8fdb\u884c\u81ea\u52a8\u8bbe\u7f6e\u3002"]
docstr: 设置是否在 :meth:`get_metric` 的时候自动 aggregate
docstr_tok: ["\u8bbe\u7f6e\u662f\u5426\u5728",":","meth",":","`","get_metric","`","\u7684\u65f6\u5019\u81ea\u52a8","aggregate"]
code: def set_auto_aggregate_when_get_metric(self, flag: bool):
      """
      设置是否在 :meth:`get_metric` 的时候自动 aggregate
  
      """
      self.aggregate_when_get_metric = flag
  
imports: from abc import abstractmethod
  from typing import Union, List
  import functools
  from contextlib import contextmanager
  import numpy
  from fastNLP.core.metrics.backend import Backend, AutoBackend
  from fastNLP.core.metrics.element import Element
  from fastNLP.envs import is_cur_env_distributed
  from fastNLP.core.log import logger
label_desc: Use image node_id 15 for calling the Metric obj's underlying member method code with example usage: obj.set_auto_aggregate_when_get_metric(flag) without return types