prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  inplace operation in pairwise_cosine_similarity
  ## 🐛 Bug
  Hello !
  The x, y values are modified inplace in the `pairwise_cosine_similarity` function. 
  This is not documented and may cause bugs that are difficult to find. 
  Thank you.
  
  ### To Reproduce
  
  ```python
  import torch
  from torchmetrics.functional import pairwise_cosine_similarity
  x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)
  y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)
  print("Result:", pairwise_cosine_similarity(x, y))
  print("X:", x)
  print("Y:", y)
  """Out[0]
  Result: tensor([[0.5547, 0.8682],
          [0.5145, 0.8437],
          [0.5300, 0.8533]])
  X: tensor([[0.5547, 0.8321],
          [0.5145, 0.8575],
          [0.5300, 0.8480]])
  Y: tensor([[1.0000, 0.0000],
          [0.8944, 0.4472]])
  """
  ```
  
  ### Environment
  torchmetrics==0.10.0
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/torchmetrics/functional/pairwise/cosine.py`
  Content:
  ```
  1 # Copyright The PyTorch Lightning team.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #     http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 from typing import Optional
  15 
  16 import torch
  17 from torch import Tensor
  18 from typing_extensions import Literal
  19 
  20 from torchmetrics.functional.pairwise.helpers import _check_input, _reduce_distance_matrix
  21 from torchmetrics.utilities.compute import _safe_matmul
  22 
  23 
  24 def _pairwise_cosine_similarity_update(
  25     x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None
  26 ) -> Tensor:
  27     """Calculates the pairwise cosine similarity matrix.
  28 
  29     Args:
  30         x: tensor of shape ``[N,d]``
  31         y: tensor of shape ``[M,d]``
  32         zero_diagonal: determines if the diagonal of the distance matrix should be set to zero
  33     """
  34     x, y, zero_diagonal = _check_input(x, y, zero_diagonal)
  35 
  36     norm = torch.norm(x, p=2, dim=1)
  37     x /= norm.unsqueeze(1)
  38     norm = torch.norm(y, p=2, dim=1)
  39     y /= norm.unsqueeze(1)
  40 
  41     distance = _safe_matmul(x, y)
  42     if zero_diagonal:
  43         distance.fill_diagonal_(0)
  44     return distance
  45 
  46 
  47 def pairwise_cosine_similarity(
  48     x: Tensor,
  49     y: Optional[Tensor] = None,
  50     reduction: Literal["mean", "sum", "none", None] = None,
  51     zero_diagonal: Optional[bool] = None,
  52 ) -> Tensor:
  53     r"""Calculates pairwise cosine similarity:
  54 
  55     .. math::
  56         s_{cos}(x,y) = \frac{<x,y>}{||x|| \cdot ||y||}
  57                      = \frac{\sum_{d=1}^D x_d \cdot y_d }{\sqrt{\sum_{d=1}^D x_i^2} \cdot \sqrt{\sum_{d=1}^D y_i^2}}
  58 
  59     If both :math:`x` and :math:`y` are passed in, the calculation will be performed pairwise
  60     between the rows of :math:`x` and :math:`y`.
  61     If only :math:`x` is passed in, the calculation will be performed between the rows of :math:`x`.
  62 
  63     Args:
  64         x: Tensor with shape ``[N, d]``
  65         y: Tensor with shape ``[M, d]``, optional
  66         reduction: reduction to apply along the last dimension. Choose between `'mean'`, `'sum'`
  67             (applied along column dimension) or  `'none'`, `None` for no reduction
  68         zero_diagonal: if the diagonal of the distance matrix should be set to 0. If only :math:`x` is given
  69             this defaults to ``True`` else if :math:`y` is also given it defaults to ``False``
  70 
  71     Returns:
  72         A ``[N,N]`` matrix of distances if only ``x`` is given, else a ``[N,M]`` matrix
  73 
  74     Example:
  75         >>> import torch
  76         >>> from torchmetrics.functional import pairwise_cosine_similarity
  77         >>> x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)
  78         >>> y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)
  79         >>> pairwise_cosine_similarity(x, y)
  80         tensor([[0.5547, 0.8682],
  81                 [0.5145, 0.8437],
  82                 [0.5300, 0.8533]])
  83         >>> pairwise_cosine_similarity(x)
  84         tensor([[0.0000, 0.9989, 0.9996],
  85                 [0.9989, 0.0000, 0.9998],
  86                 [0.9996, 0.9998, 0.0000]])
  87     """
  88     distance = _pairwise_cosine_similarity_update(x, y, zero_diagonal)
  89     return _reduce_distance_matrix(distance, reduction)
  90
  ```
  --- 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/src/torchmetrics/functional/pairwise/cosine.py b/src/torchmetrics/functional/pairwise/cosine.py
  --- a/src/torchmetrics/functional/pairwise/cosine.py
  +++ b/src/torchmetrics/functional/pairwise/cosine.py
  @@ -34,9 +34,9 @@
       x, y, zero_diagonal = _check_input(x, y, zero_diagonal)
   
       norm = torch.norm(x, p=2, dim=1)
  -    x /= norm.unsqueeze(1)
  +    x = x / norm.unsqueeze(1)
       norm = torch.norm(y, p=2, dim=1)
  -    y /= norm.unsqueeze(1)
  +    y = y / norm.unsqueeze(1)
   
       distance = _safe_matmul(x, y)
       if zero_diagonal:
  
verification_info: {"golden_diff": "diff --git a/src/torchmetrics/functional/pairwise/cosine.py b/src/torchmetrics/functional/pairwise/cosine.py\n--- a/src/torchmetrics/functional/pairwise/cosine.py\n+++ b/src/torchmetrics/functional/pairwise/cosine.py\n@@ -34,9 +34,9 @@\n     x, y, zero_diagonal = _check_input(x, y, zero_diagonal)\n \n     norm = torch.norm(x, p=2, dim=1)\n-    x /= norm.unsqueeze(1)\n+    x = x / norm.unsqueeze(1)\n     norm = torch.norm(y, p=2, dim=1)\n-    y /= norm.unsqueeze(1)\n+    y = y / norm.unsqueeze(1)\n \n     distance = _safe_matmul(x, y)\n     if zero_diagonal:\n", "issue": "inplace operation in pairwise_cosine_similarity\n## \ud83d\udc1b Bug\r\nHello !\r\nThe x, y values are modified inplace in the `pairwise_cosine_similarity` function. \r\nThis is not documented and may cause bugs that are difficult to find. \r\nThank you.\r\n\r\n### To Reproduce\r\n\r\n```python\r\nimport torch\r\nfrom torchmetrics.functional import pairwise_cosine_similarity\r\nx = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)\r\ny = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)\r\nprint(\"Result:\", pairwise_cosine_similarity(x, y))\r\nprint(\"X:\", x)\r\nprint(\"Y:\", y)\r\n\"\"\"Out[0]\r\nResult: tensor([[0.5547, 0.8682],\r\n        [0.5145, 0.8437],\r\n        [0.5300, 0.8533]])\r\nX: tensor([[0.5547, 0.8321],\r\n        [0.5145, 0.8575],\r\n        [0.5300, 0.8480]])\r\nY: tensor([[1.0000, 0.0000],\r\n        [0.8944, 0.4472]])\r\n\"\"\"\r\n```\r\n\r\n### Environment\r\ntorchmetrics==0.10.0\r\n\r\n\n", "before_files": [{"content": "# Copyright The PyTorch Lightning team.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom typing import Optional\n\nimport torch\nfrom torch import Tensor\nfrom typing_extensions import Literal\n\nfrom torchmetrics.functional.pairwise.helpers import _check_input, _reduce_distance_matrix\nfrom torchmetrics.utilities.compute import _safe_matmul\n\n\ndef _pairwise_cosine_similarity_update(\n    x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None\n) -> Tensor:\n    \"\"\"Calculates the pairwise cosine similarity matrix.\n\n    Args:\n        x: tensor of shape ``[N,d]``\n        y: tensor of shape ``[M,d]``\n        zero_diagonal: determines if the diagonal of the distance matrix should be set to zero\n    \"\"\"\n    x, y, zero_diagonal = _check_input(x, y, zero_diagonal)\n\n    norm = torch.norm(x, p=2, dim=1)\n    x /= norm.unsqueeze(1)\n    norm = torch.norm(y, p=2, dim=1)\n    y /= norm.unsqueeze(1)\n\n    distance = _safe_matmul(x, y)\n    if zero_diagonal:\n        distance.fill_diagonal_(0)\n    return distance\n\n\ndef pairwise_cosine_similarity(\n    x: Tensor,\n    y: Optional[Tensor] = None,\n    reduction: Literal[\"mean\", \"sum\", \"none\", None] = None,\n    zero_diagonal: Optional[bool] = None,\n) -> Tensor:\n    r\"\"\"Calculates pairwise cosine similarity:\n\n    .. math::\n        s_{cos}(x,y) = \\frac{<x,y>}{||x|| \\cdot ||y||}\n                     = \\frac{\\sum_{d=1}^D x_d \\cdot y_d }{\\sqrt{\\sum_{d=1}^D x_i^2} \\cdot \\sqrt{\\sum_{d=1}^D y_i^2}}\n\n    If both :math:`x` and :math:`y` are passed in, the calculation will be performed pairwise\n    between the rows of :math:`x` and :math:`y`.\n    If only :math:`x` is passed in, the calculation will be performed between the rows of :math:`x`.\n\n    Args:\n        x: Tensor with shape ``[N, d]``\n        y: Tensor with shape ``[M, d]``, optional\n        reduction: reduction to apply along the last dimension. Choose between `'mean'`, `'sum'`\n            (applied along column dimension) or  `'none'`, `None` for no reduction\n        zero_diagonal: if the diagonal of the distance matrix should be set to 0. If only :math:`x` is given\n            this defaults to ``True`` else if :math:`y` is also given it defaults to ``False``\n\n    Returns:\n        A ``[N,N]`` matrix of distances if only ``x`` is given, else a ``[N,M]`` matrix\n\n    Example:\n        >>> import torch\n        >>> from torchmetrics.functional import pairwise_cosine_similarity\n        >>> x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)\n        >>> y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)\n        >>> pairwise_cosine_similarity(x, y)\n        tensor([[0.5547, 0.8682],\n                [0.5145, 0.8437],\n                [0.5300, 0.8533]])\n        >>> pairwise_cosine_similarity(x)\n        tensor([[0.0000, 0.9989, 0.9996],\n                [0.9989, 0.0000, 0.9998],\n                [0.9996, 0.9998, 0.0000]])\n    \"\"\"\n    distance = _pairwise_cosine_similarity_update(x, y, zero_diagonal)\n    return _reduce_distance_matrix(distance, reduction)\n", "path": "src/torchmetrics/functional/pairwise/cosine.py"}], "after_files": [{"content": "# Copyright The PyTorch Lightning team.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom typing import Optional\n\nimport torch\nfrom torch import Tensor\nfrom typing_extensions import Literal\n\nfrom torchmetrics.functional.pairwise.helpers import _check_input, _reduce_distance_matrix\nfrom torchmetrics.utilities.compute import _safe_matmul\n\n\ndef _pairwise_cosine_similarity_update(\n    x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None\n) -> Tensor:\n    \"\"\"Calculates the pairwise cosine similarity matrix.\n\n    Args:\n        x: tensor of shape ``[N,d]``\n        y: tensor of shape ``[M,d]``\n        zero_diagonal: determines if the diagonal of the distance matrix should be set to zero\n    \"\"\"\n    x, y, zero_diagonal = _check_input(x, y, zero_diagonal)\n\n    norm = torch.norm(x, p=2, dim=1)\n    x = x / norm.unsqueeze(1)\n    norm = torch.norm(y, p=2, dim=1)\n    y = y / norm.unsqueeze(1)\n\n    distance = _safe_matmul(x, y)\n    if zero_diagonal:\n        distance.fill_diagonal_(0)\n    return distance\n\n\ndef pairwise_cosine_similarity(\n    x: Tensor,\n    y: Optional[Tensor] = None,\n    reduction: Literal[\"mean\", \"sum\", \"none\", None] = None,\n    zero_diagonal: Optional[bool] = None,\n) -> Tensor:\n    r\"\"\"Calculates pairwise cosine similarity:\n\n    .. math::\n        s_{cos}(x,y) = \\frac{<x,y>}{||x|| \\cdot ||y||}\n                     = \\frac{\\sum_{d=1}^D x_d \\cdot y_d }{\\sqrt{\\sum_{d=1}^D x_i^2} \\cdot \\sqrt{\\sum_{d=1}^D y_i^2}}\n\n    If both :math:`x` and :math:`y` are passed in, the calculation will be performed pairwise\n    between the rows of :math:`x` and :math:`y`.\n    If only :math:`x` is passed in, the calculation will be performed between the rows of :math:`x`.\n\n    Args:\n        x: Tensor with shape ``[N, d]``\n        y: Tensor with shape ``[M, d]``, optional\n        reduction: reduction to apply along the last dimension. Choose between `'mean'`, `'sum'`\n            (applied along column dimension) or  `'none'`, `None` for no reduction\n        zero_diagonal: if the diagonal of the distance matrix should be set to 0. If only :math:`x` is given\n            this defaults to ``True`` else if :math:`y` is also given it defaults to ``False``\n\n    Returns:\n        A ``[N,N]`` matrix of distances if only ``x`` is given, else a ``[N,M]`` matrix\n\n    Example:\n        >>> import torch\n        >>> from torchmetrics.functional import pairwise_cosine_similarity\n        >>> x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)\n        >>> y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)\n        >>> pairwise_cosine_similarity(x, y)\n        tensor([[0.5547, 0.8682],\n                [0.5145, 0.8437],\n                [0.5300, 0.8533]])\n        >>> pairwise_cosine_similarity(x)\n        tensor([[0.0000, 0.9989, 0.9996],\n                [0.9989, 0.0000, 0.9998],\n                [0.9996, 0.9998, 0.0000]])\n    \"\"\"\n    distance = _pairwise_cosine_similarity_update(x, y, zero_diagonal)\n    return _reduce_distance_matrix(distance, reduction)\n", "path": "src/torchmetrics/functional/pairwise/cosine.py"}]}