File size: 1,214 Bytes
c7b2ae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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