We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
PearsonCorrCoeff returns nan when input is of type torch.float16 or torch.bfloat16.
## 🐛 Bug

PearsonCorrCoeff returns nan when input is of type torch.float16 or torch.bfloat16 and all values are close.

### To Reproduce

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

<details>
  <summary>

```python
import torch
import torchmetrics as tm
pcc = tm.regression.PearsonCorrCoef().to("cuda")
pred = torch.tensor([0.4746, 0.4805, 0.4766, 0.4805, 0.4766, 0.4805, 0.4785, 0.4824, 0.4805],dtype=torch.float16).to("cuda")
target = torch.tensor([0.0336, 0.3676, 0.6302, 0.7192, 0.2295, 0.2886, 0.6302, 0.7096, 0.0208],dtype=torch.float16).to("cuda")
print(pcc(pred,target))
print(pcc(pred.to(torch.float32),target.to(torch.float32)))
tensor(nan, device='cuda:0')
tensor(0.3720, device='cuda:0')
```

</summary>

<!-- Ideally attach a minimal code sample to reproduce the decried issue.
Minimal means having the shortest code but still preserving the bug. -->

</details>

### Expected behavior

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

### Environment

Python version: 3.10.9
Torch version: 1.12.1
TorchMetrics version: 0.11.1
GPU device name: Tesla T4
CUDA Version: 11.4 

### Additional context

When running in a training loop I found that some fraction (~30%) of steps would not produce a nan number when using torch.float16 or bfloat16, while the other ~70% would.
This seems to occur because the values in pred above are not very different (changing one value of pred above to be more different than the rest will compute a correct PCC), however I think that this should still be able to be computed with half precision and the standard deviation of pred shown above.
<!-- Add any other context about the problem here. -->


--- 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/regression/pearson.py`
Content:
```
1 # Copyright The 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 Tuple
15 
16 import torch
17 from torch import Tensor
18 
19 from torchmetrics.functional.regression.utils import _check_data_shape_to_num_outputs
20 from torchmetrics.utilities.checks import _check_same_shape
21 
22 
23 def _pearson_corrcoef_update(
24     preds: Tensor,
25     target: Tensor,
26     mean_x: Tensor,
27     mean_y: Tensor,
28     var_x: Tensor,
29     var_y: Tensor,
30     corr_xy: Tensor,
31     n_prior: Tensor,
32     num_outputs: int,
33 ) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
34     """Update and returns variables required to compute Pearson Correlation Coefficient.
35 
36     Check for same shape of input tensors.
37 
38     Args:
39         preds: estimated scores
40         target: ground truth scores
41         mean_x: current mean estimate of x tensor
42         mean_y: current mean estimate of y tensor
43         var_x: current variance estimate of x tensor
44         var_y: current variance estimate of y tensor
45         corr_xy: current covariance estimate between x and y tensor
46         n_prior: current number of observed observations
47         num_outputs: Number of outputs in multioutput setting
48     """
49     # Data checking
50     _check_same_shape(preds, target)
51     _check_data_shape_to_num_outputs(preds, target, num_outputs)
52 
53     n_obs = preds.shape[0]
54     mx_new = (n_prior * mean_x + preds.mean(0) * n_obs) / (n_prior + n_obs)
55     my_new = (n_prior * mean_y + target.mean(0) * n_obs) / (n_prior + n_obs)
56     n_prior += n_obs
57     var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)
58     var_y += ((target - my_new) * (target - mean_y)).sum(0)
59     corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)
60     mean_x = mx_new
61     mean_y = my_new
62 
63     return mean_x, mean_y, var_x, var_y, corr_xy, n_prior
64 
65 
66 def _pearson_corrcoef_compute(
67     var_x: Tensor,
68     var_y: Tensor,
69     corr_xy: Tensor,
70     nb: Tensor,
71 ) -> Tensor:
72     """Compute the final pearson correlation based on accumulated statistics.
73 
74     Args:
75         var_x: variance estimate of x tensor
76         var_y: variance estimate of y tensor
77         corr_xy: covariance estimate between x and y tensor
78         nb: number of observations
79     """
80     var_x /= nb - 1
81     var_y /= nb - 1
82     corr_xy /= nb - 1
83     corrcoef = (corr_xy / (var_x * var_y).sqrt()).squeeze()
84     return torch.clamp(corrcoef, -1.0, 1.0)
85 
86 
87 def pearson_corrcoef(preds: Tensor, target: Tensor) -> Tensor:
88     """Compute pearson correlation coefficient.
89 
90     Args:
91         preds: estimated scores
92         target: ground truth scores
93 
94     Example (single output regression):
95         >>> from torchmetrics.functional.regression import pearson_corrcoef
96         >>> target = torch.tensor([3, -0.5, 2, 7])
97         >>> preds = torch.tensor([2.5, 0.0, 2, 8])
98         >>> pearson_corrcoef(preds, target)
99         tensor(0.9849)
100 
101     Example (multi output regression):
102         >>> from torchmetrics.functional.regression import pearson_corrcoef
103         >>> target = torch.tensor([[3, -0.5], [2, 7]])
104         >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
105         >>> pearson_corrcoef(preds, target)
106         tensor([1., 1.])
107     """
108     d = preds.shape[1] if preds.ndim == 2 else 1
109     _temp = torch.zeros(d, dtype=preds.dtype, device=preds.device)
110     mean_x, mean_y, var_x = _temp.clone(), _temp.clone(), _temp.clone()
111     var_y, corr_xy, nb = _temp.clone(), _temp.clone(), _temp.clone()
112     _, _, var_x, var_y, corr_xy, nb = _pearson_corrcoef_update(
113         preds, target, mean_x, mean_y, var_x, var_y, corr_xy, nb, num_outputs=1 if preds.ndim == 1 else preds.shape[-1]
114     )
115     return _pearson_corrcoef_compute(var_x, var_y, corr_xy, nb)
116
```
--- 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/src/torchmetrics/functional/regression/pearson.py b/src/torchmetrics/functional/regression/pearson.py
--- a/src/torchmetrics/functional/regression/pearson.py
+++ b/src/torchmetrics/functional/regression/pearson.py
@@ -49,13 +49,25 @@
     # Data checking
     _check_same_shape(preds, target)
     _check_data_shape_to_num_outputs(preds, target, num_outputs)
+    cond = n_prior.mean() > 0
 
     n_obs = preds.shape[0]
-    mx_new = (n_prior * mean_x + preds.mean(0) * n_obs) / (n_prior + n_obs)
-    my_new = (n_prior * mean_y + target.mean(0) * n_obs) / (n_prior + n_obs)
+    if cond:
+        mx_new = (n_prior * mean_x + preds.sum(0)) / (n_prior + n_obs)
+        my_new = (n_prior * mean_y + target.sum(0)) / (n_prior + n_obs)
+    else:
+        mx_new = preds.mean(0)
+        my_new = target.mean(0)
+
     n_prior += n_obs
-    var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)
-    var_y += ((target - my_new) * (target - mean_y)).sum(0)
+
+    if cond:
+        var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)
+        var_y += ((target - my_new) * (target - mean_y)).sum(0)
+
+    else:
+        var_x += preds.var(0) * (n_obs - 1)
+        var_y += target.var(0) * (n_obs - 1)
     corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)
     mean_x = mx_new
     mean_y = my_new

{"golden_diff": "diff --git a/src/torchmetrics/functional/regression/pearson.py b/src/torchmetrics/functional/regression/pearson.py\n--- a/src/torchmetrics/functional/regression/pearson.py\n+++ b/src/torchmetrics/functional/regression/pearson.py\n@@ -49,13 +49,25 @@\n     # Data checking\n     _check_same_shape(preds, target)\n     _check_data_shape_to_num_outputs(preds, target, num_outputs)\n+    cond = n_prior.mean() > 0\n \n     n_obs = preds.shape[0]\n-    mx_new = (n_prior * mean_x + preds.mean(0) * n_obs) / (n_prior + n_obs)\n-    my_new = (n_prior * mean_y + target.mean(0) * n_obs) / (n_prior + n_obs)\n+    if cond:\n+        mx_new = (n_prior * mean_x + preds.sum(0)) / (n_prior + n_obs)\n+        my_new = (n_prior * mean_y + target.sum(0)) / (n_prior + n_obs)\n+    else:\n+        mx_new = preds.mean(0)\n+        my_new = target.mean(0)\n+\n     n_prior += n_obs\n-    var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)\n-    var_y += ((target - my_new) * (target - mean_y)).sum(0)\n+\n+    if cond:\n+        var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)\n+        var_y += ((target - my_new) * (target - mean_y)).sum(0)\n+\n+    else:\n+        var_x += preds.var(0) * (n_obs - 1)\n+        var_y += target.var(0) * (n_obs - 1)\n     corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)\n     mean_x = mx_new\n     mean_y = my_new\n", "issue": "PearsonCorrCoeff returns nan when input is of type torch.float16 or torch.bfloat16.\n## \ud83d\udc1b Bug\r\n\r\nPearsonCorrCoeff returns nan when input is of type torch.float16 or torch.bfloat16 and all values are close.\r\n\r\n### To Reproduce\r\n\r\n<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->\r\n\r\n<details>\r\n  <summary>\r\n\r\n```python\r\nimport torch\r\nimport torchmetrics as tm\r\npcc = tm.regression.PearsonCorrCoef().to(\"cuda\")\r\npred = torch.tensor([0.4746, 0.4805, 0.4766, 0.4805, 0.4766, 0.4805, 0.4785, 0.4824, 0.4805],dtype=torch.float16).to(\"cuda\")\r\ntarget = torch.tensor([0.0336, 0.3676, 0.6302, 0.7192, 0.2295, 0.2886, 0.6302, 0.7096, 0.0208],dtype=torch.float16).to(\"cuda\")\r\nprint(pcc(pred,target))\r\nprint(pcc(pred.to(torch.float32),target.to(torch.float32)))\r\ntensor(nan, device='cuda:0')\r\ntensor(0.3720, device='cuda:0')\r\n```\r\n\r\n</summary>\r\n\r\n<!-- Ideally attach a minimal code sample to reproduce the decried issue.\r\nMinimal means having the shortest code but still preserving the bug. -->\r\n\r\n</details>\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\nPython version: 3.10.9\r\nTorch version: 1.12.1\r\nTorchMetrics version: 0.11.1\r\nGPU device name: Tesla T4\r\nCUDA Version: 11.4 \r\n\r\n### Additional context\r\n\r\nWhen running in a training loop I found that some fraction (~30%) of steps would not produce a nan number when using torch.float16 or bfloat16, while the other ~70% would.\r\nThis seems to occur because the values in pred above are not very different (changing one value of pred above to be more different than the rest will compute a correct PCC), however I think that this should still be able to be computed with half precision and the standard deviation of pred shown above.\r\n<!-- Add any other context about the problem here. -->\r\n\n", "before_files": [{"content": "# Copyright The 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 Tuple\n\nimport torch\nfrom torch import Tensor\n\nfrom torchmetrics.functional.regression.utils import _check_data_shape_to_num_outputs\nfrom torchmetrics.utilities.checks import _check_same_shape\n\n\ndef _pearson_corrcoef_update(\n    preds: Tensor,\n    target: Tensor,\n    mean_x: Tensor,\n    mean_y: Tensor,\n    var_x: Tensor,\n    var_y: Tensor,\n    corr_xy: Tensor,\n    n_prior: Tensor,\n    num_outputs: int,\n) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:\n    \"\"\"Update and returns variables required to compute Pearson Correlation Coefficient.\n\n    Check for same shape of input tensors.\n\n    Args:\n        preds: estimated scores\n        target: ground truth scores\n        mean_x: current mean estimate of x tensor\n        mean_y: current mean estimate of y tensor\n        var_x: current variance estimate of x tensor\n        var_y: current variance estimate of y tensor\n        corr_xy: current covariance estimate between x and y tensor\n        n_prior: current number of observed observations\n        num_outputs: Number of outputs in multioutput setting\n    \"\"\"\n    # Data checking\n    _check_same_shape(preds, target)\n    _check_data_shape_to_num_outputs(preds, target, num_outputs)\n\n    n_obs = preds.shape[0]\n    mx_new = (n_prior * mean_x + preds.mean(0) * n_obs) / (n_prior + n_obs)\n    my_new = (n_prior * mean_y + target.mean(0) * n_obs) / (n_prior + n_obs)\n    n_prior += n_obs\n    var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)\n    var_y += ((target - my_new) * (target - mean_y)).sum(0)\n    corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)\n    mean_x = mx_new\n    mean_y = my_new\n\n    return mean_x, mean_y, var_x, var_y, corr_xy, n_prior\n\n\ndef _pearson_corrcoef_compute(\n    var_x: Tensor,\n    var_y: Tensor,\n    corr_xy: Tensor,\n    nb: Tensor,\n) -> Tensor:\n    \"\"\"Compute the final pearson correlation based on accumulated statistics.\n\n    Args:\n        var_x: variance estimate of x tensor\n        var_y: variance estimate of y tensor\n        corr_xy: covariance estimate between x and y tensor\n        nb: number of observations\n    \"\"\"\n    var_x /= nb - 1\n    var_y /= nb - 1\n    corr_xy /= nb - 1\n    corrcoef = (corr_xy / (var_x * var_y).sqrt()).squeeze()\n    return torch.clamp(corrcoef, -1.0, 1.0)\n\n\ndef pearson_corrcoef(preds: Tensor, target: Tensor) -> Tensor:\n    \"\"\"Compute pearson correlation coefficient.\n\n    Args:\n        preds: estimated scores\n        target: ground truth scores\n\n    Example (single output regression):\n        >>> from torchmetrics.functional.regression import pearson_corrcoef\n        >>> target = torch.tensor([3, -0.5, 2, 7])\n        >>> preds = torch.tensor([2.5, 0.0, 2, 8])\n        >>> pearson_corrcoef(preds, target)\n        tensor(0.9849)\n\n    Example (multi output regression):\n        >>> from torchmetrics.functional.regression import pearson_corrcoef\n        >>> target = torch.tensor([[3, -0.5], [2, 7]])\n        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])\n        >>> pearson_corrcoef(preds, target)\n        tensor([1., 1.])\n    \"\"\"\n    d = preds.shape[1] if preds.ndim == 2 else 1\n    _temp = torch.zeros(d, dtype=preds.dtype, device=preds.device)\n    mean_x, mean_y, var_x = _temp.clone(), _temp.clone(), _temp.clone()\n    var_y, corr_xy, nb = _temp.clone(), _temp.clone(), _temp.clone()\n    _, _, var_x, var_y, corr_xy, nb = _pearson_corrcoef_update(\n        preds, target, mean_x, mean_y, var_x, var_y, corr_xy, nb, num_outputs=1 if preds.ndim == 1 else preds.shape[-1]\n    )\n    return _pearson_corrcoef_compute(var_x, var_y, corr_xy, nb)\n", "path": "src/torchmetrics/functional/regression/pearson.py"}], "after_files": [{"content": "# Copyright The 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 Tuple\n\nimport torch\nfrom torch import Tensor\n\nfrom torchmetrics.functional.regression.utils import _check_data_shape_to_num_outputs\nfrom torchmetrics.utilities.checks import _check_same_shape\n\n\ndef _pearson_corrcoef_update(\n    preds: Tensor,\n    target: Tensor,\n    mean_x: Tensor,\n    mean_y: Tensor,\n    var_x: Tensor,\n    var_y: Tensor,\n    corr_xy: Tensor,\n    n_prior: Tensor,\n    num_outputs: int,\n) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:\n    \"\"\"Update and returns variables required to compute Pearson Correlation Coefficient.\n\n    Check for same shape of input tensors.\n\n    Args:\n        preds: estimated scores\n        target: ground truth scores\n        mean_x: current mean estimate of x tensor\n        mean_y: current mean estimate of y tensor\n        var_x: current variance estimate of x tensor\n        var_y: current variance estimate of y tensor\n        corr_xy: current covariance estimate between x and y tensor\n        n_prior: current number of observed observations\n        num_outputs: Number of outputs in multioutput setting\n    \"\"\"\n    # Data checking\n    _check_same_shape(preds, target)\n    _check_data_shape_to_num_outputs(preds, target, num_outputs)\n    cond = n_prior.mean() > 0\n\n    n_obs = preds.shape[0]\n    if cond:\n        mx_new = (n_prior * mean_x + preds.sum(0)) / (n_prior + n_obs)\n        my_new = (n_prior * mean_y + target.sum(0)) / (n_prior + n_obs)\n    else:\n        mx_new = preds.mean(0)\n        my_new = target.mean(0)\n\n    n_prior += n_obs\n\n    if cond:\n        var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)\n        var_y += ((target - my_new) * (target - mean_y)).sum(0)\n\n    else:\n        var_x += preds.var(0) * (n_obs - 1)\n        var_y += target.var(0) * (n_obs - 1)\n    corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)\n    mean_x = mx_new\n    mean_y = my_new\n\n    return mean_x, mean_y, var_x, var_y, corr_xy, n_prior\n\n\ndef _pearson_corrcoef_compute(\n    var_x: Tensor,\n    var_y: Tensor,\n    corr_xy: Tensor,\n    nb: Tensor,\n) -> Tensor:\n    \"\"\"Compute the final pearson correlation based on accumulated statistics.\n\n    Args:\n        var_x: variance estimate of x tensor\n        var_y: variance estimate of y tensor\n        corr_xy: covariance estimate between x and y tensor\n        nb: number of observations\n    \"\"\"\n    var_x /= nb - 1\n    var_y /= nb - 1\n    corr_xy /= nb - 1\n    corrcoef = (corr_xy / (var_x * var_y).sqrt()).squeeze()\n    return torch.clamp(corrcoef, -1.0, 1.0)\n\n\ndef pearson_corrcoef(preds: Tensor, target: Tensor) -> Tensor:\n    \"\"\"Compute pearson correlation coefficient.\n\n    Args:\n        preds: estimated scores\n        target: ground truth scores\n\n    Example (single output regression):\n        >>> from torchmetrics.functional.regression import pearson_corrcoef\n        >>> target = torch.tensor([3, -0.5, 2, 7])\n        >>> preds = torch.tensor([2.5, 0.0, 2, 8])\n        >>> pearson_corrcoef(preds, target)\n        tensor(0.9849)\n\n    Example (multi output regression):\n        >>> from torchmetrics.functional.regression import pearson_corrcoef\n        >>> target = torch.tensor([[3, -0.5], [2, 7]])\n        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])\n        >>> pearson_corrcoef(preds, target)\n        tensor([1., 1.])\n    \"\"\"\n    d = preds.shape[1] if preds.ndim == 2 else 1\n    _temp = torch.zeros(d, dtype=preds.dtype, device=preds.device)\n    mean_x, mean_y, var_x = _temp.clone(), _temp.clone(), _temp.clone()\n    var_y, corr_xy, nb = _temp.clone(), _temp.clone(), _temp.clone()\n    _, _, var_x, var_y, corr_xy, nb = _pearson_corrcoef_update(\n        preds, target, mean_x, mean_y, var_x, var_y, corr_xy, nb, num_outputs=1 if preds.ndim == 1 else preds.shape[-1]\n    )\n    return _pearson_corrcoef_compute(var_x, var_y, corr_xy, nb)\n", "path": "src/torchmetrics/functional/regression/pearson.py"}]}