Repository: huggingface/diffusers
Pull request: [Contributor Experience] Fix test collection on MPS
# What does this PR do?

Fixes test collection on MPS.

Fixes #6807


## Before submitting
- [x] Did you read the [contributor guideline](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md)?
- [x] Did you read our [philosophy doc](https://github.com/huggingface/diffusers/blob/main/PHILOSOPHY.md) (important for complex PRs)?

## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Core library:
- JAX and MPS: @pcuenca


Problem statement:
[Contributor Experience] Test Collection fails on MPS
### Describe the bug

On MPS, the test collection fails. Therefore not all tests are shown e.g. in VS Code, which makes the contributor experience more tedious.

Currently, 4515-4486 = 29 tests are missed.

---

The error is caused because in `utls/testing_utils.py` the variable `torch_device` is defined as a string (`"cpu"`, `"cuda"` or `"mps"`), but used as a device (i.e., `torch_device.type` is called).

I'll submit a small fix PR.

### Reproduction

`pytest --collect-only tests`

### Logs

```shell
============================================================================ ERRORS ============================================================================
______________________________________________________ ERROR collecting tests/models/test_layers_utils.py ______________________________________________________
src/diffusers/utils/testing_utils.py:862: in _is_torch_fp64_available
    x = torch.zeros((2, 2), dtype=torch.float64).to(device)
E   TypeError: Cannot convert a MPS Tensor to float64 dtype as the MPS framework doesn't support float64. Please use float32 instead.

During handling of the above exception, another exception occurred:
tests/models/test_layers_utils.py:319: in <module>
    class Transformer2DModelTests(unittest.TestCase):
tests/models/test_layers_utils.py:430: in Transformer2DModelTests
    @require_torch_accelerator_with_fp64
src/diffusers/utils/testing_utils.py:241: in require_torch_accelerator_with_fp64
    return unittest.skipUnless(_is_torch_fp64_available(torch_device), "test requires accelerator with fp64 support")(
src/diffusers/utils/testing_utils.py:867: in _is_torch_fp64_available
    if device.type == "cuda":
E   AttributeError: 'str' object has no attribute 'type'
```


### System Info

- `diffusers` version: 0.26.0.dev0
- Platform: macOS-14.2.1-arm64-arm-64bit
- Python version: 3.11.7
- PyTorch version (GPU?): 2.1.2 (False)
- Huggingface_hub version: 0.20.2
- Transformers version: 4.36.2
- Accelerate version: 0.26.1
- xFormers version: not installed
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help?

Questions on Tests: @DN6 @sayakpaul

Hints from issue discussion:

Commit message: Update testing_utils.py

Patch to review:
diff --git a/src/diffusers/utils/testing_utils.py b/src/diffusers/utils/testing_utils.py
index 86e31eb688cd..87551d1ceb60 100644
--- a/src/diffusers/utils/testing_utils.py
+++ b/src/diffusers/utils/testing_utils.py
@@ -840,7 +840,7 @@ def _is_torch_fp16_available(device):
         return True
 
     except Exception as e:
-        if device.type == "cuda":
+        if device == "cuda":
             raise ValueError(
                 f"You have passed a device of type 'cuda' which should work with 'fp16', but 'cuda' does not seem to be correctly installed on your machine: {e}"
             )
@@ -860,7 +860,7 @@ def _is_torch_fp64_available(device):
         return True
 
     except Exception as e:
-        if device.type == "cuda":
+        if device == "cuda":
             raise ValueError(
                 f"You have passed a device of type 'cuda' which should work with 'fp64', but 'cuda' does not seem to be correctly installed on your machine: {e}"
             )


Code review comments:
diff_hunk: @@ -860,7 +860,7 @@ def _is_torch_fp64_available(device):
         return True
 
     except Exception as e:
-        if device.type == "cuda":
path: src/diffusers/utils/testing_utils.py
text: @user1:
Actually the issue seems to only be with the fp64 check. I think we can just convert the `device` str to device object like we have in the fp16 test. 

```python 
device = torch.device(device)
```

@author:
You're right - updated!