We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Compatibility with future PT 1.12
## 🐛 Bug

```
>       distance = x @ y.T
E       RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'
```

### To Reproduce

https://github.com/PyTorchLightning/metrics/runs/6275393755?check_suite_focus=true

#### Code sample

```py
distance = x @ y.T
```

### Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

### Environment

- OS (e.g., Linux): linux
- Python & PyTorch Version (e.g., 1.0): py3.8 & pt1.12

### Additional context

context see https://github.com/pytorch/pytorch/pull/75647
slack: https://pytorch.slack.com/archives/C3PDTEV8E/p1651742487294399

tl;dr: We don't have correct and fast linalg algorithms for half in CPU as none of the backend libraries that we use support them so it's better not to support these via half-baked implementations, and simply make the users cast their inputs if they want to use these methods

> but for GPU it shall be supported, correct? >> Yes

note as well that this just applies to Half on CPU and for linalg functios (think matmul and conv). Pointwise functions on Half on CPU will still be available, and Half on CUDA will still have full support

> it was implemented up till 1.11 but there was no real speed-up, correct?

Not only it was slower, but it was not numerically stable, so it was pretty much a bug (hence the removal without deprecation)

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `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 
22 
23 def _pairwise_cosine_similarity_update(
24     x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None
25 ) -> Tensor:
26     """Calculates the pairwise cosine similarity matrix.
27 
28     Args:
29         x: tensor of shape ``[N,d]``
30         y: tensor of shape ``[M,d]``
31         zero_diagonal: determines if the diagonal of the distance matrix should be set to zero
32     """
33     x, y, zero_diagonal = _check_input(x, y, zero_diagonal)
34 
35     norm = torch.norm(x, p=2, dim=1)
36     x /= norm.unsqueeze(1)
37     norm = torch.norm(y, p=2, dim=1)
38     y /= norm.unsqueeze(1)
39 
40     distance = x @ y.T
41     if zero_diagonal:
42         distance.fill_diagonal_(0)
43     return distance
44 
45 
46 def pairwise_cosine_similarity(
47     x: Tensor,
48     y: Optional[Tensor] = None,
49     reduction: Literal["mean", "sum", "none", None] = None,
50     zero_diagonal: Optional[bool] = None,
51 ) -> Tensor:
52     r"""Calculates pairwise cosine similarity:
53 
54     .. math::
55         s_{cos}(x,y) = \frac{<x,y>}{||x|| \cdot ||y||}
56                      = \frac{\sum_{d=1}^D x_d \cdot y_d }{\sqrt{\sum_{d=1}^D x_i^2} \cdot \sqrt{\sum_{d=1}^D x_i^2}}
57 
58     If both :math:`x` and :math:`y` are passed in, the calculation will be performed pairwise
59     between the rows of :math:`x` and :math:`y`.
60     If only :math:`x` is passed in, the calculation will be performed between the rows of :math:`x`.
61 
62     Args:
63         x: Tensor with shape ``[N, d]``
64         y: Tensor with shape ``[M, d]``, optional
65         reduction: reduction to apply along the last dimension. Choose between `'mean'`, `'sum'`
66             (applied along column dimension) or  `'none'`, `None` for no reduction
67         zero_diagonal: if the diagonal of the distance matrix should be set to 0. If only :math:`x` is given
68             this defaults to ``True`` else if :math:`y` is also given it defaults to ``False``
69 
70     Returns:
71         A ``[N,N]`` matrix of distances if only ``x`` is given, else a ``[N,M]`` matrix
72 
73     Example:
74         >>> import torch
75         >>> from torchmetrics.functional import pairwise_cosine_similarity
76         >>> x = torch.tensor([[2, 3], [3, 5], [5, 8]], dtype=torch.float32)
77         >>> y = torch.tensor([[1, 0], [2, 1]], dtype=torch.float32)
78         >>> pairwise_cosine_similarity(x, y)
79         tensor([[0.5547, 0.8682],
80                 [0.5145, 0.8437],
81                 [0.5300, 0.8533]])
82         >>> pairwise_cosine_similarity(x)
83         tensor([[0.0000, 0.9989, 0.9996],
84                 [0.9989, 0.0000, 0.9998],
85                 [0.9996, 0.9998, 0.0000]])
86 
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()

```

diff --git a/torchmetrics/functional/pairwise/cosine.py b/torchmetrics/functional/pairwise/cosine.py
--- a/torchmetrics/functional/pairwise/cosine.py
+++ b/torchmetrics/functional/pairwise/cosine.py
@@ -20,6 +20,16 @@
 from torchmetrics.functional.pairwise.helpers import _check_input, _reduce_distance_matrix
 
 
+def _safe_matmul(x: Tensor, y: Tensor) -> Tensor:
+    """Safe calculation of matrix multiplication.
+
+    If input is float16, will cast to float32 for computation and back again.
+    """
+    if x.dtype == torch.float16 or y.dtype == torch.float16:
+        return (x.float() @ y.T.float()).half()
+    return x @ y.T
+
+
 def _pairwise_cosine_similarity_update(
     x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None
 ) -> Tensor:
@@ -37,7 +47,7 @@
     norm = torch.norm(y, p=2, dim=1)
     y /= norm.unsqueeze(1)
 
-    distance = x @ y.T
+    distance = _safe_matmul(x, y)
     if zero_diagonal:
         distance.fill_diagonal_(0)
     return distance

{"golden_diff": "diff --git a/torchmetrics/functional/pairwise/cosine.py b/torchmetrics/functional/pairwise/cosine.py\n--- a/torchmetrics/functional/pairwise/cosine.py\n+++ b/torchmetrics/functional/pairwise/cosine.py\n@@ -20,6 +20,16 @@\n from torchmetrics.functional.pairwise.helpers import _check_input, _reduce_distance_matrix\n \n \n+def _safe_matmul(x: Tensor, y: Tensor) -> Tensor:\n+    \"\"\"Safe calculation of matrix multiplication.\n+\n+    If input is float16, will cast to float32 for computation and back again.\n+    \"\"\"\n+    if x.dtype == torch.float16 or y.dtype == torch.float16:\n+        return (x.float() @ y.T.float()).half()\n+    return x @ y.T\n+\n+\n def _pairwise_cosine_similarity_update(\n     x: Tensor, y: Optional[Tensor] = None, zero_diagonal: Optional[bool] = None\n ) -> Tensor:\n@@ -37,7 +47,7 @@\n     norm = torch.norm(y, p=2, dim=1)\n     y /= norm.unsqueeze(1)\n \n-    distance = x @ y.T\n+    distance = _safe_matmul(x, y)\n     if zero_diagonal:\n         distance.fill_diagonal_(0)\n     return distance\n", "issue": "Compatibility with future PT 1.12\n## \ud83d\udc1b Bug\r\n\r\n```\r\n>       distance = x @ y.T\r\nE       RuntimeError: \"addmm_impl_cpu_\" not implemented for 'Half'\r\n```\r\n\r\n### To Reproduce\r\n\r\nhttps://github.com/PyTorchLightning/metrics/runs/6275393755?check_suite_focus=true\r\n\r\n#### Code sample\r\n\r\n```py\r\ndistance = x @ y.T\r\n```\r\n\r\n### Expected behavior\r\n\r\n<!-- A clear and concise description of what you expected to happen. -->\r\n\r\n### Environment\r\n\r\n- OS (e.g., Linux): linux\r\n- Python & PyTorch Version (e.g., 1.0): py3.8 & pt1.12\r\n\r\n### Additional context\r\n\r\ncontext see https://github.com/pytorch/pytorch/pull/75647\r\nslack: https://pytorch.slack.com/archives/C3PDTEV8E/p1651742487294399\r\n\r\ntl;dr: We don't have correct and fast linalg algorithms for half in CPU as none of the backend libraries that we use support them so it's better not to support these via half-baked implementations, and simply make the users cast their inputs if they want to use these methods\r\n\r\n> but for GPU it shall be supported, correct? >> Yes\r\n\r\nnote as well that this just applies to Half on CPU and for linalg functios (think matmul and conv). Pointwise functions on Half on CPU will still be available, and Half on CUDA will still have full support\r\n\r\n> it was implemented up till 1.11 but there was no real speed-up, correct?\r\n\r\nNot only it was slower, but it was not numerically stable, so it was pretty much a bug (hence the removal without deprecation)\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\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 = x @ y.T\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 x_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    \"\"\"\n    distance = _pairwise_cosine_similarity_update(x, y, zero_diagonal)\n    return _reduce_distance_matrix(distance, reduction)\n", "path": "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\n\n\ndef _safe_matmul(x: Tensor, y: Tensor) -> Tensor:\n    \"\"\"Safe calculation of matrix multiplication.\n\n    If input is float16, will cast to float32 for computation and back again.\n    \"\"\"\n    if x.dtype == torch.float16 or y.dtype == torch.float16:\n        return (x.float() @ y.T.float()).half()\n    return x @ y.T\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 x_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    \"\"\"\n    distance = _pairwise_cosine_similarity_update(x, y, zero_diagonal)\n    return _reduce_distance_matrix(distance, reduction)\n", "path": "torchmetrics/functional/pairwise/cosine.py"}]}