Spaces:
Sleeping
Sleeping
Download device_setup.py from Aayan873/MultiModal_RAG: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Aayan873/MultiModal_RAG/resolve/main/device_setup.py
- Command line
-
hf download hf://spaces/Aayan873/MultiModal_RAG/device_setup.py
-
curl -L -o device_setup.py https://huggingface.co/spaces/Aayan873/MultiModal_RAG/resolve/main/device_setup.py
1.28 kB
| # Device setup utilities for GPU/CPU detection | |
| import torch | |
| import gc | |
| from config import GPU_MEMORY_FRACTION | |
| def setup_device(): | |
| # GPU or CPU setup, Returns device object and bool indicating if GPU is available | |
| if torch.cuda.is_available(): | |
| device = torch.device("cuda") | |
| gpu_name = torch.cuda.get_device_name(0) | |
| gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9 | |
| print(f"Using GPU: {gpu_name} ({gpu_memory:.1f}GB)") | |
| # Seting some fraction of memory to avoid OOM errors | |
| torch.cuda.set_per_process_memory_fraction(GPU_MEMORY_FRACTION) | |
| return device, True | |
| else: | |
| print("GPU not available, using CPU") | |
| return torch.device("cpu"), False | |
| def clear_gpu_memory(): | |
| """Clear GPU memory cache to free up VRAM""" | |
| if torch.cuda.is_available(): | |
| torch.cuda.empty_cache() | |
| gc.collect() | |
| print("GPU memory cache cleared") | |
| def get_gpu_memory_info(): | |
| """Get current GPU memory usage information""" | |
| if torch.cuda.is_available(): | |
| gpu_memory_used = torch.cuda.memory_allocated(0) / 1e9 | |
| gpu_memory_total = torch.cuda.get_device_properties(0).total_memory / 1e9 | |
| return gpu_memory_used, gpu_memory_total | |
| return 0, 0 |