File size: 8,105 Bytes
1099afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdedf43
1099afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DEPENDENCIES
import sys
import threading
from enum import Enum
from typing import Any
from typing import Dict
from pathlib import Path
from typing import Optional
from dataclasses import field
from datetime import datetime
from dataclasses import dataclass

# Add parent directory to path for imports
sys.path.append(str(Path(__file__).parent.parent))

from utils.logger import log_info
from utils.logger import log_error
from utils.logger import ContractAnalyzerLogger 


class ModelType(Enum):
    """
    Enum for model types
    """
    LEGAL_BERT = "legal-bert"
    EMBEDDING  = "embedding"
    TOKENIZER  = "tokenizer"
    CLASSIFIER = "classifier"


class ModelStatus(Enum):
    """
    Model loading status
    """
    NOT_LOADED = "not_loaded"
    LOADING    = "loading"
    LOADED     = "loaded"
    ERROR      = "error"


@dataclass
class ModelInfo:
    """
    Model metadata and state
    """
    name           : str
    type           : ModelType
    status         : ModelStatus        = ModelStatus.NOT_LOADED
    model          : Optional[Any]      = None
    tokenizer      : Optional[Any]      = None
    loaded_at      : Optional[datetime] = None
    error_message  : Optional[str]      = None
    memory_size_mb : float              = 0.0
    access_count   : int                = 0
    last_accessed  : Optional[datetime] = None
    metadata       : Dict[str, Any]     = field(default_factory = dict)
    

    def mark_accessed(self):
        """
        Update access statistics
        """
        self.access_count += 1
        self.last_accessed = datetime.now()
    

    def get_age_seconds(self) -> float:
        """
        Get seconds since last access
        """
        if self.last_accessed:
            return (datetime.now() - self.last_accessed).total_seconds()
        
        return float('inf')


class ModelRegistry:
    """
    Thread-safe singleton model registry : manages all loaded models with LRU eviction
    """
    _instance = None
    _lock     = threading.Lock()
    
    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    cls._instance              = super().__new__(cls)
                    cls._instance._initialized = False
        
        return cls._instance
    

    def __init__(self):
        if self._initialized:
            return
        
        self._registry : Dict[ModelType, ModelInfo] = dict()
        self._model_lock                            = threading.Lock()
        self._max_models                            = 3  # LRU cache size
        self._initialized                           = True
        self.logger                                 = ContractAnalyzerLogger.get_logger()
        
        log_info("ModelRegistry initialized", max_models = self._max_models)
    

    def register(self, model_type: ModelType, model_info: ModelInfo):
        """
        Register a model (thread-safe)
        """
        with self._model_lock:
            self._registry[model_type] = model_info
            self._enforce_cache_limit()
            
            log_info(f"Model registered: {model_type.value}",
                     model_name = model_info.name,
                     status     = model_info.status.value,
                     memory_mb  = model_info.memory_size_mb,
                    )
    

    def get(self, model_type: ModelType) -> Optional[ModelInfo]:
        """
        Get model info (thread-safe)
        """
        with self._model_lock:
            info = self._registry.get(model_type)
            
            if info:
                info.mark_accessed()
                log_info(f"Model accessed: {model_type.value}",
                         access_count = info.access_count,
                         status       = info.status.value,
                        )

            return info
    

    def is_loaded(self, model_type: ModelType) -> bool:
        """
        Check if model is loaded
        """
        info = self.get(model_type)
        return info is not None and (info.status == ModelStatus.LOADED)
    

    def unload(self, model_type: ModelType):
        """
        Unload a model from memory
        """
        with self._model_lock:
            if model_type in self._registry:
                info = self._registry[model_type]
                
                log_info(f"Unloading model: {model_type.value}",
                         memory_freed_mb = info.memory_size_mb,
                         access_count    = info.access_count,
                        )
                
                # Clear references to allow garbage collection
                info.model     = None
                info.tokenizer = None
                info.status    = ModelStatus.NOT_LOADED

                del self._registry[model_type]
    

    def get_all_loaded(self) -> list[ModelInfo]:
        """
        Get all loaded models
        """
        with self._model_lock:
            loaded = [info for info in self._registry.values() if (info.status == ModelStatus.LOADED)]
            
            if loaded:
                log_info(f"Retrieved {len(loaded)} loaded models", models = [info.name for info in loaded])
            
            return loaded
    

    def get_memory_usage(self) -> float:
        """
        Get total memory usage in MB
        """
        with self._model_lock:
            total = sum(info.memory_size_mb for info in self._registry.values() if (info.status == ModelStatus.LOADED))

            return total
    

    def _enforce_cache_limit(self):
        """
        Enforce LRU cache limit
        """
        loaded_models = [(model_type, info) for model_type, info in self._registry.items() if (info.status == ModelStatus.LOADED)]
        
        if (len(loaded_models) > self._max_models):
            # Sort by last access time (oldest first)
            loaded_models.sort(key = lambda x: x[1].get_age_seconds(), reverse = True)
            
            # Unload oldest models
            for model_type, info in loaded_models[self._max_models:]:
                log_info(f"LRU eviction: {model_type.value}",
                         reason      = "cache_limit_exceeded",
                         age_seconds = info.get_age_seconds(),
                         max_models  = self._max_models,
                        )

                self.unload(model_type)
    

    def clear_all(self):
        """
        Clear all models from registry
        """
        with self._model_lock:
            model_count  = len(self._registry)
            total_memory = self.get_memory_usage()
            
            log_info("Clearing all models from registry",
                     models_cleared  = model_count,
                     memory_freed_mb = total_memory,
                    )
            
            for model_type in list(self._registry.keys()):
                self.unload(model_type)
    

    def get_stats(self) -> Dict[str, Any]:
        """
        Get registry statistics
        """
        with self._model_lock:
            stats = {"total_models"    : len(self._registry),
                     "loaded_models"   : sum(1 for info in self._registry.values() if info.status == ModelStatus.LOADED),
                     "total_memory_mb" : self.get_memory_usage(),
                     "models"          : {model_type.value: {"status"        : info.status.value,
                                                             "access_count"  : info.access_count,
                                                             "memory_mb"     : info.memory_size_mb,
                                                             "last_accessed" : info.last_accessed.isoformat() if info.last_accessed else None,
                                                            }
                                          for model_type, info in self._registry.items()
                                         },
                    }
            
            log_info("Registry stats retrieved", **stats)
            
            return stats