prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Improve Frequency
  ## 🚀 Feature
  
  If we would like to log datapoints/second every 100 iterations, we most probably do like this 
  ```python
  wps_metric = Frequency(output_transformer=lambda x: x['ntokens'])
  wps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=100))
  ```
  however, seems like this wont take into account all other iterations while computing the total number of tokens.
  ```python
  class Frequency(Metric):
      ....
      def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
          engine.add_event_handler(Events.EPOCH_STARTED, self.started)
          engine.add_event_handler(event_name, self.iteration_completed)
          engine.add_event_handler(event_name, self.completed, name)
  ```
  IMO, should be 
  ```python
  class Frequency(Metric):
      ....
      def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
          engine.add_event_handler(Events.EPOCH_STARTED, self.started)
          engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)
          engine.add_event_handler(event_name, self.completed, name)
  ```
  
  cc @erip 
  
  Improve Frequency
  ## 🚀 Feature
  
  If we would like to log datapoints/second every 100 iterations, we most probably do like this 
  ```python
  wps_metric = Frequency(output_transformer=lambda x: x['ntokens'])
  wps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=100))
  ```
  however, seems like this wont take into account all other iterations while computing the total number of tokens.
  ```python
  class Frequency(Metric):
      ....
      def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
          engine.add_event_handler(Events.EPOCH_STARTED, self.started)
          engine.add_event_handler(event_name, self.iteration_completed)
          engine.add_event_handler(event_name, self.completed, name)
  ```
  IMO, should be 
  ```python
  class Frequency(Metric):
      ....
      def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
          engine.add_event_handler(Events.EPOCH_STARTED, self.started)
          engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)
          engine.add_event_handler(event_name, self.completed, name)
  ```
  
  cc @erip 
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `ignite/metrics/frequency.py`
  Content:
  ```
  1 import torch
  2 import torch.distributed as dist
  3 
  4 from ignite.engine import Events
  5 from ignite.metrics import Metric
  6 from ignite.handlers.timing import Timer
  7 from ignite.metrics.metric import sync_all_reduce, reinit__is_reduced
  8 
  9 
  10 class Frequency(Metric):
  11     """Provides metrics for the number of examples processed per second.
  12 
  13     Examples:
  14 
  15         .. code-block:: python
  16 
  17             # Compute number of tokens processed
  18             wps_metric = Frequency(output_transform=lambda x: x['ntokens'])
  19             wps_metric.attach(trainer, name='wps')
  20             # Logging with TQDM
  21             ProgressBar(persist=True).attach(trainer, metric_names=['wps'])
  22             # Progress bar will looks like
  23             # Epoch [2/10]: [12/24]  50%|█████      , wps=400 [00:17<1:23]
  24     """
  25 
  26     def __init__(self, output_transform=lambda x: x, device=None):
  27         self._timer = None
  28         self._acc = None
  29         self._n = None
  30         self._elapsed = None
  31         super(Frequency, self).__init__(output_transform=output_transform, device=device)
  32 
  33     @reinit__is_reduced
  34     def reset(self):
  35         self._timer = Timer()
  36         self._acc = 0
  37         self._n = 0
  38         self._elapsed = 0.0
  39         super(Frequency, self).reset()
  40 
  41     @reinit__is_reduced
  42     def update(self, output):
  43         self._acc += output
  44         self._n = self._acc
  45         self._elapsed = torch.tensor(self._timer.value(), device=self._device)
  46 
  47     @sync_all_reduce("_n", "_elapsed")
  48     def compute(self):
  49         time_divisor = 1.0
  50 
  51         if dist.is_available() and dist.is_initialized():
  52             time_divisor *= dist.get_world_size()
  53 
  54         # Returns the average processed objects per second across all workers
  55         return self._n / self._elapsed.item() * time_divisor
  56 
  57     def completed(self, engine, name):
  58         engine.state.metrics[name] = int(self.compute())
  59 
  60     def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
  61         engine.add_event_handler(Events.EPOCH_STARTED, self.started)
  62         engine.add_event_handler(event_name, self.iteration_completed)
  63         engine.add_event_handler(event_name, self.completed, name)
  64
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/ignite/metrics/frequency.py b/ignite/metrics/frequency.py
  --- a/ignite/metrics/frequency.py
  +++ b/ignite/metrics/frequency.py
  @@ -19,8 +19,21 @@
               wps_metric.attach(trainer, name='wps')
               # Logging with TQDM
               ProgressBar(persist=True).attach(trainer, metric_names=['wps'])
  -            # Progress bar will looks like
  +            # Progress bar will look like
               # Epoch [2/10]: [12/24]  50%|█████      , wps=400 [00:17<1:23]
  +
  +
  +        To compute examples processed per second every 50th iteration:
  +
  +        .. code-block:: python
  +
  +            # Compute number of tokens processed
  +            wps_metric = Frequency(output_transform=lambda x: x['ntokens'])
  +            wps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=50))
  +            # Logging with TQDM
  +            ProgressBar(persist=True).attach(trainer, metric_names=['wps'])
  +            # Progress bar will look like
  +            # Epoch [2/10]: [50/100]  50%|█████      , wps=400 [00:17<00:35]
       """
   
       def __init__(self, output_transform=lambda x: x, device=None):
  @@ -59,5 +72,5 @@
   
       def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):
           engine.add_event_handler(Events.EPOCH_STARTED, self.started)
  -        engine.add_event_handler(event_name, self.iteration_completed)
  +        engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)
           engine.add_event_handler(event_name, self.completed, name)
  
verification_info: {"golden_diff": "diff --git a/ignite/metrics/frequency.py b/ignite/metrics/frequency.py\n--- a/ignite/metrics/frequency.py\n+++ b/ignite/metrics/frequency.py\n@@ -19,8 +19,21 @@\n             wps_metric.attach(trainer, name='wps')\n             # Logging with TQDM\n             ProgressBar(persist=True).attach(trainer, metric_names=['wps'])\n-            # Progress bar will looks like\n+            # Progress bar will look like\n             # Epoch [2/10]: [12/24]  50%|\u2588\u2588\u2588\u2588\u2588      , wps=400 [00:17<1:23]\n+\n+\n+        To compute examples processed per second every 50th iteration:\n+\n+        .. code-block:: python\n+\n+            # Compute number of tokens processed\n+            wps_metric = Frequency(output_transform=lambda x: x['ntokens'])\n+            wps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=50))\n+            # Logging with TQDM\n+            ProgressBar(persist=True).attach(trainer, metric_names=['wps'])\n+            # Progress bar will look like\n+            # Epoch [2/10]: [50/100]  50%|\u2588\u2588\u2588\u2588\u2588      , wps=400 [00:17<00:35]\n     \"\"\"\n \n     def __init__(self, output_transform=lambda x: x, device=None):\n@@ -59,5 +72,5 @@\n \n     def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\n         engine.add_event_handler(Events.EPOCH_STARTED, self.started)\n-        engine.add_event_handler(event_name, self.iteration_completed)\n+        engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)\n         engine.add_event_handler(event_name, self.completed, name)\n", "issue": "Improve Frequency\n## \ud83d\ude80 Feature\r\n\r\nIf we would like to log datapoints/second every 100 iterations, we most probably do like this \r\n```python\r\nwps_metric = Frequency(output_transformer=lambda x: x['ntokens'])\r\nwps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=100))\r\n```\r\nhowever, seems like this wont take into account all other iterations while computing the total number of tokens.\r\n```python\r\nclass Frequency(Metric):\r\n    ....\r\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\r\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\r\n        engine.add_event_handler(event_name, self.iteration_completed)\r\n        engine.add_event_handler(event_name, self.completed, name)\r\n```\r\nIMO, should be \r\n```python\r\nclass Frequency(Metric):\r\n    ....\r\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\r\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\r\n        engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)\r\n        engine.add_event_handler(event_name, self.completed, name)\r\n```\r\n\r\ncc @erip \r\n\nImprove Frequency\n## \ud83d\ude80 Feature\r\n\r\nIf we would like to log datapoints/second every 100 iterations, we most probably do like this \r\n```python\r\nwps_metric = Frequency(output_transformer=lambda x: x['ntokens'])\r\nwps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=100))\r\n```\r\nhowever, seems like this wont take into account all other iterations while computing the total number of tokens.\r\n```python\r\nclass Frequency(Metric):\r\n    ....\r\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\r\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\r\n        engine.add_event_handler(event_name, self.iteration_completed)\r\n        engine.add_event_handler(event_name, self.completed, name)\r\n```\r\nIMO, should be \r\n```python\r\nclass Frequency(Metric):\r\n    ....\r\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\r\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\r\n        engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)\r\n        engine.add_event_handler(event_name, self.completed, name)\r\n```\r\n\r\ncc @erip \r\n\n", "before_files": [{"content": "import torch\nimport torch.distributed as dist\n\nfrom ignite.engine import Events\nfrom ignite.metrics import Metric\nfrom ignite.handlers.timing import Timer\nfrom ignite.metrics.metric import sync_all_reduce, reinit__is_reduced\n\n\nclass Frequency(Metric):\n    \"\"\"Provides metrics for the number of examples processed per second.\n\n    Examples:\n\n        .. code-block:: python\n\n            # Compute number of tokens processed\n            wps_metric = Frequency(output_transform=lambda x: x['ntokens'])\n            wps_metric.attach(trainer, name='wps')\n            # Logging with TQDM\n            ProgressBar(persist=True).attach(trainer, metric_names=['wps'])\n            # Progress bar will looks like\n            # Epoch [2/10]: [12/24]  50%|\u2588\u2588\u2588\u2588\u2588      , wps=400 [00:17<1:23]\n    \"\"\"\n\n    def __init__(self, output_transform=lambda x: x, device=None):\n        self._timer = None\n        self._acc = None\n        self._n = None\n        self._elapsed = None\n        super(Frequency, self).__init__(output_transform=output_transform, device=device)\n\n    @reinit__is_reduced\n    def reset(self):\n        self._timer = Timer()\n        self._acc = 0\n        self._n = 0\n        self._elapsed = 0.0\n        super(Frequency, self).reset()\n\n    @reinit__is_reduced\n    def update(self, output):\n        self._acc += output\n        self._n = self._acc\n        self._elapsed = torch.tensor(self._timer.value(), device=self._device)\n\n    @sync_all_reduce(\"_n\", \"_elapsed\")\n    def compute(self):\n        time_divisor = 1.0\n\n        if dist.is_available() and dist.is_initialized():\n            time_divisor *= dist.get_world_size()\n\n        # Returns the average processed objects per second across all workers\n        return self._n / self._elapsed.item() * time_divisor\n\n    def completed(self, engine, name):\n        engine.state.metrics[name] = int(self.compute())\n\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\n        engine.add_event_handler(event_name, self.iteration_completed)\n        engine.add_event_handler(event_name, self.completed, name)\n", "path": "ignite/metrics/frequency.py"}], "after_files": [{"content": "import torch\nimport torch.distributed as dist\n\nfrom ignite.engine import Events\nfrom ignite.metrics import Metric\nfrom ignite.handlers.timing import Timer\nfrom ignite.metrics.metric import sync_all_reduce, reinit__is_reduced\n\n\nclass Frequency(Metric):\n    \"\"\"Provides metrics for the number of examples processed per second.\n\n    Examples:\n\n        .. code-block:: python\n\n            # Compute number of tokens processed\n            wps_metric = Frequency(output_transform=lambda x: x['ntokens'])\n            wps_metric.attach(trainer, name='wps')\n            # Logging with TQDM\n            ProgressBar(persist=True).attach(trainer, metric_names=['wps'])\n            # Progress bar will look like\n            # Epoch [2/10]: [12/24]  50%|\u2588\u2588\u2588\u2588\u2588      , wps=400 [00:17<1:23]\n\n\n        To compute examples processed per second every 50th iteration:\n\n        .. code-block:: python\n\n            # Compute number of tokens processed\n            wps_metric = Frequency(output_transform=lambda x: x['ntokens'])\n            wps_metric.attach(trainer, name='wps', event_name=Events.ITERATION_COMPLETED(every=50))\n            # Logging with TQDM\n            ProgressBar(persist=True).attach(trainer, metric_names=['wps'])\n            # Progress bar will look like\n            # Epoch [2/10]: [50/100]  50%|\u2588\u2588\u2588\u2588\u2588      , wps=400 [00:17<00:35]\n    \"\"\"\n\n    def __init__(self, output_transform=lambda x: x, device=None):\n        self._timer = None\n        self._acc = None\n        self._n = None\n        self._elapsed = None\n        super(Frequency, self).__init__(output_transform=output_transform, device=device)\n\n    @reinit__is_reduced\n    def reset(self):\n        self._timer = Timer()\n        self._acc = 0\n        self._n = 0\n        self._elapsed = 0.0\n        super(Frequency, self).reset()\n\n    @reinit__is_reduced\n    def update(self, output):\n        self._acc += output\n        self._n = self._acc\n        self._elapsed = torch.tensor(self._timer.value(), device=self._device)\n\n    @sync_all_reduce(\"_n\", \"_elapsed\")\n    def compute(self):\n        time_divisor = 1.0\n\n        if dist.is_available() and dist.is_initialized():\n            time_divisor *= dist.get_world_size()\n\n        # Returns the average processed objects per second across all workers\n        return self._n / self._elapsed.item() * time_divisor\n\n    def completed(self, engine, name):\n        engine.state.metrics[name] = int(self.compute())\n\n    def attach(self, engine, name, event_name=Events.ITERATION_COMPLETED):\n        engine.add_event_handler(Events.EPOCH_STARTED, self.started)\n        engine.add_event_handler(Events.ITERATION_COMPLETED, self.iteration_completed)\n        engine.add_event_handler(event_name, self.completed, name)\n", "path": "ignite/metrics/frequency.py"}]}