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: 注册一个 element 对象，注册之后便可以通过在 Metric 中直接通过 ``self.{name}`` 进行调用，可以认为该对象即为对应 backend 的
  tensor 直接进行加减乘除计算即可。
  
  .. warning::
  
      如果想使得该 metric 可自动扩展到多卡的情况，请一定申明 ``aggregate_method`` 。
  
  :param name: 当前 element 的名字，注册后，在 Metric 中可以通过 ``self.{name}`` 访问该变量。
  :param value: 初始化的值。在调用 :meth:`Metric.reset` 方法时也将自动设置为该值
  :param aggregate_method: 如何聚合多卡上的结果，如果为单卡执行，该值无意义。如果设置为 None 则表示该 element 不进行聚合。
  :param backend: 使用的 backend 。Element 的类型会根据 ``backend`` 进行实际的初始化。例如 ``backend`` 为 ``'torch'`` 则该对象为
      :class:`torch.Tensor` ； 如果 ``'backend'`` 为 ``'paddle'`` 则该对象为 :class:`paddle.Tensor` ；如果 ``backend`` 为
      ``'jittor'`` , 则该对象为 :class:`jittor.Var` 。一般情况下直接默认为 ``'auto'`` 就行了， **fastNLP** 会根据实际调用 :meth`Metric.update`
      函数时传入的参数进行合理的初始化，例如当传入的参数中只包含 :class:`torch.Tensor` 这一种 tensor 时（可以有其它非 tensor 类型的输入）
      则认为 ``backend`` 为 ``'torch'`` ；只包含 :class:`jittor.Var` 这一种 tensor 时（可以有其它非 tensor 类型的输入）则认为 ``backend``
      为 ``'jittor'`` 。如果没有检测到任何一种 tensor ，就默认使用 :class:`float` 类型作为 element 。
  :return: 注册的 Element 对象
docstr_tok: ["\u6ce8\u518c\u4e00\u4e2a","element","\u5bf9\u8c61\uff0c\u6ce8\u518c\u4e4b\u540e\u4fbf\u53ef\u4ee5\u901a\u8fc7\u5728","Metric","\u4e2d\u76f4\u63a5\u901a\u8fc7","``","self",".","{","name","}","``","\u8fdb\u884c\u8c03\u7528\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u8be5\u5bf9\u8c61\u5373\u4e3a\u5bf9\u5e94","backend","\u7684","tensor","\u76f4\u63a5\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u8ba1\u7b97\u5373\u53ef\u3002","..","warning",":",":","\u5982\u679c\u60f3\u4f7f\u5f97\u8be5","metric","\u53ef\u81ea\u52a8\u6269\u5c55\u5230\u591a\u5361\u7684\u60c5\u51b5\uff0c\u8bf7\u4e00\u5b9a\u7533\u660e","``","aggregate_method","``","\u3002",":","param","name",":","\u5f53\u524d","element","\u7684\u540d\u5b57\uff0c\u6ce8\u518c\u540e\uff0c\u5728","Metric","\u4e2d\u53ef\u4ee5\u901a\u8fc7","``","self",".","{","name","}","``","\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002",":","param","value",":","\u521d\u59cb\u5316\u7684\u503c\u3002\u5728\u8c03\u7528",":","meth",":","`","Metric.reset","`","\u65b9\u6cd5\u65f6\u4e5f\u5c06\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u8be5\u503c",":","param","aggregate_method",":","\u5982\u4f55\u805a\u5408\u591a\u5361\u4e0a\u7684\u7ed3\u679c\uff0c\u5982\u679c\u4e3a\u5355\u5361\u6267\u884c\uff0c\u8be5\u503c\u65e0\u610f\u4e49\u3002\u5982\u679c\u8bbe\u7f6e\u4e3a","None","\u5219\u8868\u793a\u8be5","element","\u4e0d\u8fdb\u884c\u805a\u5408\u3002",":","param","backend",":","\u4f7f\u7528\u7684","backend","\u3002Element","\u7684\u7c7b\u578b\u4f1a\u6839\u636e","``","backend","``","\u8fdb\u884c\u5b9e\u9645\u7684\u521d\u59cb\u5316\u3002\u4f8b\u5982","``","backend","``","\u4e3a","``","'torch","'","``","\u5219\u8be5\u5bf9\u8c61\u4e3a",":","class",":","`","torch.Tensor","`","\uff1b","\u5982\u679c","``","'backend","'","``","\u4e3a","``","'paddle","'","``","\u5219\u8be5\u5bf9\u8c61\u4e3a",":","class",":","`","paddle.Tensor","`","\uff1b\u5982\u679c","``","backend","``","\u4e3a","``","'jittor","'","``",",","\u5219\u8be5\u5bf9\u8c61\u4e3a",":","class",":","`","jittor.Var","`","\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u76f4\u63a5\u9ed8\u8ba4\u4e3a","``","'auto","'","``","\u5c31\u884c\u4e86\uff0c","*","*","fastNLP","*","*","\u4f1a\u6839\u636e\u5b9e\u9645\u8c03\u7528",":","meth","`","Metric.update","`","\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53c2\u6570\u8fdb\u884c\u5408\u7406\u7684\u521d\u59cb\u5316\uff0c\u4f8b\u5982\u5f53\u4f20\u5165\u7684\u53c2\u6570\u4e2d\u53ea\u5305\u542b",":","class",":","`","torch.Tensor","`","\u8fd9\u4e00\u79cd","tensor","\u65f6\uff08\u53ef\u4ee5\u6709\u5176\u5b83\u975e","tensor","\u7c7b\u578b\u7684\u8f93\u5165\uff09","\u5219\u8ba4\u4e3a","``","backend","``","\u4e3a","``","'torch","'","``","\uff1b\u53ea\u5305\u542b",":","class",":","`","jittor.Var","`","\u8fd9\u4e00\u79cd","tensor","\u65f6\uff08\u53ef\u4ee5\u6709\u5176\u5b83\u975e","tensor","\u7c7b\u578b\u7684\u8f93\u5165\uff09\u5219\u8ba4\u4e3a","``","backend","``","\u4e3a","``","'jittor","'","``","\u3002\u5982\u679c\u6ca1\u6709\u68c0\u6d4b\u5230\u4efb\u4f55\u4e00\u79cd","tensor","\uff0c\u5c31\u9ed8\u8ba4\u4f7f\u7528",":","class",":","`","float","`","\u7c7b\u578b\u4f5c\u4e3a","element","\u3002",":","return",":","\u6ce8\u518c\u7684","Element","\u5bf9\u8c61"]
code: def register_element(
      self,
      name,
      value: float = 0,
      aggregate_method=None,
      backend="auto",
  ) -> Element:
      """
      注册一个 element 对象，注册之后便可以通过在 Metric 中直接通过 ``self.{name}`` 进行调用，可以认为该对象即为对应 backend 的
      tensor 直接进行加减乘除计算即可。
  
      .. warning::
  
          如果想使得该 metric 可自动扩展到多卡的情况，请一定申明 ``aggregate_method`` 。
  
      :param name: 当前 element 的名字，注册后，在 Metric 中可以通过 ``self.{name}`` 访问该变量。
      :param value: 初始化的值。在调用 :meth:`Metric.reset` 方法时也将自动设置为该值
      :param aggregate_method: 如何聚合多卡上的结果，如果为单卡执行，该值无意义。如果设置为 None 则表示该 element 不进行聚合。
      :param backend: 使用的 backend 。Element 的类型会根据 ``backend`` 进行实际的初始化。例如 ``backend`` 为 ``'torch'`` 则该对象为
          :class:`torch.Tensor` ； 如果 ``'backend'`` 为 ``'paddle'`` 则该对象为 :class:`paddle.Tensor` ；如果 ``backend`` 为
          ``'jittor'`` , 则该对象为 :class:`jittor.Var` 。一般情况下直接默认为 ``'auto'`` 就行了， **fastNLP** 会根据实际调用 :meth`Metric.update`
          函数时传入的参数进行合理的初始化，例如当传入的参数中只包含 :class:`torch.Tensor` 这一种 tensor 时（可以有其它非 tensor 类型的输入）
          则认为 ``backend`` 为 ``'torch'`` ；只包含 :class:`jittor.Var` 这一种 tensor 时（可以有其它非 tensor 类型的输入）则认为 ``backend``
          为 ``'jittor'`` 。如果没有检测到任何一种 tensor ，就默认使用 :class:`float` 类型作为 element 。
      :return: 注册的 Element 对象
      """
      if backend == "auto":
          backend = self.backend
      else:
          backend = AutoBackend(backend)
  
      assert name is not None and name not in self.elements
  
      element = Element(
          name=name,
          value=value,
          aggregate_method=aggregate_method,
          backend=backend,
      )
      self.elements[name] = element
      setattr(self, name, element)
      return element
  
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 3 for calling the Metric obj's underlying member method code with example usage: obj.register_element(name, value, aggregate_method, backend) and returns: element