Spaces:
Build error
Build error
| """ | |
| Optimizations for Hugging Face Free Tier | |
| """ | |
| import os | |
| import torch | |
| import gc | |
| from functools import lru_cache | |
| def free_memory(): | |
| """Clear GPU and CPU memory""" | |
| gc.collect() | |
| if torch.cuda.is_available(): | |
| torch.cuda.empty_cache() | |
| torch.cuda.ipc_collect() | |
| def limit_resources(): | |
| """Set resource limits for free tier""" | |
| # Reduce model precision | |
| torch.set_float32_matmul_precision('medium') | |
| # Limit threads | |
| torch.set_num_threads(2) | |
| # Enable memory efficient settings | |
| os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:128' | |
| # Disable gradients for inference | |
| torch.set_grad_enabled(False) | |
| class ResourceMonitor: | |
| """Monitor and log resource usage""" | |
| def __init__(self): | |
| self.start_memory = self.get_memory_usage() | |
| def get_memory_usage(self): | |
| """Get current memory usage in MB""" | |
| if torch.cuda.is_available(): | |
| return torch.cuda.memory_allocated() / 1024**2 | |
| return 0 | |
| def log_usage(self, stage=""): | |
| """Log memory usage""" | |
| current = self.get_memory_usage() | |
| print(f"๐ Memory usage {stage}: {current:.1f}MB") | |
| return current |