1
0
Files
ai-school/gpu_utils.py
2025-11-24 14:20:38 +08:00

18 lines
420 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import torch
def print_gpu_availability():
"""打印PyTorch的GPU可用性"""
if torch.cuda.is_available():
print(f"GPU可用{torch.cuda.get_device_name(0)}")
else:
print("GPU不可用")
def get_gpu_device() -> torch.device:
"""获取PyTorch的GPU设备"""
if torch.cuda.is_available():
return torch.device("cuda")
else:
raise Exception("找不到CUDA")