class_docstr: The internal computation class template.
  A metric implementation should overwrite `update()` and `compute()`. These two
  APIs focus on the actual mathematical meaning of the metric, without detailed
  knowledge of model output and task information.
  
  Args:
      my_rank (int): the rank of this trainer.
      batch_size (int): batch size used by this trainer.
      n_tasks (int): the number tasks this communication object will have to compute.
      window_size (int): the window size for the window metric.
      compute_on_all_ranks (bool): whether to compute metrics on all ranks. This is
          necessary if the non-leader rank wants to consume the metrics results.
      should_validate_update (bool): whether to check the inputs of `update()` and
          skip the update if the inputs are invalid. Invalid inputs include the case
          where all examples have 0 weights for a batch.
      process_group (Optional[ProcessGroup]): the process group used for the
          communication. Will use the default process group if not specified.
class_docstr_tok: ["The","internal","computation","class","template",".","A","metric","implementation","should","overwrite","`","update","(",")","`","and","`","compute","(",")","`",".","These","two","APIs","focus","on","the","actual","mathematical","meaning","of","the","metric",",","without","detailed","knowledge","of","model","output","and","task","information",".","Args",":","my_rank","(","int",")",":","the","rank","of","this","trainer",".","batch_size","(","int",")",":","batch","size","used","by","this","trainer",".","n_tasks","(","int",")",":","the","number","tasks","this","communication","object","will","have","to","compute",".","window_size","(","int",")",":","the","window","size","for","the","window","metric",".","compute_on_all_ranks","(","bool",")",":","whether","to","compute","metrics","on","all","ranks",".","This","is","necessary","if","the","non-leader","rank","wants","to","consume","the","metrics","results",".","should_validate_update","(","bool",")",":","whether","to","check","the","inputs","of","`","update","(",")","`","and","skip","the","update","if","the","inputs","are","invalid",".","Invalid","inputs","include","the","case","where","all","examples","have","0","weights","for","a","batch",".","process_group","(","Optional","[","ProcessGroup","]",")",":","the","process","group","used","for","the","communication",".","Will","use","the","default","process","group","if","not","specified","."]
docstr: name (str): the name of this state. The state will be accessible
      with `self.THE_NAME_YOU_DEFINE`.
  default (DefaultValueT): the initial value of this state. The most common
      initial value is `torch.zeros(self._n_tasks, dtype=torch.float)`, but
      users need to check the math formula to decide what is the correct
      initial value for the metric. Note the `self._n_tasks` in the above
      code. As a metric may handle multiple tasks at the same time, the
      highest dimension of a state should be `self._n_tasks`.
  add_window_state (bool): when this is True, a `window_{name}` state will
      be created to record the window state information for this state.
  dist_reduce_fx (str): the reduction function when aggregating the local
      state. For example, tower_qps uses “sum” to aggregate the total
      trained examples.
  persistent (bool): set this to True if you want to save/checkpoint the
      metric and this state is required to compute the checkpointed metric.
docstr_tok: ["name","(","str",")",":","the","name","of","this","state",".","The","state","will","be","accessible","with","`","self.THE_NAME_YOU_DEFINE","`",".","default","(","DefaultValueT",")",":","the","initial","value","of","this","state",".","The","most","common","initial","value","is","`","torch.zeros","(","self._n_tasks",",","dtype=torch.float",")","`",",","but","users","need","to","check","the","math","formula","to","decide","what","is","the","correct","initial","value","for","the","metric",".","Note","the","`","self._n_tasks","`","in","the","above","code",".","As","a","metric","may","handle","multiple","tasks","at","the","same","time",",","the","highest","dimension","of","a","state","should","be","`","self._n_tasks","`",".","add_window_state","(","bool",")",":","when","this","is","True",",","a","`","window_","{","name","}","`","state","will","be","created","to","record","the","window","state","information","for","this","state",".","dist_reduce_fx","(","str",")",":","the","reduction","function","when","aggregating","the","local","state",".","For","example",",","tower_qps","uses","\u201c","sum","\u201d","to","aggregate","the","total","trained","examples",".","persistent","(","bool",")",":","set","this","to","True","if","you","want","to","save\/checkpoint","the","metric","and","this","state","is","required","to","compute","the","checkpointed","metric","."]
code: def _add_state(
      self,
      name: str,
      default: DefaultValueT,
      add_window_state: bool,
      **kwargs: Any,
  ) -> None:
      """
      name (str): the name of this state. The state will be accessible
          with `self.THE_NAME_YOU_DEFINE`.
      default (DefaultValueT): the initial value of this state. The most common
          initial value is `torch.zeros(self._n_tasks, dtype=torch.float)`, but
          users need to check the math formula to decide what is the correct
          initial value for the metric. Note the `self._n_tasks` in the above
          code. As a metric may handle multiple tasks at the same time, the
          highest dimension of a state should be `self._n_tasks`.
      add_window_state (bool): when this is True, a `window_{name}` state will
          be created to record the window state information for this state.
      dist_reduce_fx (str): the reduction function when aggregating the local
          state. For example, tower_qps uses “sum” to aggregate the total
          trained examples.
      persistent (bool): set this to True if you want to save/checkpoint the
          metric and this state is required to compute the checkpointed metric.
      """
      # pyre-fixme[6]: Expected `Union[List[typing.Any], torch.Tensor]` for 2nd
      #  param but got `DefaultValueT`.
      super().add_state(name, default, **kwargs)
      if add_window_state:
          if self._batch_window_buffers is None:
              raise RuntimeError(
                  "Users is adding a window state while window metric is disabled."
              )
          kwargs["persistent"] = False
          window_state_name = self.get_window_state_name(name)
          # Avoid pyre error
          assert isinstance(default, torch.Tensor)
          super().add_state(
              window_state_name, default.detach().clone(), **kwargs
          )
  
          self._batch_window_buffers[window_state_name] = WindowBuffer(
              max_size=self._window_size,
              max_buffer_count=MAX_BUFFER_COUNT,
          )
  
imports: import abc
  import itertools
  import math
  from collections import defaultdict, deque
  from dataclasses import dataclass
  from enum import Enum
  from typing import Any, Callable, cast, Deque, Dict, Iterator, List, Mapping, Optional, Sequence, Set, Tuple, Type, TypeVar, Union
  import torch
  import torch.distributed
  import torch.nn
  from torch.profiler import record_function
  from torchmetrics import Metric
  from torchrec.distributed.types import get_tensor_size_bytes
  from torchrec.metrics.metrics_config import RecComputeMode, RecTaskInfo
  from torchrec.metrics.metrics_namespace import compose_metric_key, MetricNameBase, MetricNamespaceBase, MetricPrefix
label_desc: Use image node_id 4 for calling the RecMetricComputation obj's underlying member method code with example usage: obj._add_state(name, default, add_window_state) without return types