Repository: Lightning-AI/torchmetrics

Issue:
MAE can't handle MPS backend
## 🐛 Bug

The currently, the mean absolute error can't handle the MPS backend. This is a simple fix and just requires casting to .float()

### To Reproduce

This works:

```python
import torchmetrics
import torch

a = torch.tensor([1, 2, 3]).to(torch.device("mps"))

acc = torchmetrics.Accuracy().to(torch.device("mps"))
acc.update(a, a)
acc.compute()
```

this also works:

```
a = torch.tensor([1, 2, 3])

mae = torchmetrics.MeanAbsoluteError()
mae.update(a, a)
mae.compute()

```

but this crashes

```python
a = torch.tensor([1, 2, 3]).to(torch.device("mps"))

mae = torchmetrics.MeanAbsoluteError().to(torch.device("mps"))
mae.update(a, a)
mae.compute()
```

```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [12], in <module>
      1 a = torch.tensor([1, 2, 3]).to(torch.device("mps"))
      3 acc = torchmetrics.MeanAbsoluteError().to(torch.device("mps"))
----> 4 acc.update(a, a)
      5 acc.compute()

File ~/miniforge3/lib/python3.9/site-packages/torchmetrics/metric.py:391, in Metric._wrap_update.<locals>.wrapped_func(*args, **kwargs)
    389 with torch.set_grad_enabled(self._enable_grad):
    390     try:
--> 391         update(*args, **kwargs)
    392     except RuntimeError as err:
    393         if "Expected all tensors to be on" in str(err):

File ~/miniforge3/lib/python3.9/site-packages/torchmetrics/regression/mae.py:63, in MeanAbsoluteError.update(self, preds, target)
     56 def update(self, preds: Tensor, target: Tensor) -> None:  # type: ignore
     57     """Update state with predictions and targets.
     58 
     59     Args:
     60         preds: Predictions from model
     61         target: Ground truth values
     62     """
---> 63     sum_abs_error, n_obs = _mean_absolute_error_update(preds, target)
     65     self.sum_abs_error += sum_abs_error
     66     self.total += n_obs

File ~/miniforge3/lib/python3.9/site-packages/torchmetrics/functional/regression/mae.py:33, in _mean_absolute_error_update(preds, target)
     23 """Updates and returns variables required to compute Mean Absolute Error.
     24 
     25 Checks for same shape of input tensors.
   (...)
     29     target: Ground truth tensor
     30 """
     32 _check_same_shape(preds, target)
---> 33 sum_abs_error = torch.sum(torch.abs(preds - target))
     34 n_obs = target.numel()
     35 return sum_abs_error, n_obs

TypeError: Operation 'abs_out_mps()' does not support input type 'int64' in MPS backend.
```


### Environment

```
torch       : 1.12.1
lightning   : 2022.9.8
torchmetrics: 0.9.3
```



Discussion:

Resolving patch:
diff --git a/src/torchmetrics/functional/regression/mae.py b/src/torchmetrics/functional/regression/mae.py
--- a/src/torchmetrics/functional/regression/mae.py
+++ b/src/torchmetrics/functional/regression/mae.py
@@ -28,8 +28,9 @@ def _mean_absolute_error_update(preds: Tensor, target: Tensor) -> Tuple[Tensor,
         preds: Predicted tensor
         target: Ground truth tensor
     """
-
     _check_same_shape(preds, target)
+    preds = preds if preds.is_floating_point else preds.float()
+    target = target if target.is_floating_point else target.float()
     sum_abs_error = torch.sum(torch.abs(preds - target))
     n_obs = target.numel()
     return sum_abs_error, n_obs


Test patch: