File size: 11,647 Bytes
fcf0a07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""

Memory Manager for Mamba Swarm

Handles memory optimization, caching, and distributed memory management

"""

import torch
import torch.nn as nn
import gc
import psutil
import threading
from typing import Dict, Any, Optional, List, Tuple
from dataclasses import dataclass
from collections import OrderedDict
import numpy as np
import logging

@dataclass
class MemoryStats:
    total_memory: float
    used_memory: float
    free_memory: float
    gpu_memory: float
    gpu_free: float
    cache_size: float

class LRUCache:
    """Least Recently Used cache for model states and activations"""
    
    def __init__(self, max_size: int = 1000):
        self.max_size = max_size
        self.cache = OrderedDict()
        self.lock = threading.Lock()
    
    def get(self, key: str) -> Optional[torch.Tensor]:
        with self.lock:
            if key in self.cache:
                # Move to end (most recently used)
                value = self.cache.pop(key)
                self.cache[key] = value
                return value
            return None
    
    def put(self, key: str, value: torch.Tensor):
        with self.lock:
            if key in self.cache:
                self.cache.pop(key)
            elif len(self.cache) >= self.max_size:
                # Remove least recently used
                oldest_key = next(iter(self.cache))
                old_value = self.cache.pop(oldest_key)
                del old_value
            
            self.cache[key] = value.clone() if isinstance(value, torch.Tensor) else value
    
    def clear(self):
        with self.lock:
            self.cache.clear()
            gc.collect()

class GradientAccumulator:
    """Manages gradient accumulation across multiple steps"""
    
    def __init__(self, accumulation_steps: int = 8):
        self.accumulation_steps = accumulation_steps
        self.current_step = 0
        self.accumulated_gradients = {}
    
    def accumulate(self, model: nn.Module):
        """Accumulate gradients from current backward pass"""
        for name, param in model.named_parameters():
            if param.grad is not None:
                if name not in self.accumulated_gradients:
                    self.accumulated_gradients[name] = param.grad.clone()
                else:
                    self.accumulated_gradients[name] += param.grad
        
        self.current_step += 1
    
    def should_update(self) -> bool:
        """Check if we should perform optimizer step"""
        return self.current_step % self.accumulation_steps == 0
    
    def get_averaged_gradients(self) -> Dict[str, torch.Tensor]:
        """Get accumulated gradients averaged over accumulation steps"""
        averaged = {}
        for name, grad in self.accumulated_gradients.items():
            averaged[name] = grad / self.accumulation_steps
        return averaged
    
    def reset(self):
        """Reset accumulator"""
        self.accumulated_gradients.clear()
        self.current_step = 0

class MemoryManager:
    """Comprehensive memory management for Mamba Swarm"""
    
    def __init__(self, 

                 max_cache_size: int = 2000,

                 gradient_accumulation_steps: int = 8,

                 auto_cleanup: bool = True,

                 memory_threshold: float = 0.85):
        
        self.logger = logging.getLogger(__name__)
        self.max_cache_size = max_cache_size
        self.gradient_accumulation_steps = gradient_accumulation_steps
        self.auto_cleanup = auto_cleanup
        self.memory_threshold = memory_threshold
        
        # Initialize components
        self.activation_cache = LRUCache(max_cache_size)
        self.state_cache = LRUCache(max_cache_size // 2)
        self.gradient_accumulator = GradientAccumulator(gradient_accumulation_steps)
        
        # Memory tracking
        self.peak_memory_usage = 0.0
        self.memory_history = []
        self.cleanup_threshold = memory_threshold
        
        # Device management
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.setup_memory_optimization()
    
    def setup_memory_optimization(self):
        """Setup memory optimization settings"""
        if torch.cuda.is_available():
            # Enable memory mapping for large tensors
            torch.backends.cuda.matmul.allow_tf32 = True
            torch.backends.cudnn.allow_tf32 = True
            
            # Set memory fraction
            if hasattr(torch.cuda, 'set_per_process_memory_fraction'):
                torch.cuda.set_per_process_memory_fraction(0.9)
    
    def get_memory_stats(self) -> MemoryStats:
        """Get current memory statistics"""
        # System memory
        memory = psutil.virtual_memory()
        total_memory = memory.total / (1024**3)  # GB
        used_memory = memory.used / (1024**3)
        free_memory = memory.available / (1024**3)
        
        # GPU memory
        gpu_memory = 0.0
        gpu_free = 0.0
        if torch.cuda.is_available():
            gpu_memory = torch.cuda.memory_allocated() / (1024**3)
            gpu_free = (torch.cuda.memory_reserved() - torch.cuda.memory_allocated()) / (1024**3)
        
        # Cache size estimation
        cache_size = (len(self.activation_cache.cache) + len(self.state_cache.cache)) * 0.001  # Rough estimate
        
        stats = MemoryStats(
            total_memory=total_memory,
            used_memory=used_memory,
            free_memory=free_memory,
            gpu_memory=gpu_memory,
            gpu_free=gpu_free,
            cache_size=cache_size
        )
        
        # Update peak usage
        current_usage = used_memory + gpu_memory
        if current_usage > self.peak_memory_usage:
            self.peak_memory_usage = current_usage
        
        return stats
    
    def check_memory_pressure(self) -> bool:
        """Check if system is under memory pressure"""
        stats = self.get_memory_stats()
        memory_usage_ratio = stats.used_memory / stats.total_memory
        
        if torch.cuda.is_available():
            gpu_usage_ratio = stats.gpu_memory / (stats.gpu_memory + stats.gpu_free + 1e-6)
            return memory_usage_ratio > self.cleanup_threshold or gpu_usage_ratio > self.cleanup_threshold
        
        return memory_usage_ratio > self.cleanup_threshold
    
    def cleanup_memory(self, aggressive: bool = False):
        """Perform memory cleanup"""
        if aggressive:
            self.activation_cache.clear()
            self.state_cache.clear()
            self.gradient_accumulator.reset()
        
        # Python garbage collection
        gc.collect()
        
        # GPU memory cleanup
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
            torch.cuda.synchronize()
        
        self.logger.info(f"Memory cleanup completed. Aggressive: {aggressive}")
    
    def cache_activation(self, key: str, activation: torch.Tensor):
        """Cache activation with memory pressure check"""
        if self.auto_cleanup and self.check_memory_pressure():
            self.cleanup_memory()
        
        self.activation_cache.put(key, activation)
    
    def get_cached_activation(self, key: str) -> Optional[torch.Tensor]:
        """Retrieve cached activation"""
        return self.activation_cache.get(key)
    
    def cache_hidden_state(self, key: str, state: torch.Tensor):
        """Cache hidden state"""
        self.state_cache.put(key, state)
    
    def get_cached_state(self, key: str) -> Optional[torch.Tensor]:
        """Retrieve cached hidden state"""
        return self.state_cache.get(key)
    
    def manage_gradient_accumulation(self, model: nn.Module) -> bool:
        """Manage gradient accumulation and return if optimizer step should be taken"""
        self.gradient_accumulator.accumulate(model)
        
        if self.gradient_accumulator.should_update():
            # Apply accumulated gradients
            averaged_grads = self.gradient_accumulator.get_averaged_gradients()
            
            for name, param in model.named_parameters():
                if name in averaged_grads:
                    param.grad = averaged_grads[name]
            
            self.gradient_accumulator.reset()
            return True
        
        return False
    
    def optimize_model_memory(self, model: nn.Module):
        """Optimize model memory usage"""
        # Enable gradient checkpointing for large models
        for module in model.modules():
            if hasattr(module, 'gradient_checkpointing'):
                module.gradient_checkpointing = True
        
        # Convert to half precision if possible
        if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 7:
            model = model.half()
        
        return model
    
    def create_memory_efficient_dataloader(self, dataset, batch_size: int, **kwargs):
        """Create memory-efficient dataloader"""
        # Adjust batch size based on available memory
        stats = self.get_memory_stats()
        
        if stats.free_memory < 2.0:  # Less than 2GB free
            batch_size = max(1, batch_size // 2)
            self.logger.warning(f"Reduced batch size to {batch_size} due to low memory")
        
        return torch.utils.data.DataLoader(
            dataset,
            batch_size=batch_size,
            num_workers=min(4, psutil.cpu_count()),
            pin_memory=torch.cuda.is_available(),
            prefetch_factor=2,
            **kwargs
        )
    
    def monitor_memory_usage(self):
        """Monitor and log memory usage"""
        stats = self.get_memory_stats()
        self.memory_history.append({
            'timestamp': torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None,
            'stats': stats
        })
        
        # Keep only recent history
        if len(self.memory_history) > 100:
            self.memory_history = self.memory_history[-50:]
        
        self.logger.debug(f"Memory - System: {stats.used_memory:.2f}GB/{stats.total_memory:.2f}GB, "
                         f"GPU: {stats.gpu_memory:.2f}GB, Cache: {stats.cache_size:.2f}GB")
    
    def get_memory_report(self) -> Dict[str, Any]:
        """Generate comprehensive memory report"""
        stats = self.get_memory_stats()
        
        return {
            'current_stats': stats.__dict__,
            'peak_usage': self.peak_memory_usage,
            'cache_stats': {
                'activation_cache_size': len(self.activation_cache.cache),
                'state_cache_size': len(self.state_cache.cache),
                'max_cache_size': self.max_cache_size
            },
            'gradient_accumulation': {
                'current_step': self.gradient_accumulator.current_step,
                'accumulation_steps': self.gradient_accumulation_steps,
                'accumulated_params': len(self.gradient_accumulator.accumulated_gradients)
            },
            'memory_pressure': self.check_memory_pressure(),
            'device': str(self.device)
        }
    
    def __enter__(self):
        """Context manager entry"""
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager exit with cleanup"""
        self.cleanup_memory(aggressive=True)