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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import websockets
import json
import numpy as np
from typing import Dict, Any, Optional, Union
import threading
from queue import Queue
import time
import asyncio
import hashlib

class WebSocketGPUStorage:
    # Singleton instance
    _instance = None
    _lock = threading.Lock()

    def __new__(cls, url: str = "wss://factorst-wbs1.hf.space/ws"):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super().__new__(cls)
                cls._instance._init_singleton(url)
            return cls._instance

    def _init_singleton(self, url: str):
        """Initialize the singleton instance"""
        if hasattr(self, 'initialized'):
            return
        
        self.url = url
        self.websocket = None
        self.connected = False
        self.message_queue = Queue()
        self.response_queues: Dict[str, Queue] = {}
        self.lock = threading.Lock()
        self._closing = False
        self._loop = None
        self.error_count = 0
        self.last_error_time = 0
        self.max_retries = 5
        self.tensor_registry: Dict[str, Dict[str, Any]] = {}  # Track tensor metadata
        self.model_registry: Dict[str, Dict[str, Any]] = {}  # Track loaded models
        self.resource_monitor = {
            'vram_used': 0,
            'active_tensors': 0,
            'loaded_models': set()
        }
        
        # Start WebSocket connection in a separate thread
        self.ws_thread = threading.Thread(target=self._run_websocket_loop, daemon=True)
        self.ws_thread.start()
        self.initialized = True
        
    def __init__(self, url: str = "wss://factorst-wbs1.hf.space/ws"):
        """This will actually just return the singleton instance"""
        pass

    def _run_websocket_loop(self):
        self._loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self._loop)
        self._loop.run_until_complete(self._websocket_handler())

    async def _websocket_handler(self):
        while not self._closing:
            try:
                async with websockets.connect(self.url) as websocket:
                    self.websocket = websocket
                    self.connected = True
                    self.error_count = 0  # Reset error count on successful connection
                    print("Connected to GPU storage server")

                    while True:
                        # Handle outgoing messages
                        try:
                            while not self.message_queue.empty():
                                msg_id, operation = self.message_queue.get()
                                await websocket.send(json.dumps(operation))
                                
                                # Wait for response with timeout
                                try:
                                    response = await asyncio.wait_for(websocket.recv(), timeout=30)
                                    response_data = json.loads(response)
                                    
                                    # Put response in corresponding queue
                                    if msg_id in self.response_queues:
                                        self.response_queues[msg_id].put(response_data)
                                except asyncio.TimeoutError:
                                    if msg_id in self.response_queues:
                                        self.response_queues[msg_id].put({
                                            "status": "error",
                                            "message": "Operation timed out"
                                        })
                                except Exception as e:
                                    if msg_id in self.response_queues:
                                        self.response_queues[msg_id].put({
                                            "status": "error",
                                            "message": f"Error processing response: {str(e)}"
                                        })

                        except Exception as e:
                            print(f"Error processing message: {str(e)}")

                        # Keep connection alive with heartbeat
                        try:
                            await websocket.ping()
                        except:
                            break  # Break inner loop on ping failure
                            
                        await asyncio.sleep(0.001)  # 1ms sleep for electron-speed response

            except Exception as e:
                print(f"WebSocket connection error: {e}")
                self.connected = False
                await asyncio.sleep(1)  # Wait before reconnecting

    def _send_operation(self, operation: Dict[str, Any]) -> Dict[str, Any]:
        if self._closing:
            return {"status": "error", "message": "WebSocket is closing"}
            
        if not self.wait_for_connection(timeout=10):
            return {"status": "error", "message": "Not connected to GPU storage server"}
            
        msg_id = str(time.time())
        response_queue = Queue()
        
        with self.lock:
            self.response_queues[msg_id] = response_queue
            self.message_queue.put((msg_id, operation))
        
        try:
            # Wait for response with configurable timeout
            response = response_queue.get(timeout=30)  # Extended timeout for large models
            if response.get("status") == "error" and "model_size" in operation:
                # Retry once for model loading operations
                self.message_queue.put((msg_id, operation))
                response = response_queue.get(timeout=30)
        except Exception as e:
            response = {"status": "error", "message": f"Operation failed: {str(e)}"}
        finally:
            with self.lock:
                if msg_id in self.response_queues:
                    del self.response_queues[msg_id]
            
        return response

    def store_tensor(self, tensor_id: str, data: np.ndarray, model_size: Optional[int] = None) -> bool:
        try:
            if data is None:
                raise ValueError("Cannot store None tensor")
                
            # Calculate tensor metadata
            tensor_shape = data.shape
            tensor_dtype = str(data.dtype)
            tensor_size = data.nbytes
            
            operation = {
                'operation': 'vram',
                'type': 'write',
                'block_id': tensor_id,
                'data': data.tolist(),
                'model_size': model_size if model_size is not None else -1,  # -1 indicates unlimited
                'metadata': {
                    'shape': tensor_shape,
                    'dtype': tensor_dtype,
                    'size': tensor_size,
                    'timestamp': time.time()
                }
            }
            
            response = self._send_operation(operation)
            if response.get('status') == 'success':
                # Update tensor registry
                with self.lock:
                    self.tensor_registry[tensor_id] = {
                        'shape': tensor_shape,
                        'dtype': tensor_dtype,
                        'size': tensor_size,
                        'timestamp': time.time()
                    }
                    self.resource_monitor['vram_used'] += tensor_size
                    self.resource_monitor['active_tensors'] += 1
                return True
            else:
                print(f"Failed to store tensor {tensor_id}: {response.get('message', 'Unknown error')}")
                return False
        except Exception as e:
            print(f"Error storing tensor {tensor_id}: {str(e)}")
            return False

    def load_tensor(self, tensor_id: str) -> Optional[np.ndarray]:
        try:
            # Check tensor registry first
            if tensor_id not in self.tensor_registry:
                print(f"Tensor {tensor_id} not registered in VRAM")
                return None
                
            operation = {
                'operation': 'vram',
                'type': 'read',
                'block_id': tensor_id,
                'expected_metadata': self.tensor_registry.get(tensor_id, {})
            }
            
            response = self._send_operation(operation)
            if response.get('status') == 'success':
                data = response.get('data')
                if data is None:
                    print(f"No data found for tensor {tensor_id}")
                    return None
                    
                # Verify tensor metadata
                metadata = response.get('metadata', {})
                expected_metadata = self.tensor_registry.get(tensor_id, {})
                if metadata.get('shape') != expected_metadata.get('shape'):
                    print(f"Warning: Tensor {tensor_id} shape mismatch")
                    
                try:
                    # Convert to numpy array with correct dtype
                    arr = np.array(data, dtype=np.dtype(expected_metadata.get('dtype', 'float32')))
                    if arr.shape != expected_metadata.get('shape'):
                        arr = arr.reshape(expected_metadata.get('shape'))
                    return arr
                except Exception as e:
                    print(f"Error converting tensor data: {str(e)}")
                    return None
            else:
                print(f"Failed to load tensor {tensor_id}: {response.get('message', 'Unknown error')}")
                return None
        except Exception as e:
            print(f"Error loading tensor {tensor_id}: {str(e)}")
            return None

    def store_state(self, component: str, state_id: str, state_data: Dict[str, Any]) -> bool:
        try:
            operation = {
                'operation': 'state',
                'type': 'save',
                'component': component,
                'state_id': state_id,
                'data': state_data,
                'timestamp': time.time()
            }
            
            response = self._send_operation(operation)
            if response.get('status') != 'success':
                print(f"Failed to store state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
                return False
            return True
        except Exception as e:
            print(f"Error storing state for {component}/{state_id}: {str(e)}")
            return False

    def load_state(self, component: str, state_id: str) -> Optional[Dict[str, Any]]:
        try:
            operation = {
                'operation': 'state',
                'type': 'load',
                'component': component,
                'state_id': state_id
            }
            
            response = self._send_operation(operation)
            if response.get('status') == 'success':
                data = response.get('data')
                if data is None:
                    print(f"No state found for {component}/{state_id}")
                    return None
                return data
            else:
                print(f"Failed to load state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
                return None
        except Exception as e:
            print(f"Error loading state for {component}/{state_id}: {str(e)}")
            return None

    def is_model_loaded(self, model_name: str) -> bool:
        """Check if a model is already loaded in VRAM"""
        return model_name in self.resource_monitor['loaded_models']

    def load_model(self, model_name: str, model_path: Optional[str] = None, model_data: Optional[Dict] = None) -> bool:
        """Load a model into VRAM if not already loaded"""
        try:
            # Check if model is already loaded
            if self.is_model_loaded(model_name):
                print(f"Model {model_name} already loaded in VRAM")
                return True

            # Calculate model hash if path provided
            model_hash = None
            if model_path:
                model_hash = self._calculate_model_hash(model_path)

            operation = {
                'operation': 'model',
                'type': 'load',
                'model_name': model_name,
                'model_hash': model_hash,
                'model_data': model_data
            }

            response = self._send_operation(operation)
            if response.get('status') == 'success':
                with self.lock:
                    self.model_registry[model_name] = {
                        'hash': model_hash,
                        'timestamp': time.time(),
                        'tensors': response.get('tensor_ids', [])
                    }
                    self.resource_monitor['loaded_models'].add(model_name)
                print(f"Successfully loaded model {model_name}")
                return True
            else:
                print(f"Failed to load model {model_name}: {response.get('message', 'Unknown error')}")
                return False
        except Exception as e:
            print(f"Error loading model {model_name}: {str(e)}")
            return False

    def _calculate_model_hash(self, model_path: str) -> str:
        """Calculate SHA256 hash of model file"""
        try:
            sha256_hash = hashlib.sha256()
            with open(model_path, "rb") as f:
                for byte_block in iter(lambda: f.read(4096), b""):
                    sha256_hash.update(byte_block)
            return sha256_hash.hexdigest()
        except Exception as e:
            print(f"Error calculating model hash: {str(e)}")
            return ""

    def cache_data(self, key: str, data: Any) -> bool:
        operation = {
            'operation': 'cache',
            'type': 'set',
            'key': key,
            'data': data
        }
        
        response = self._send_operation(operation)
        return response.get('status') == 'success'

    def get_cached_data(self, key: str) -> Optional[Any]:
        operation = {
            'operation': 'cache',
            'type': 'get',
            'key': key
        }
        
        response = self._send_operation(operation)
        if response.get('status') == 'success':
            return response['data']
        return None

    def wait_for_connection(self, timeout: float = 30.0) -> bool:
        """Wait for WebSocket connection to be established"""
        start_time = time.time()
        while not self._closing and not self.connected:
            if time.time() - start_time > timeout:
                print("Connection timeout exceeded")
                return False
            time.sleep(0.1)
        return self.connected

    def is_connected(self) -> bool:
        """Check if WebSocket connection is active"""
        return self.connected and not self._closing
        
    def get_connection_status(self) -> Dict[str, Any]:
        """Get detailed connection status"""
        return {
            "connected": self.connected,
            "closing": self._closing,
            "error_count": self.error_count,
            "url": self.url,
            "last_error_time": self.last_error_time,
            "loaded_models": list(self.resource_monitor['loaded_models'])
        }
        
    def start_inference(self, model_name: str, input_data: np.ndarray) -> Optional[Dict[str, Any]]:
        """Start inference with a loaded model"""
        try:
            if not self.is_model_loaded(model_name):
                print(f"Model {model_name} not loaded. Please load the model first.")
                return None
                
            operation = {
                'operation': 'inference',
                'type': 'run',
                'model_name': model_name,
                'input_data': input_data.tolist() if isinstance(input_data, np.ndarray) else input_data
            }
            
            response = self._send_operation(operation)
            if response.get('status') == 'success':
                return {
                    'output': np.array(response['output']) if 'output' in response else None,
                    'metrics': response.get('metrics', {}),
                    'model_info': self.model_registry.get(model_name, {})
                }
            else:
                print(f"Inference failed: {response.get('message', 'Unknown error')}")
                return None
        except Exception as e:
            print(f"Error during inference: {str(e)}")
            return None

    def close(self):
        """Close WebSocket connection and cleanup resources."""
        if not self._closing:
            self._closing = True
            if self.websocket and self._loop:
                async def cleanup():
                    try:
                        # Clean up registries
                        with self.lock:
                            self.tensor_registry.clear()
                            self.model_registry.clear()
                            self.resource_monitor['vram_used'] = 0
                            self.resource_monitor['active_tensors'] = 0
                            self.resource_monitor['loaded_models'].clear()
                        
                        # Notify server about cleanup
                        if self.connected:
                            try:
                                await self.websocket.send(json.dumps({
                                    'operation': 'cleanup',
                                    'type': 'full'
                                }))
                            except:
                                pass
                                
                        await self.websocket.close()
                    except Exception as e:
                        print(f"Error during cleanup: {str(e)}")
                    finally:
                        self.connected = False
                        
                if self._loop.is_running():
                    self._loop.create_task(cleanup())
                else:
                    asyncio.run(cleanup())

    async def aclose(self):
        """Asynchronously close WebSocket connection."""
        if not self._closing:
            self._closing = True
            if self.websocket:
                try:
                    await self.websocket.close()
                except:
                    pass
                finally:
                    self.connected = False
                    
    def __del__(self):
        """Ensure cleanup on deletion."""
        self.close()