File size: 2,408 Bytes
5b14aa2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | """GPU utility functions for detecting and managing GPU availability."""
import logging
from typing import Dict, Optional
logger = logging.getLogger(__name__)
def is_gpu_available() -> bool:
"""Check if GPU is available for deep learning models.
Returns:
True if GPU is available, False otherwise
"""
try:
import torch
if torch.cuda.is_available():
gpu_count = torch.cuda.device_count()
gpu_name = torch.cuda.get_device_name(0) if gpu_count > 0 else "Unknown"
logger.info(f"GPU detected: {gpu_name} (count: {gpu_count})")
return True
else:
logger.info("No CUDA GPU available")
return False
except ImportError:
logger.info("PyTorch not available, assuming no GPU")
return False
except Exception as e:
logger.warning(f"Error checking GPU availability: {e}")
return False
def get_gpu_info() -> Dict:
"""Get detailed GPU information.
Returns:
Dictionary with GPU information
"""
info = {
"available": False,
"count": 0,
"names": [],
"memory": []
}
try:
import torch
if torch.cuda.is_available():
info["available"] = True
info["count"] = torch.cuda.device_count()
info["names"] = [torch.cuda.get_device_name(i) for i in range(info["count"])]
info["memory"] = [torch.cuda.get_device_properties(i).total_memory for i in range(info["count"])]
except ImportError:
pass
except Exception as e:
logger.warning(f"Error getting GPU info: {e}")
return info
def should_use_gpu_processor() -> bool:
"""Determine if GPU processor should be used based on GPU availability.
Returns:
True if GPU processor should be used, False otherwise
"""
return is_gpu_available()
def get_processor_preference() -> str:
"""Get the preferred processor type based on system capabilities.
Returns:
'gpu' if GPU is available
Raises:
RuntimeError: If GPU is not available
"""
if should_use_gpu_processor():
return 'gpu'
else:
raise RuntimeError(
"GPU is not available. Please ensure CUDA is installed and a compatible GPU is present, "
"or use cloud processing mode."
) |