Spaces:
Runtime error
Runtime error
Create setup_device.py
Browse files- setup_device.py +18 -0
setup_device.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
def get_device():
|
| 4 |
+
if torch.cuda.is_available():
|
| 5 |
+
device = torch.device("cuda")
|
| 6 |
+
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
|
| 7 |
+
else:
|
| 8 |
+
device = torch.device("cpu")
|
| 9 |
+
print("CUDA not available. Using CPU.")
|
| 10 |
+
return device
|
| 11 |
+
|
| 12 |
+
# Usage example:
|
| 13 |
+
if __name__ == "__main__":
|
| 14 |
+
device = get_device()
|
| 15 |
+
|
| 16 |
+
# Example: Create a tensor on the selected device
|
| 17 |
+
x = torch.rand(3, 3).to(device)
|
| 18 |
+
print(f"Tensor device: {x.device}")
|