Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Upload 37 files
Browse files- ai.py +803 -0
- ai_http.py +477 -0
- core.py +54 -0
- custom_vram.py +69 -0
- electron_speed.py +68 -0
- flip_flops.py +91 -0
- gpu_arch.py +351 -0
- gpu_chip.py +116 -0
- gpu_state_db.py +60 -0
- http_storage.py +526 -0
- logic_gates.py +357 -0
- multi_gpu_system.py +154 -0
- multi_gpu_system_http.py +164 -0
- multicore.py +38 -0
- streaming_multiprocessor.py +112 -0
- tensor_core.py +363 -0
- test_ai_integration.py +489 -0
- test_ai_integration_http.py +488 -0
- test_multi_chip_gpu.py +102 -0
- virtual_vram.py +124 -0
- vram/__pycache__/ram_controller.cpython-311.pyc +0 -0
- vram/__pycache__/ram_controller.cpython-312.pyc +0 -0
- vram/__pycache__/ram_controller.cpython-313.pyc +0 -0
- vram/dram_cache.py +36 -0
- vram/electron_speed.py +113 -0
- vram/ftl.py +19 -0
- vram/interface.py +17 -0
- vram/main.py +39 -0
- vram/nand_block.py +11 -0
- vram/nand_cell.py +35 -0
- vram/nand_memory.py +28 -0
- vram/nand_page.py +23 -0
- vram/nand_plane.py +5 -0
- vram/nvme.py +54 -0
- vram/ram_controller.py +51 -0
- vram_server.py +0 -0
- websocket_storage.py +455 -0
ai.py
ADDED
|
@@ -0,0 +1,803 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import time
|
| 3 |
+
from typing import Dict, Any, Optional, Tuple, Union, List
|
| 4 |
+
from enum import Enum
|
| 5 |
+
from tensor_core import TensorCoreArray
|
| 6 |
+
|
| 7 |
+
class VectorOperation(Enum):
|
| 8 |
+
"""Enumeration of supported vector operations."""
|
| 9 |
+
ADD = "add"
|
| 10 |
+
SUBTRACT = "subtract"
|
| 11 |
+
MULTIPLY = "multiply"
|
| 12 |
+
DIVIDE = "divide"
|
| 13 |
+
DOT_PRODUCT = "dot_product"
|
| 14 |
+
CROSS_PRODUCT = "cross_product"
|
| 15 |
+
NORMALIZE = "normalize"
|
| 16 |
+
MAGNITUDE = "magnitude"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class AIAccelerator:
|
| 20 |
+
"""
|
| 21 |
+
AI Accelerator that simulates GPU-based AI computations.
|
| 22 |
+
|
| 23 |
+
This class leverages NumPy's optimized operations to simulate the parallel
|
| 24 |
+
processing capabilities of the vGPU for AI workloads.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def __init__(self, vram=None, num_sms: int = 800, cores_per_sm: int = 222, storage=None):
|
| 28 |
+
"""Initialize AI Accelerator with electron-speed awareness and shared WebSocket storage."""
|
| 29 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity
|
| 30 |
+
|
| 31 |
+
self.storage = storage # Use the shared storage instance
|
| 32 |
+
if self.storage is None:
|
| 33 |
+
from websocket_storage import WebSocketGPUStorage
|
| 34 |
+
self.storage = WebSocketGPUStorage() # Only create new if not provided
|
| 35 |
+
if not self.storage.wait_for_connection():
|
| 36 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 37 |
+
|
| 38 |
+
self.vram = vram
|
| 39 |
+
self.num_sms = num_sms
|
| 40 |
+
self.cores_per_sm = cores_per_sm
|
| 41 |
+
self.total_cores = num_sms * cores_per_sm
|
| 42 |
+
|
| 43 |
+
# Configure for maximum parallel processing at electron speed
|
| 44 |
+
total_tensor_cores = num_sms * cores_per_sm # Use ALL cores for tensor operations
|
| 45 |
+
self.tensor_core_array = TensorCoreArray(
|
| 46 |
+
num_tensor_cores=total_tensor_cores,
|
| 47 |
+
bits=32,
|
| 48 |
+
bandwidth_tbps=drift_velocity / 1e-12 # Bandwidth scaled to electron drift speed
|
| 49 |
+
)
|
| 50 |
+
self.tensor_cores_initialized = False
|
| 51 |
+
|
| 52 |
+
# Initialize model, tensor, and tokenizer tracking
|
| 53 |
+
self.model_registry: Dict[str, Dict[str, Any]] = {} # Track loaded models
|
| 54 |
+
self.tensor_registry: Dict[str, Dict[str, Any]] = {} # Track tensor metadata
|
| 55 |
+
self.tokenizer_registry: Dict[str, Any] = {} # Track tokenizers
|
| 56 |
+
self.resource_monitor = {
|
| 57 |
+
'vram_used': 0,
|
| 58 |
+
'active_tensors': 0,
|
| 59 |
+
'loaded_models': set()
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
def _serialize_model_config(self, config: Any) -> dict:
|
| 63 |
+
"""Convert model config to a serializable format."""
|
| 64 |
+
# Handle None case first
|
| 65 |
+
if config is None:
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
# Handle Florence2LanguageConfig specifically
|
| 69 |
+
if config.__class__.__name__ == "Florence2LanguageConfig":
|
| 70 |
+
try:
|
| 71 |
+
return {
|
| 72 |
+
"type": "Florence2LanguageConfig",
|
| 73 |
+
"model_type": getattr(config, "model_type", ""),
|
| 74 |
+
"architectures": getattr(config, "architectures", []),
|
| 75 |
+
"hidden_size": getattr(config, "hidden_size", 0),
|
| 76 |
+
"num_attention_heads": getattr(config, "num_attention_heads", 0),
|
| 77 |
+
"num_hidden_layers": getattr(config, "num_hidden_layers", 0),
|
| 78 |
+
"intermediate_size": getattr(config, "intermediate_size", 0),
|
| 79 |
+
"max_position_embeddings": getattr(config, "max_position_embeddings", 0),
|
| 80 |
+
"layer_norm_eps": getattr(config, "layer_norm_eps", 1e-12),
|
| 81 |
+
"vocab_size": getattr(config, "vocab_size", 0)
|
| 82 |
+
}
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Warning: Error serializing Florence2LanguageConfig: {e}")
|
| 85 |
+
return {"type": "Florence2LanguageConfig", "error": str(e)}
|
| 86 |
+
|
| 87 |
+
# Handle standard types
|
| 88 |
+
if isinstance(config, (int, float, str, bool)):
|
| 89 |
+
return config
|
| 90 |
+
|
| 91 |
+
# Handle lists and tuples
|
| 92 |
+
if isinstance(config, (list, tuple)):
|
| 93 |
+
return [self._serialize_model_config(item) for item in config]
|
| 94 |
+
|
| 95 |
+
# Handle dictionaries
|
| 96 |
+
if isinstance(config, dict):
|
| 97 |
+
return {k: self._serialize_model_config(v) for k, v in config.items()}
|
| 98 |
+
|
| 99 |
+
# Handle objects with __dict__
|
| 100 |
+
if hasattr(config, '__dict__'):
|
| 101 |
+
config_dict = {}
|
| 102 |
+
for key, value in config.__dict__.items():
|
| 103 |
+
try:
|
| 104 |
+
# Skip private attributes
|
| 105 |
+
if key.startswith('_'):
|
| 106 |
+
continue
|
| 107 |
+
config_dict[key] = self._serialize_model_config(value)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"Warning: Error serializing attribute {key}: {e}")
|
| 110 |
+
config_dict[key] = str(value)
|
| 111 |
+
return config_dict
|
| 112 |
+
|
| 113 |
+
# Fallback: convert to string representation
|
| 114 |
+
try:
|
| 115 |
+
return str(config)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return f"<Unserializable object of type {type(config).__name__}: {str(e)}>"
|
| 118 |
+
|
| 119 |
+
def store_model_state(self, model_name: str, model_info: Dict[str, Any]) -> bool:
|
| 120 |
+
"""Store model state in WebSocket storage with proper serialization."""
|
| 121 |
+
try:
|
| 122 |
+
# Convert any non-serializable parts of model_info
|
| 123 |
+
serializable_info = self._serialize_model_config(model_info)
|
| 124 |
+
|
| 125 |
+
# Store in model registry
|
| 126 |
+
self.model_registry[model_name] = serializable_info
|
| 127 |
+
|
| 128 |
+
# Save to storage
|
| 129 |
+
if self.storage:
|
| 130 |
+
# Store model info
|
| 131 |
+
info_success = self.storage.store_state(
|
| 132 |
+
"models",
|
| 133 |
+
f"{model_name}/info",
|
| 134 |
+
serializable_info
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Store model state
|
| 138 |
+
state_success = self.storage.store_state(
|
| 139 |
+
"models",
|
| 140 |
+
f"{model_name}/state",
|
| 141 |
+
{"loaded": True, "timestamp": time.time()}
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if info_success and state_success:
|
| 145 |
+
self.resource_monitor['loaded_models'].add(model_name)
|
| 146 |
+
return True
|
| 147 |
+
|
| 148 |
+
return False
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"Error storing model state: {str(e)}")
|
| 151 |
+
return False
|
| 152 |
+
|
| 153 |
+
def initialize_tensor_cores(self):
|
| 154 |
+
"""Initialize tensor cores and verify they're ready for computation"""
|
| 155 |
+
if self.tensor_cores_initialized:
|
| 156 |
+
return True
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
# Verify tensor core array is properly initialized
|
| 160 |
+
if not hasattr(self, 'tensor_core_array') or self.tensor_core_array is None:
|
| 161 |
+
raise RuntimeError("Tensor core array not properly initialized")
|
| 162 |
+
|
| 163 |
+
# Initialize tensor cores if needed
|
| 164 |
+
if hasattr(self.tensor_core_array, 'initialize'):
|
| 165 |
+
self.tensor_core_array.initialize()
|
| 166 |
+
|
| 167 |
+
# Verify VRAM access
|
| 168 |
+
if self.vram is None:
|
| 169 |
+
raise RuntimeError("VRAM not properly configured")
|
| 170 |
+
|
| 171 |
+
# Test tensor core functionality with a small computation
|
| 172 |
+
test_input = [[1.0, 2.0], [3.0, 4.0]]
|
| 173 |
+
# Convert input to numpy array if needed
|
| 174 |
+
if isinstance(test_input, list):
|
| 175 |
+
test_input = np.array(test_input, dtype=np.float32)
|
| 176 |
+
|
| 177 |
+
test_result = self.tensor_core_array.matmul(test_input, test_input)
|
| 178 |
+
if test_result is None or not isinstance(test_result, (np.ndarray, list)) or len(test_result) == 0:
|
| 179 |
+
raise RuntimeError("Tensor core test computation failed")
|
| 180 |
+
|
| 181 |
+
self.tensor_cores_initialized = True
|
| 182 |
+
return True
|
| 183 |
+
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"Failed to initialize tensor cores: {str(e)}")
|
| 186 |
+
self.tensor_cores_initialized = False
|
| 187 |
+
return False
|
| 188 |
+
|
| 189 |
+
# AI operation statistics
|
| 190 |
+
self.operations_performed = 0
|
| 191 |
+
self.total_compute_time = 0.0
|
| 192 |
+
self.flops_performed = 0
|
| 193 |
+
|
| 194 |
+
# WebSocket-based memory management
|
| 195 |
+
self.model_registry = {} # Track loaded models
|
| 196 |
+
self.matrix_registry = {} # Track loaded matrices
|
| 197 |
+
self.matrix_counter = 0
|
| 198 |
+
self.activation_cache: Dict[str, str] = {} # Cache activation outputs
|
| 199 |
+
self.weight_cache: Dict[str, Any] = {} # Cache preprocessed weights
|
| 200 |
+
|
| 201 |
+
# Model registries
|
| 202 |
+
self.model_registry: Dict[str, Any] = {}
|
| 203 |
+
self.tokenizer_registry: Dict[str, Any] = {}
|
| 204 |
+
self.model_configs: Dict[str, Any] = {} # Store model architectures
|
| 205 |
+
self.model_loaded = False
|
| 206 |
+
|
| 207 |
+
# Batch processing configuration
|
| 208 |
+
self.max_batch_size = 64
|
| 209 |
+
self.min_batch_size = 4
|
| 210 |
+
self.dynamic_batching = True # Enable automatic batch size adjustment
|
| 211 |
+
|
| 212 |
+
def set_vram(self, vram):
|
| 213 |
+
"""Set the VRAM reference."""
|
| 214 |
+
self.vram = vram
|
| 215 |
+
|
| 216 |
+
def allocate_matrix(self, shape: Tuple[int, ...], dtype=np.float32,
|
| 217 |
+
name: Optional[str] = None) -> str:
|
| 218 |
+
"""Allocate a matrix in VRAM and return its ID."""
|
| 219 |
+
if not self.vram:
|
| 220 |
+
raise RuntimeError("VRAM not available")
|
| 221 |
+
|
| 222 |
+
if name is None:
|
| 223 |
+
name = f"matrix_{self.matrix_counter}"
|
| 224 |
+
self.matrix_counter += 1
|
| 225 |
+
|
| 226 |
+
# Create matrix data
|
| 227 |
+
matrix_data = np.zeros(shape, dtype=dtype)
|
| 228 |
+
|
| 229 |
+
# Store in VRAM as a texture (reusing texture storage mechanism)
|
| 230 |
+
matrix_id = self.vram.load_texture(matrix_data, name)
|
| 231 |
+
self.matrix_registry[name] = matrix_id
|
| 232 |
+
|
| 233 |
+
return name
|
| 234 |
+
|
| 235 |
+
def load_matrix(self, matrix_data: np.ndarray, name: Optional[str] = None) -> str:
|
| 236 |
+
"""Load matrix data into VRAM and return its ID."""
|
| 237 |
+
if not self.vram:
|
| 238 |
+
raise RuntimeError("VRAM not available")
|
| 239 |
+
|
| 240 |
+
if name is None:
|
| 241 |
+
name = f"matrix_{self.matrix_counter}"
|
| 242 |
+
self.matrix_counter += 1
|
| 243 |
+
|
| 244 |
+
# Store in VRAM
|
| 245 |
+
matrix_id = self.vram.load_texture(matrix_data, name)
|
| 246 |
+
self.matrix_registry[name] = matrix_id
|
| 247 |
+
|
| 248 |
+
return name
|
| 249 |
+
|
| 250 |
+
def get_matrix(self, matrix_id: str) -> Optional[np.ndarray]:
|
| 251 |
+
"""Retrieve matrix data from VRAM."""
|
| 252 |
+
if not self.vram or matrix_id not in self.matrix_registry:
|
| 253 |
+
return None
|
| 254 |
+
|
| 255 |
+
vram_id = self.matrix_registry[matrix_id]
|
| 256 |
+
return self.vram.get_texture(vram_id)
|
| 257 |
+
|
| 258 |
+
def matrix_multiply(self, matrix_a_id: str, matrix_b_id: str,
|
| 259 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 260 |
+
"""Perform matrix multiplication using simulated GPU parallelism."""
|
| 261 |
+
start_time = time.time()
|
| 262 |
+
|
| 263 |
+
# Retrieve matrices from VRAM
|
| 264 |
+
matrix_a = self.get_matrix(matrix_a_id)
|
| 265 |
+
matrix_b = self.get_matrix(matrix_b_id)
|
| 266 |
+
|
| 267 |
+
if matrix_a is None or matrix_b is None:
|
| 268 |
+
print(f"Error: Could not retrieve matrices {matrix_a_id} or {matrix_b_id}")
|
| 269 |
+
return None
|
| 270 |
+
|
| 271 |
+
try:
|
| 272 |
+
# Check if matrices can be multiplied
|
| 273 |
+
if matrix_a.shape[-1] != matrix_b.shape[0]:
|
| 274 |
+
print(f"Error: Matrix dimensions incompatible for multiplication: "
|
| 275 |
+
f"{matrix_a.shape} x {matrix_b.shape}")
|
| 276 |
+
return None
|
| 277 |
+
|
| 278 |
+
# Simulate parallel processing by breaking down the operation
|
| 279 |
+
# In a real GPU, this would be distributed across SMs and cores
|
| 280 |
+
def _simulate_parallel_matmul(self, matrix_a: np.ndarray, matrix_b: np.ndarray) -> np.ndarray:
|
| 281 |
+
"""Route matrix multiplication through the virtual TensorCoreArray."""
|
| 282 |
+
A = matrix_a.tolist()
|
| 283 |
+
B = matrix_b.tolist()
|
| 284 |
+
result = self.tensor_core_array.matmul(A, B)
|
| 285 |
+
return np.array(result)
|
| 286 |
+
|
| 287 |
+
# Store result in VRAM
|
| 288 |
+
if result_id is None:
|
| 289 |
+
result_id = f"result_{self.matrix_counter}"
|
| 290 |
+
self.matrix_counter += 1
|
| 291 |
+
|
| 292 |
+
result_matrix_id = self.load_matrix(result, result_id)
|
| 293 |
+
|
| 294 |
+
# Update statistics
|
| 295 |
+
compute_time = time.time() - start_time
|
| 296 |
+
self.total_compute_time += compute_time
|
| 297 |
+
self.operations_performed += 1
|
| 298 |
+
|
| 299 |
+
# Calculate FLOPs (2 * M * N * K for matrix multiplication)
|
| 300 |
+
m, k = matrix_a.shape
|
| 301 |
+
k2, n = matrix_b.shape
|
| 302 |
+
flops = 2 * m * n * k
|
| 303 |
+
self.flops_performed += flops
|
| 304 |
+
|
| 305 |
+
print(f"Matrix multiplication completed: {matrix_a.shape} x {matrix_b.shape} "
|
| 306 |
+
f"= {result.shape} in {compute_time:.4f}s")
|
| 307 |
+
print(f"Simulated {flops:,} FLOPs across {self.total_cores} cores")
|
| 308 |
+
|
| 309 |
+
return result_matrix_id
|
| 310 |
+
|
| 311 |
+
except Exception as e:
|
| 312 |
+
print(f"Error in matrix multiplication: {e}")
|
| 313 |
+
return None
|
| 314 |
+
|
| 315 |
+
def _simulate_parallel_matmul(self, matrix_a: np.ndarray, matrix_b: np.ndarray) -> np.ndarray:
|
| 316 |
+
"""Simulate parallel matrix multiplication across SMs."""
|
| 317 |
+
# Use NumPy's optimized matrix multiplication
|
| 318 |
+
# In a real implementation, this would be broken down into blocks
|
| 319 |
+
# and distributed across the simulated SMs
|
| 320 |
+
|
| 321 |
+
# For demonstration, we can show how the work would be distributed
|
| 322 |
+
m, k = matrix_a.shape
|
| 323 |
+
k2, n = matrix_b.shape
|
| 324 |
+
|
| 325 |
+
# Calculate work distribution
|
| 326 |
+
total_output_elements = m * n
|
| 327 |
+
elements_per_sm = max(1, total_output_elements // self.num_sms)
|
| 328 |
+
|
| 329 |
+
print(f"Distributing {total_output_elements:,} output elements across "
|
| 330 |
+
f"{self.num_sms} SMs ({elements_per_sm} elements per SM)")
|
| 331 |
+
|
| 332 |
+
# Perform the actual computation using NumPy
|
| 333 |
+
result = np.dot(matrix_a, matrix_b)
|
| 334 |
+
|
| 335 |
+
return result
|
| 336 |
+
|
| 337 |
+
def vector_operation(self, operation: VectorOperation, vector_a_id: str,
|
| 338 |
+
vector_b_id: Optional[str] = None,
|
| 339 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 340 |
+
"""Perform vector operations using simulated GPU parallelism."""
|
| 341 |
+
start_time = time.time()
|
| 342 |
+
|
| 343 |
+
# Retrieve vectors from VRAM
|
| 344 |
+
vector_a = self.get_matrix(vector_a_id)
|
| 345 |
+
if vector_a is None:
|
| 346 |
+
print(f"Error: Could not retrieve vector {vector_a_id}")
|
| 347 |
+
return None
|
| 348 |
+
|
| 349 |
+
vector_b = None
|
| 350 |
+
if vector_b_id:
|
| 351 |
+
vector_b = self.get_matrix(vector_b_id)
|
| 352 |
+
if vector_b is None:
|
| 353 |
+
print(f"Error: Could not retrieve vector {vector_b_id}")
|
| 354 |
+
return None
|
| 355 |
+
|
| 356 |
+
try:
|
| 357 |
+
result = None
|
| 358 |
+
flops = 0
|
| 359 |
+
|
| 360 |
+
if operation == VectorOperation.ADD:
|
| 361 |
+
if vector_b is None:
|
| 362 |
+
raise ValueError("Vector B required for addition")
|
| 363 |
+
result = vector_a + vector_b
|
| 364 |
+
flops = vector_a.size
|
| 365 |
+
|
| 366 |
+
elif operation == VectorOperation.SUBTRACT:
|
| 367 |
+
if vector_b is None:
|
| 368 |
+
raise ValueError("Vector B required for subtraction")
|
| 369 |
+
result = vector_a - vector_b
|
| 370 |
+
flops = vector_a.size
|
| 371 |
+
|
| 372 |
+
elif operation == VectorOperation.MULTIPLY:
|
| 373 |
+
if vector_b is None:
|
| 374 |
+
raise ValueError("Vector B required for multiplication")
|
| 375 |
+
result = vector_a * vector_b
|
| 376 |
+
flops = vector_a.size
|
| 377 |
+
|
| 378 |
+
elif operation == VectorOperation.DIVIDE:
|
| 379 |
+
if vector_b is None:
|
| 380 |
+
raise ValueError("Vector B required for division")
|
| 381 |
+
result = vector_a / vector_b
|
| 382 |
+
flops = vector_a.size
|
| 383 |
+
|
| 384 |
+
elif operation == VectorOperation.DOT_PRODUCT:
|
| 385 |
+
if vector_b is None:
|
| 386 |
+
raise ValueError("Vector B required for dot product")
|
| 387 |
+
result = np.dot(vector_a.flatten(), vector_b.flatten())
|
| 388 |
+
flops = 2 * vector_a.size
|
| 389 |
+
|
| 390 |
+
elif operation == VectorOperation.CROSS_PRODUCT:
|
| 391 |
+
if vector_b is None:
|
| 392 |
+
raise ValueError("Vector B required for cross product")
|
| 393 |
+
result = np.cross(vector_a, vector_b)
|
| 394 |
+
flops = 6 # Approximate for 3D cross product
|
| 395 |
+
|
| 396 |
+
elif operation == VectorOperation.NORMALIZE:
|
| 397 |
+
magnitude = np.linalg.norm(vector_a)
|
| 398 |
+
result = vector_a / magnitude if magnitude > 0 else vector_a
|
| 399 |
+
flops = vector_a.size * 2 # Division + magnitude calculation
|
| 400 |
+
|
| 401 |
+
elif operation == VectorOperation.MAGNITUDE:
|
| 402 |
+
result = np.array([np.linalg.norm(vector_a)])
|
| 403 |
+
flops = vector_a.size * 2 # Squares and sum
|
| 404 |
+
|
| 405 |
+
else:
|
| 406 |
+
raise ValueError(f"Unsupported vector operation: {operation}")
|
| 407 |
+
|
| 408 |
+
# Store result in VRAM
|
| 409 |
+
if result_id is None:
|
| 410 |
+
result_id = f"vector_result_{self.matrix_counter}"
|
| 411 |
+
self.matrix_counter += 1
|
| 412 |
+
|
| 413 |
+
result_vector_id = self.load_matrix(result, result_id)
|
| 414 |
+
|
| 415 |
+
# Update statistics
|
| 416 |
+
compute_time = time.time() - start_time
|
| 417 |
+
self.total_compute_time += compute_time
|
| 418 |
+
self.operations_performed += 1
|
| 419 |
+
self.flops_performed += flops
|
| 420 |
+
|
| 421 |
+
print(f"Vector operation {operation.value} completed in {compute_time:.4f}s")
|
| 422 |
+
|
| 423 |
+
return result_vector_id
|
| 424 |
+
|
| 425 |
+
except Exception as e:
|
| 426 |
+
print(f"Error in vector operation {operation.value}: {e}")
|
| 427 |
+
return None
|
| 428 |
+
|
| 429 |
+
def convolution_2d(self, input_id: str, kernel_id: str,
|
| 430 |
+
stride: int = 1, padding: int = 0,
|
| 431 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 432 |
+
"""Perform 2D convolution operation."""
|
| 433 |
+
start_time = time.time()
|
| 434 |
+
|
| 435 |
+
# Retrieve input and kernel from VRAM
|
| 436 |
+
input_data = self.get_matrix(input_id)
|
| 437 |
+
kernel = self.get_matrix(kernel_id)
|
| 438 |
+
|
| 439 |
+
if input_data is None or kernel is None:
|
| 440 |
+
print(f"Error: Could not retrieve input or kernel")
|
| 441 |
+
return None
|
| 442 |
+
|
| 443 |
+
try:
|
| 444 |
+
# Simple 2D convolution implementation
|
| 445 |
+
# In a real GPU implementation, this would be highly optimized
|
| 446 |
+
# and distributed across many cores
|
| 447 |
+
|
| 448 |
+
if len(input_data.shape) == 2:
|
| 449 |
+
input_h, input_w = input_data.shape
|
| 450 |
+
channels = 1
|
| 451 |
+
else:
|
| 452 |
+
input_h, input_w, channels = input_data.shape
|
| 453 |
+
|
| 454 |
+
kernel_h, kernel_w = kernel.shape[:2]
|
| 455 |
+
|
| 456 |
+
# Calculate output dimensions
|
| 457 |
+
output_h = (input_h + 2 * padding - kernel_h) // stride + 1
|
| 458 |
+
output_w = (input_w + 2 * padding - kernel_w) // stride + 1
|
| 459 |
+
|
| 460 |
+
# Initialize output
|
| 461 |
+
if channels == 1:
|
| 462 |
+
output = np.zeros((output_h, output_w))
|
| 463 |
+
else:
|
| 464 |
+
output = np.zeros((output_h, output_w, channels))
|
| 465 |
+
|
| 466 |
+
# Pad input if necessary
|
| 467 |
+
if padding > 0:
|
| 468 |
+
if channels == 1:
|
| 469 |
+
padded_input = np.pad(input_data, padding, mode='constant')
|
| 470 |
+
else:
|
| 471 |
+
padded_input = np.pad(input_data,
|
| 472 |
+
((padding, padding), (padding, padding), (0, 0)),
|
| 473 |
+
mode='constant')
|
| 474 |
+
else:
|
| 475 |
+
padded_input = input_data
|
| 476 |
+
|
| 477 |
+
# Perform convolution
|
| 478 |
+
flops = 0
|
| 479 |
+
for y in range(0, output_h):
|
| 480 |
+
for x in range(0, output_w):
|
| 481 |
+
y_start = y * stride
|
| 482 |
+
x_start = x * stride
|
| 483 |
+
|
| 484 |
+
if channels == 1:
|
| 485 |
+
patch = padded_input[y_start:y_start+kernel_h, x_start:x_start+kernel_w]
|
| 486 |
+
output[y, x] = np.sum(patch * kernel)
|
| 487 |
+
flops += kernel_h * kernel_w * 2 # Multiply and add
|
| 488 |
+
else:
|
| 489 |
+
for c in range(channels):
|
| 490 |
+
patch = padded_input[y_start:y_start+kernel_h,
|
| 491 |
+
x_start:x_start+kernel_w, c]
|
| 492 |
+
output[y, x, c] = np.sum(patch * kernel)
|
| 493 |
+
flops += kernel_h * kernel_w * 2
|
| 494 |
+
|
| 495 |
+
# Store result in VRAM
|
| 496 |
+
if result_id is None:
|
| 497 |
+
result_id = f"conv_result_{self.matrix_counter}"
|
| 498 |
+
self.matrix_counter += 1
|
| 499 |
+
|
| 500 |
+
result_conv_id = self.load_matrix(output, result_id)
|
| 501 |
+
|
| 502 |
+
# Update statistics
|
| 503 |
+
compute_time = time.time() - start_time
|
| 504 |
+
self.total_compute_time += compute_time
|
| 505 |
+
self.operations_performed += 1
|
| 506 |
+
self.flops_performed += flops
|
| 507 |
+
|
| 508 |
+
print(f"2D Convolution completed: {input_data.shape} * {kernel.shape} "
|
| 509 |
+
f"= {output.shape} in {compute_time:.4f}s")
|
| 510 |
+
print(f"Simulated {flops:,} FLOPs")
|
| 511 |
+
|
| 512 |
+
return result_conv_id
|
| 513 |
+
|
| 514 |
+
except Exception as e:
|
| 515 |
+
print(f"Error in 2D convolution: {e}")
|
| 516 |
+
return None
|
| 517 |
+
|
| 518 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 519 |
+
"""Get AI accelerator statistics."""
|
| 520 |
+
avg_compute_time = self.total_compute_time / max(1, self.operations_performed)
|
| 521 |
+
flops_per_second = self.flops_performed / max(0.001, self.total_compute_time)
|
| 522 |
+
|
| 523 |
+
return {
|
| 524 |
+
"operations_performed": self.operations_performed,
|
| 525 |
+
"total_compute_time": self.total_compute_time,
|
| 526 |
+
"avg_compute_time": avg_compute_time,
|
| 527 |
+
"flops_performed": self.flops_performed,
|
| 528 |
+
"flops_per_second": flops_per_second,
|
| 529 |
+
"matrices_in_memory": len(self.matrix_registry),
|
| 530 |
+
"simulated_cores": self.total_cores,
|
| 531 |
+
"simulated_sms": self.num_sms
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
def reset_stats(self) -> None:
|
| 535 |
+
"""Reset AI accelerator statistics."""
|
| 536 |
+
self.operations_performed = 0
|
| 537 |
+
self.total_compute_time = 0.0
|
| 538 |
+
self.flops_performed = 0
|
| 539 |
+
|
| 540 |
+
def optimize_attention_weights(self, weight_matrix):
|
| 541 |
+
"""Preprocess attention weights for faster computation."""
|
| 542 |
+
# Optimize weight layout for tensor core operations
|
| 543 |
+
if isinstance(weight_matrix, np.ndarray):
|
| 544 |
+
# Reshape for optimal memory access
|
| 545 |
+
if len(weight_matrix.shape) == 2:
|
| 546 |
+
# Pad to multiple of tensor core size if needed
|
| 547 |
+
h, w = weight_matrix.shape
|
| 548 |
+
pad_h = (8 - h % 8) if h % 8 != 0 else 0
|
| 549 |
+
pad_w = (8 - w % 8) if w % 8 != 0 else 0
|
| 550 |
+
if pad_h > 0 or pad_w > 0:
|
| 551 |
+
weight_matrix = np.pad(weight_matrix, ((0, pad_h), (0, pad_w)))
|
| 552 |
+
return weight_matrix
|
| 553 |
+
return weight_matrix
|
| 554 |
+
|
| 555 |
+
def parallel_attention(self, query, key_value_weights, features_per_sm):
|
| 556 |
+
"""Execute multi-head attention using parallel tensor cores."""
|
| 557 |
+
# Split attention heads across SMs
|
| 558 |
+
num_heads = min(self.num_sms, 32) # Max 32 attention heads
|
| 559 |
+
head_dim = query.shape[-1] // num_heads
|
| 560 |
+
|
| 561 |
+
# Parallel processing of attention heads
|
| 562 |
+
attention_results = []
|
| 563 |
+
for i in range(0, num_heads):
|
| 564 |
+
start_idx = i * head_dim
|
| 565 |
+
end_idx = (i + 1) * head_dim
|
| 566 |
+
|
| 567 |
+
# Process attention head using tensor core
|
| 568 |
+
q_head = [row[start_idx:end_idx] for row in query]
|
| 569 |
+
k_head = [row[start_idx:end_idx] for row in key_value_weights]
|
| 570 |
+
|
| 571 |
+
# Compute attention scores using tensor core
|
| 572 |
+
attention_scores = self.tensor_core_array.matmul(
|
| 573 |
+
q_head, k_head,
|
| 574 |
+
split_size=features_per_sm
|
| 575 |
+
)
|
| 576 |
+
attention_results.append(attention_scores)
|
| 577 |
+
|
| 578 |
+
# Combine attention heads
|
| 579 |
+
return self.combine_attention_heads(attention_results)
|
| 580 |
+
|
| 581 |
+
def combine_attention_heads(self, attention_heads):
|
| 582 |
+
"""Combine attention heads efficiently using tensor cores."""
|
| 583 |
+
if not attention_heads:
|
| 584 |
+
return None
|
| 585 |
+
|
| 586 |
+
# Get dimensions
|
| 587 |
+
num_heads = len(attention_heads)
|
| 588 |
+
batch_size = len(attention_heads[0])
|
| 589 |
+
head_dim = len(attention_heads[0][0])
|
| 590 |
+
|
| 591 |
+
# Concatenate heads efficiently
|
| 592 |
+
combined = [[0.0] * (head_dim * num_heads) for _ in range(batch_size)]
|
| 593 |
+
for i in range(batch_size):
|
| 594 |
+
for h in range(num_heads):
|
| 595 |
+
for j in range(head_dim):
|
| 596 |
+
combined[i][h * head_dim + j] = attention_heads[h][i][j]
|
| 597 |
+
|
| 598 |
+
return combined
|
| 599 |
+
|
| 600 |
+
def calculate_tflops(self, model_info, batch_size, inference_time):
|
| 601 |
+
"""Calculate effective TFLOPS for the inference."""
|
| 602 |
+
total_params = sum(np.prod(self.get_matrix(w_id).shape) for w_id in model_info["weights"].values())
|
| 603 |
+
ops_per_param = 2 # Multiply-add
|
| 604 |
+
total_ops = total_params * batch_size * ops_per_param
|
| 605 |
+
return (total_ops / inference_time) / 1e12 # Convert to TFLOPS
|
| 606 |
+
|
| 607 |
+
def _serialize_tensor(self, tensor: Any) -> np.ndarray:
|
| 608 |
+
"""Convert a PyTorch tensor to numpy array safely."""
|
| 609 |
+
try:
|
| 610 |
+
if hasattr(tensor, 'detach'):
|
| 611 |
+
tensor = tensor.detach()
|
| 612 |
+
if hasattr(tensor, 'cpu'):
|
| 613 |
+
tensor = tensor.cpu()
|
| 614 |
+
if hasattr(tensor, 'numpy'):
|
| 615 |
+
return tensor.numpy()
|
| 616 |
+
return np.array(tensor)
|
| 617 |
+
except Exception as e:
|
| 618 |
+
print(f"Warning: Error converting tensor to numpy: {e}")
|
| 619 |
+
return None
|
| 620 |
+
|
| 621 |
+
def load_model(self, model_id: str, model: Any, processor: Any):
|
| 622 |
+
"""Loads a model directly into WebSocket storage without CPU intermediary."""
|
| 623 |
+
try:
|
| 624 |
+
if model is None and processor is None:
|
| 625 |
+
# Zero-copy mode
|
| 626 |
+
self.model_registry[model_id] = {
|
| 627 |
+
"zero_copy": True,
|
| 628 |
+
"websocket_mapped": True
|
| 629 |
+
}
|
| 630 |
+
self.tokenizer_registry[model_id] = None
|
| 631 |
+
self.model_loaded = True
|
| 632 |
+
return
|
| 633 |
+
|
| 634 |
+
# Verify WebSocket connection first
|
| 635 |
+
if not self.storage or not self.storage.wait_for_connection():
|
| 636 |
+
raise RuntimeError("WebSocket connection not available")
|
| 637 |
+
|
| 638 |
+
# 1. Store model configuration
|
| 639 |
+
try:
|
| 640 |
+
config_dict = (self._serialize_model_config(model.config)
|
| 641 |
+
if hasattr(model, "config") else {})
|
| 642 |
+
model_info = {
|
| 643 |
+
"architecture": model.__class__.__name__ if model else "Unknown",
|
| 644 |
+
"processor": processor.__class__.__name__ if processor else "Unknown",
|
| 645 |
+
"config": config_dict
|
| 646 |
+
}
|
| 647 |
+
except Exception as e:
|
| 648 |
+
print(f"Warning: Error serializing model config: {e}")
|
| 649 |
+
model_info = {
|
| 650 |
+
"architecture": str(type(model).__name__),
|
| 651 |
+
"error": str(e)
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
# Store model info with retry
|
| 655 |
+
for attempt in range(3):
|
| 656 |
+
try:
|
| 657 |
+
if self.storage.store_state(f"models/{model_id}/info", "info", model_info):
|
| 658 |
+
break
|
| 659 |
+
print(f"Retrying model info storage, attempt {attempt + 1}")
|
| 660 |
+
time.sleep(1)
|
| 661 |
+
except Exception as e:
|
| 662 |
+
if attempt == 2:
|
| 663 |
+
raise RuntimeError(f"Failed to store model info: {e}")
|
| 664 |
+
|
| 665 |
+
# 2. Store model weights
|
| 666 |
+
if hasattr(model, "state_dict"):
|
| 667 |
+
weight_registry = {}
|
| 668 |
+
for name, param in model.state_dict().items():
|
| 669 |
+
# Convert tensor to numpy and store in chunks if needed
|
| 670 |
+
tensor_data = self._serialize_tensor(param)
|
| 671 |
+
if tensor_data is not None:
|
| 672 |
+
tensor_id = f"{model_id}/weights/{name}"
|
| 673 |
+
if tensor_data.nbytes > 1024*1024*1024: # If larger than 1GB
|
| 674 |
+
# Store large tensors in chunks
|
| 675 |
+
chunks = np.array_split(tensor_data,
|
| 676 |
+
max(1, tensor_data.nbytes // (512*1024*1024)))
|
| 677 |
+
chunk_ids = []
|
| 678 |
+
for i, chunk in enumerate(chunks):
|
| 679 |
+
chunk_id = f"{tensor_id}/chunk_{i}"
|
| 680 |
+
if self.storage.store_tensor(chunk_id, chunk):
|
| 681 |
+
chunk_ids.append(chunk_id)
|
| 682 |
+
weight_registry[name] = {
|
| 683 |
+
"type": "chunked",
|
| 684 |
+
"chunks": chunk_ids,
|
| 685 |
+
"shape": tensor_data.shape,
|
| 686 |
+
"dtype": str(tensor_data.dtype)
|
| 687 |
+
}
|
| 688 |
+
else:
|
| 689 |
+
# Store small tensors directly
|
| 690 |
+
if self.storage.store_tensor(tensor_id, tensor_data):
|
| 691 |
+
weight_registry[name] = {
|
| 692 |
+
"type": "direct",
|
| 693 |
+
"tensor_id": tensor_id,
|
| 694 |
+
"shape": tensor_data.shape,
|
| 695 |
+
"dtype": str(tensor_data.dtype)
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
# Store weight registry
|
| 699 |
+
self.storage.store_state(f"models/{model_id}/weights", "registry", weight_registry)
|
| 700 |
+
self.model_registry[model_id] = {
|
| 701 |
+
"weight_registry": weight_registry,
|
| 702 |
+
"websocket_mapped": True
|
| 703 |
+
}
|
| 704 |
+
|
| 705 |
+
# Map weight tensors directly to WebSocket storage
|
| 706 |
+
if model is not None and hasattr(model, "state_dict"):
|
| 707 |
+
model_weights = {}
|
| 708 |
+
|
| 709 |
+
for name, param in model.state_dict().items():
|
| 710 |
+
tensor_id = f"{model_id}/weights/{name}"
|
| 711 |
+
|
| 712 |
+
# Store tensor directly in WebSocket storage
|
| 713 |
+
if not self.storage.store_tensor(tensor_id, param.detach().numpy()):
|
| 714 |
+
raise RuntimeError(f"Failed to store tensor {name}")
|
| 715 |
+
model_weights[name] = tensor_id
|
| 716 |
+
|
| 717 |
+
# Store only WebSocket references
|
| 718 |
+
self.model_registry[model_id] = {
|
| 719 |
+
"weights": model_weights,
|
| 720 |
+
"architecture_id": hash(str(type(model))),
|
| 721 |
+
"websocket_mapped": True
|
| 722 |
+
}
|
| 723 |
+
else:
|
| 724 |
+
# Store the entire model state in WebSocket storage
|
| 725 |
+
tensor_id = f"{model_id}/model_state"
|
| 726 |
+
if not self.storage.store_state(f"models/{model_id}/state", "state", model):
|
| 727 |
+
raise RuntimeError("Failed to store model state")
|
| 728 |
+
self.model_registry[model_id] = tensor_id
|
| 729 |
+
|
| 730 |
+
# Store tokenizer/processor
|
| 731 |
+
self.tokenizer_registry[model_id] = processor
|
| 732 |
+
self.model_loaded = True
|
| 733 |
+
print(f"Model '{model_id}' loaded into WebSocket storage")
|
| 734 |
+
except Exception as e:
|
| 735 |
+
print(f"Error loading model into WebSocket storage: {str(e)}")
|
| 736 |
+
raise
|
| 737 |
+
|
| 738 |
+
def has_model(self, model_id: str) -> bool:
|
| 739 |
+
"""Checks if a model is loaded in the accelerator's registry."""
|
| 740 |
+
return model_id in self.model_registry
|
| 741 |
+
|
| 742 |
+
def inference(self, model_id: str, input_data: np.ndarray, idx: Optional[int] = None) -> Optional[np.ndarray]:
|
| 743 |
+
"""Execute pure WebSocket-based inference with zero CPU usage."""
|
| 744 |
+
print(f"[DEBUG] Starting WebSocket-based inference for model_id={model_id}")
|
| 745 |
+
try:
|
| 746 |
+
if not self.has_model(model_id):
|
| 747 |
+
print(f"[ERROR] Model {model_id} not loaded in WebSocket storage.")
|
| 748 |
+
return None
|
| 749 |
+
|
| 750 |
+
model_info = self.model_registry[model_id]
|
| 751 |
+
processor = self.tokenizer_registry[model_id]
|
| 752 |
+
|
| 753 |
+
# Store input data in WebSocket storage
|
| 754 |
+
input_tensor_id = f"{model_id}/inputs/{idx if idx is not None else time.time_ns()}"
|
| 755 |
+
self.storage.store_tensor(input_tensor_id, input_data)
|
| 756 |
+
|
| 757 |
+
# Process input using tensor cores through WebSocket
|
| 758 |
+
processed_data = processor(input_data, return_tensors="np")
|
| 759 |
+
processed_tensor_id = f"{model_id}/processed/{idx if idx is not None else time.time_ns()}"
|
| 760 |
+
self.storage.store_tensor(processed_tensor_id, processed_data["input_ids"])
|
| 761 |
+
|
| 762 |
+
# Load weights from WebSocket storage and perform forward pass
|
| 763 |
+
if isinstance(model_info, dict) and "weights" in model_info:
|
| 764 |
+
# Initialize hidden states
|
| 765 |
+
hidden_states = processed_data["input_ids"]
|
| 766 |
+
|
| 767 |
+
# Process through each layer using tensor cores
|
| 768 |
+
for layer_name, weight_id in model_info["weights"].items():
|
| 769 |
+
if "weight" in layer_name:
|
| 770 |
+
# Load weights from WebSocket storage
|
| 771 |
+
weights = self.storage.load_tensor(weight_id)
|
| 772 |
+
if weights is None:
|
| 773 |
+
continue
|
| 774 |
+
|
| 775 |
+
# Process through tensor cores
|
| 776 |
+
if "attention" in layer_name:
|
| 777 |
+
hidden_states = self.parallel_attention(
|
| 778 |
+
hidden_states,
|
| 779 |
+
weights,
|
| 780 |
+
features_per_sm=hidden_states.shape[-1] // self.num_sms
|
| 781 |
+
)
|
| 782 |
+
else:
|
| 783 |
+
# Regular layer processing
|
| 784 |
+
hidden_states = self.tensor_core_array.matmul(
|
| 785 |
+
hidden_states.tolist(),
|
| 786 |
+
weights.tolist()
|
| 787 |
+
)
|
| 788 |
+
|
| 789 |
+
# Store final output in WebSocket storage
|
| 790 |
+
output_tensor_id = f"{model_id}/outputs/{idx if idx is not None else time.time_ns()}"
|
| 791 |
+
output = np.array(hidden_states)
|
| 792 |
+
self.storage.store_tensor(output_tensor_id, output)
|
| 793 |
+
|
| 794 |
+
return output
|
| 795 |
+
else:
|
| 796 |
+
print(f"[ERROR] Unsupported model format in WebSocket storage")
|
| 797 |
+
return None
|
| 798 |
+
|
| 799 |
+
except Exception as e:
|
| 800 |
+
print(f"[ERROR] WebSocket-based inference failed for idx={idx}: {e}")
|
| 801 |
+
return None
|
| 802 |
+
|
| 803 |
+
|
ai_http.py
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import time
|
| 3 |
+
from typing import Dict, Any, Optional, Tuple, Union, List
|
| 4 |
+
from enum import Enum
|
| 5 |
+
from tensor_core import TensorCoreArray
|
| 6 |
+
|
| 7 |
+
class VectorOperation(Enum):
|
| 8 |
+
"""Enumeration of supported vector operations."""
|
| 9 |
+
ADD = "add"
|
| 10 |
+
SUBTRACT = "subtract"
|
| 11 |
+
MULTIPLY = "multiply"
|
| 12 |
+
DIVIDE = "divide"
|
| 13 |
+
DOT_PRODUCT = "dot_product"
|
| 14 |
+
CROSS_PRODUCT = "cross_product"
|
| 15 |
+
NORMALIZE = "normalize"
|
| 16 |
+
MAGNITUDE = "magnitude"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class AIAccelerator:
|
| 20 |
+
"""
|
| 21 |
+
AI Accelerator that simulates GPU-based AI computations using HTTP storage.
|
| 22 |
+
|
| 23 |
+
This class leverages NumPy's optimized operations to simulate the parallel
|
| 24 |
+
processing capabilities of the vGPU for AI workloads.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def __init__(self, vram=None, num_sms: int = 800, cores_per_sm: int = 222, storage=None):
|
| 28 |
+
"""Initialize AI Accelerator with electron-speed awareness and shared HTTP storage."""
|
| 29 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity
|
| 30 |
+
|
| 31 |
+
self.storage = storage # Use the shared storage instance
|
| 32 |
+
if self.storage is None:
|
| 33 |
+
from http_storage import HTTPGPUStorage
|
| 34 |
+
self.storage = HTTPGPUStorage() # Create HTTP storage instead of WebSocket
|
| 35 |
+
if not self.storage.wait_for_connection():
|
| 36 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 37 |
+
|
| 38 |
+
self.vram = vram
|
| 39 |
+
self.num_sms = num_sms
|
| 40 |
+
self.cores_per_sm = cores_per_sm
|
| 41 |
+
self.total_cores = num_sms * cores_per_sm
|
| 42 |
+
|
| 43 |
+
# Configure for maximum parallel processing at electron speed
|
| 44 |
+
total_tensor_cores = num_sms * cores_per_sm # Use ALL cores for tensor operations
|
| 45 |
+
self.tensor_core_array = TensorCoreArray(
|
| 46 |
+
num_tensor_cores=total_tensor_cores,
|
| 47 |
+
bits=32,
|
| 48 |
+
bandwidth_tbps=drift_velocity / 1e-12 # Bandwidth scaled to electron drift speed
|
| 49 |
+
)
|
| 50 |
+
self.tensor_cores_initialized = False
|
| 51 |
+
|
| 52 |
+
# Initialize model, tensor, and tokenizer tracking
|
| 53 |
+
self.model_registry: Dict[str, Dict[str, Any]] = {} # Track loaded models
|
| 54 |
+
self.tensor_registry: Dict[str, Dict[str, Any]] = {} # Track tensor metadata
|
| 55 |
+
self.tokenizer_registry: Dict[str, Any] = {} # Track tokenizers
|
| 56 |
+
self.resource_monitor = {
|
| 57 |
+
'vram_used': 0,
|
| 58 |
+
'active_tensors': 0,
|
| 59 |
+
'loaded_models': set()
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# AI operation statistics
|
| 63 |
+
self.operations_performed = 0
|
| 64 |
+
self.total_compute_time = 0.0
|
| 65 |
+
self.flops_performed = 0
|
| 66 |
+
|
| 67 |
+
# HTTP-based memory management
|
| 68 |
+
self.model_registry = {} # Track loaded models
|
| 69 |
+
self.matrix_registry = {} # Track loaded matrices
|
| 70 |
+
self.matrix_counter = 0
|
| 71 |
+
self.activation_cache: Dict[str, str] = {} # Cache activation outputs
|
| 72 |
+
self.weight_cache: Dict[str, Any] = {} # Cache preprocessed weights
|
| 73 |
+
|
| 74 |
+
# Model registries
|
| 75 |
+
self.model_registry: Dict[str, Any] = {}
|
| 76 |
+
self.tokenizer_registry: Dict[str, Any] = {}
|
| 77 |
+
self.model_configs: Dict[str, Any] = {} # Store model architectures
|
| 78 |
+
self.model_loaded = False
|
| 79 |
+
|
| 80 |
+
# Batch processing configuration
|
| 81 |
+
self.max_batch_size = 64
|
| 82 |
+
self.min_batch_size = 4
|
| 83 |
+
self.dynamic_batching = True # Enable automatic batch size adjustment
|
| 84 |
+
|
| 85 |
+
def _serialize_model_config(self, config: Any) -> dict:
|
| 86 |
+
"""Convert model config to a serializable format."""
|
| 87 |
+
# Handle None case first
|
| 88 |
+
if config is None:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
# Handle Florence2LanguageConfig specifically
|
| 92 |
+
if config.__class__.__name__ == "Florence2LanguageConfig":
|
| 93 |
+
try:
|
| 94 |
+
return {
|
| 95 |
+
"type": "Florence2LanguageConfig",
|
| 96 |
+
"model_type": getattr(config, "model_type", ""),
|
| 97 |
+
"architectures": getattr(config, "architectures", []),
|
| 98 |
+
"hidden_size": getattr(config, "hidden_size", 0),
|
| 99 |
+
"num_attention_heads": getattr(config, "num_attention_heads", 0),
|
| 100 |
+
"num_hidden_layers": getattr(config, "num_hidden_layers", 0),
|
| 101 |
+
"intermediate_size": getattr(config, "intermediate_size", 0),
|
| 102 |
+
"max_position_embeddings": getattr(config, "max_position_embeddings", 0),
|
| 103 |
+
"layer_norm_eps": getattr(config, "layer_norm_eps", 1e-12),
|
| 104 |
+
"vocab_size": getattr(config, "vocab_size", 0)
|
| 105 |
+
}
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Warning: Error serializing Florence2LanguageConfig: {e}")
|
| 108 |
+
return {"type": "Florence2LanguageConfig", "error": str(e)}
|
| 109 |
+
|
| 110 |
+
# Handle standard types
|
| 111 |
+
if isinstance(config, (int, float, str, bool)):
|
| 112 |
+
return config
|
| 113 |
+
|
| 114 |
+
# Handle lists and tuples
|
| 115 |
+
if isinstance(config, (list, tuple)):
|
| 116 |
+
return [self._serialize_model_config(item) for item in config]
|
| 117 |
+
|
| 118 |
+
# Handle dictionaries
|
| 119 |
+
if isinstance(config, dict):
|
| 120 |
+
return {k: self._serialize_model_config(v) for k, v in config.items()}
|
| 121 |
+
|
| 122 |
+
# Handle objects with __dict__
|
| 123 |
+
if hasattr(config, '__dict__'):
|
| 124 |
+
config_dict = {}
|
| 125 |
+
for key, value in config.__dict__.items():
|
| 126 |
+
try:
|
| 127 |
+
# Skip private attributes
|
| 128 |
+
if key.startswith('_'):
|
| 129 |
+
continue
|
| 130 |
+
config_dict[key] = self._serialize_model_config(value)
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"Warning: Error serializing attribute {key}: {e}")
|
| 133 |
+
config_dict[key] = str(value)
|
| 134 |
+
return config_dict
|
| 135 |
+
|
| 136 |
+
# Fallback: convert to string representation
|
| 137 |
+
try:
|
| 138 |
+
return str(config)
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return f"<Unserializable object of type {type(config).__name__}: {str(e)}>"
|
| 141 |
+
|
| 142 |
+
def store_model_state(self, model_name: str, model_info: Dict[str, Any]) -> bool:
|
| 143 |
+
"""Store model state in HTTP storage with proper serialization."""
|
| 144 |
+
try:
|
| 145 |
+
# Convert any non-serializable parts of model_info
|
| 146 |
+
serializable_info = self._serialize_model_config(model_info)
|
| 147 |
+
|
| 148 |
+
# Store in model registry
|
| 149 |
+
self.model_registry[model_name] = serializable_info
|
| 150 |
+
|
| 151 |
+
# Save to storage
|
| 152 |
+
if self.storage:
|
| 153 |
+
# Store model info
|
| 154 |
+
info_success = self.storage.store_state(
|
| 155 |
+
"models",
|
| 156 |
+
f"{model_name}/info",
|
| 157 |
+
serializable_info
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Store model state
|
| 161 |
+
state_success = self.storage.store_state(
|
| 162 |
+
"models",
|
| 163 |
+
f"{model_name}/state",
|
| 164 |
+
{"loaded": True, "timestamp": time.time()}
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
if info_success and state_success:
|
| 168 |
+
self.resource_monitor['loaded_models'].add(model_name)
|
| 169 |
+
return True
|
| 170 |
+
|
| 171 |
+
return False
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print(f"Error storing model state: {str(e)}")
|
| 174 |
+
return False
|
| 175 |
+
|
| 176 |
+
def initialize_tensor_cores(self):
|
| 177 |
+
"""Initialize tensor cores and verify they're ready for computation"""
|
| 178 |
+
if self.tensor_cores_initialized:
|
| 179 |
+
return True
|
| 180 |
+
|
| 181 |
+
try:
|
| 182 |
+
# Verify tensor core array is properly initialized
|
| 183 |
+
if not hasattr(self, 'tensor_core_array') or self.tensor_core_array is None:
|
| 184 |
+
raise RuntimeError("Tensor core array not properly initialized")
|
| 185 |
+
|
| 186 |
+
# Initialize tensor cores if needed
|
| 187 |
+
if hasattr(self.tensor_core_array, 'initialize'):
|
| 188 |
+
self.tensor_core_array.initialize()
|
| 189 |
+
|
| 190 |
+
# Verify VRAM access
|
| 191 |
+
if self.vram is None:
|
| 192 |
+
raise RuntimeError("VRAM not properly configured")
|
| 193 |
+
|
| 194 |
+
# Test tensor core functionality with a small computation
|
| 195 |
+
test_input = [[1.0, 2.0], [3.0, 4.0]]
|
| 196 |
+
# Convert input to numpy array if needed
|
| 197 |
+
if isinstance(test_input, list):
|
| 198 |
+
test_input = np.array(test_input, dtype=np.float32)
|
| 199 |
+
|
| 200 |
+
test_result = self.tensor_core_array.matmul(test_input, test_input)
|
| 201 |
+
if test_result is None or not isinstance(test_result, (np.ndarray, list)) or len(test_result) == 0:
|
| 202 |
+
raise RuntimeError("Tensor core test computation failed")
|
| 203 |
+
|
| 204 |
+
self.tensor_cores_initialized = True
|
| 205 |
+
return True
|
| 206 |
+
|
| 207 |
+
except Exception as e:
|
| 208 |
+
print(f"Failed to initialize tensor cores: {str(e)}")
|
| 209 |
+
self.tensor_cores_initialized = False
|
| 210 |
+
return False
|
| 211 |
+
|
| 212 |
+
def set_vram(self, vram):
|
| 213 |
+
"""Set the VRAM reference."""
|
| 214 |
+
self.vram = vram
|
| 215 |
+
|
| 216 |
+
def allocate_matrix(self, shape: Tuple[int, ...], dtype=np.float32,
|
| 217 |
+
name: Optional[str] = None) -> str:
|
| 218 |
+
"""Allocate a matrix in VRAM and return its ID."""
|
| 219 |
+
if not self.vram:
|
| 220 |
+
raise RuntimeError("VRAM not available")
|
| 221 |
+
|
| 222 |
+
if name is None:
|
| 223 |
+
name = f"matrix_{self.matrix_counter}"
|
| 224 |
+
self.matrix_counter += 1
|
| 225 |
+
|
| 226 |
+
# Create matrix data
|
| 227 |
+
matrix_data = np.zeros(shape, dtype=dtype)
|
| 228 |
+
|
| 229 |
+
# Store in VRAM using HTTP storage
|
| 230 |
+
if self.storage.store_tensor(name, matrix_data):
|
| 231 |
+
self.matrix_registry[name] = name
|
| 232 |
+
return name
|
| 233 |
+
else:
|
| 234 |
+
raise RuntimeError(f"Failed to allocate matrix {name}")
|
| 235 |
+
|
| 236 |
+
def load_matrix(self, matrix_data: np.ndarray, name: Optional[str] = None) -> str:
|
| 237 |
+
"""Load matrix data into VRAM and return its ID."""
|
| 238 |
+
if name is None:
|
| 239 |
+
name = f"matrix_{self.matrix_counter}"
|
| 240 |
+
self.matrix_counter += 1
|
| 241 |
+
|
| 242 |
+
# Store in VRAM using HTTP storage
|
| 243 |
+
if self.storage.store_tensor(name, matrix_data):
|
| 244 |
+
self.matrix_registry[name] = name
|
| 245 |
+
return name
|
| 246 |
+
else:
|
| 247 |
+
raise RuntimeError(f"Failed to load matrix {name}")
|
| 248 |
+
|
| 249 |
+
def get_matrix(self, matrix_id: str) -> Optional[np.ndarray]:
|
| 250 |
+
"""Retrieve matrix data from VRAM."""
|
| 251 |
+
if matrix_id not in self.matrix_registry:
|
| 252 |
+
return None
|
| 253 |
+
|
| 254 |
+
return self.storage.load_tensor(matrix_id)
|
| 255 |
+
|
| 256 |
+
def matrix_multiply(self, matrix_a_id: str, matrix_b_id: str,
|
| 257 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 258 |
+
"""Perform matrix multiplication using simulated GPU parallelism."""
|
| 259 |
+
start_time = time.time()
|
| 260 |
+
|
| 261 |
+
# Retrieve matrices from VRAM via HTTP storage
|
| 262 |
+
matrix_a = self.get_matrix(matrix_a_id)
|
| 263 |
+
matrix_b = self.get_matrix(matrix_b_id)
|
| 264 |
+
|
| 265 |
+
if matrix_a is None or matrix_b is None:
|
| 266 |
+
print(f"Error: Could not retrieve matrices {matrix_a_id} or {matrix_b_id}")
|
| 267 |
+
return None
|
| 268 |
+
|
| 269 |
+
try:
|
| 270 |
+
# Check if matrices can be multiplied
|
| 271 |
+
if matrix_a.shape[-1] != matrix_b.shape[0]:
|
| 272 |
+
print(f"Error: Matrix dimensions incompatible for multiplication: "
|
| 273 |
+
f"{matrix_a.shape} x {matrix_b.shape}")
|
| 274 |
+
return None
|
| 275 |
+
|
| 276 |
+
# Route matrix multiplication through the virtual TensorCoreArray
|
| 277 |
+
A = matrix_a.tolist()
|
| 278 |
+
B = matrix_b.tolist()
|
| 279 |
+
result = self.tensor_core_array.matmul(A, B)
|
| 280 |
+
result_array = np.array(result)
|
| 281 |
+
|
| 282 |
+
# Store result in VRAM
|
| 283 |
+
if result_id is None:
|
| 284 |
+
result_id = f"result_{self.matrix_counter}"
|
| 285 |
+
self.matrix_counter += 1
|
| 286 |
+
|
| 287 |
+
result_matrix_id = self.load_matrix(result_array, result_id)
|
| 288 |
+
|
| 289 |
+
# Update statistics
|
| 290 |
+
compute_time = time.time() - start_time
|
| 291 |
+
self.total_compute_time += compute_time
|
| 292 |
+
self.operations_performed += 1
|
| 293 |
+
|
| 294 |
+
# Calculate FLOPs (2 * M * N * K for matrix multiplication)
|
| 295 |
+
m, k = matrix_a.shape
|
| 296 |
+
k2, n = matrix_b.shape
|
| 297 |
+
flops = 2 * m * n * k
|
| 298 |
+
self.flops_performed += flops
|
| 299 |
+
|
| 300 |
+
print(f"Matrix multiplication completed: {matrix_a.shape} x {matrix_b.shape} "
|
| 301 |
+
f"= {result_array.shape} in {compute_time:.4f}s")
|
| 302 |
+
print(f"Simulated {flops:,} FLOPs across {self.total_cores} cores")
|
| 303 |
+
|
| 304 |
+
return result_matrix_id
|
| 305 |
+
|
| 306 |
+
except Exception as e:
|
| 307 |
+
print(f"Error in matrix multiplication: {e}")
|
| 308 |
+
return None
|
| 309 |
+
|
| 310 |
+
def vector_operation(self, operation: VectorOperation, vector_a_id: str,
|
| 311 |
+
vector_b_id: Optional[str] = None,
|
| 312 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 313 |
+
"""Perform vector operations using simulated GPU parallelism."""
|
| 314 |
+
start_time = time.time()
|
| 315 |
+
|
| 316 |
+
# Retrieve vectors from VRAM via HTTP storage
|
| 317 |
+
vector_a = self.get_matrix(vector_a_id)
|
| 318 |
+
if vector_a is None:
|
| 319 |
+
print(f"Error: Could not retrieve vector {vector_a_id}")
|
| 320 |
+
return None
|
| 321 |
+
|
| 322 |
+
vector_b = None
|
| 323 |
+
if vector_b_id:
|
| 324 |
+
vector_b = self.get_matrix(vector_b_id)
|
| 325 |
+
if vector_b is None:
|
| 326 |
+
print(f"Error: Could not retrieve vector {vector_b_id}")
|
| 327 |
+
return None
|
| 328 |
+
|
| 329 |
+
try:
|
| 330 |
+
result = None
|
| 331 |
+
flops = 0
|
| 332 |
+
|
| 333 |
+
if operation == VectorOperation.ADD:
|
| 334 |
+
if vector_b is None:
|
| 335 |
+
raise ValueError("Vector B required for addition")
|
| 336 |
+
result = vector_a + vector_b
|
| 337 |
+
flops = vector_a.size
|
| 338 |
+
|
| 339 |
+
elif operation == VectorOperation.SUBTRACT:
|
| 340 |
+
if vector_b is None:
|
| 341 |
+
raise ValueError("Vector B required for subtraction")
|
| 342 |
+
result = vector_a - vector_b
|
| 343 |
+
flops = vector_a.size
|
| 344 |
+
|
| 345 |
+
elif operation == VectorOperation.MULTIPLY:
|
| 346 |
+
if vector_b is None:
|
| 347 |
+
raise ValueError("Vector B required for multiplication")
|
| 348 |
+
result = vector_a * vector_b
|
| 349 |
+
flops = vector_a.size
|
| 350 |
+
|
| 351 |
+
elif operation == VectorOperation.DIVIDE:
|
| 352 |
+
if vector_b is None:
|
| 353 |
+
raise ValueError("Vector B required for division")
|
| 354 |
+
result = vector_a / vector_b
|
| 355 |
+
flops = vector_a.size
|
| 356 |
+
|
| 357 |
+
elif operation == VectorOperation.DOT_PRODUCT:
|
| 358 |
+
if vector_b is None:
|
| 359 |
+
raise ValueError("Vector B required for dot product")
|
| 360 |
+
result = np.dot(vector_a.flatten(), vector_b.flatten())
|
| 361 |
+
flops = 2 * vector_a.size
|
| 362 |
+
|
| 363 |
+
elif operation == VectorOperation.CROSS_PRODUCT:
|
| 364 |
+
if vector_b is None:
|
| 365 |
+
raise ValueError("Vector B required for cross product")
|
| 366 |
+
if vector_a.size != 3 or vector_b.size != 3:
|
| 367 |
+
raise ValueError("Cross product requires 3D vectors")
|
| 368 |
+
result = np.cross(vector_a.flatten(), vector_b.flatten())
|
| 369 |
+
flops = 6 # Cross product operations
|
| 370 |
+
|
| 371 |
+
elif operation == VectorOperation.NORMALIZE:
|
| 372 |
+
magnitude = np.linalg.norm(vector_a)
|
| 373 |
+
if magnitude == 0:
|
| 374 |
+
result = vector_a
|
| 375 |
+
else:
|
| 376 |
+
result = vector_a / magnitude
|
| 377 |
+
flops = vector_a.size + 1 # Division + sqrt
|
| 378 |
+
|
| 379 |
+
elif operation == VectorOperation.MAGNITUDE:
|
| 380 |
+
result = np.array([np.linalg.norm(vector_a)])
|
| 381 |
+
flops = vector_a.size + 1 # Sum of squares + sqrt
|
| 382 |
+
|
| 383 |
+
else:
|
| 384 |
+
raise ValueError(f"Unknown vector operation: {operation}")
|
| 385 |
+
|
| 386 |
+
# Store result
|
| 387 |
+
if result_id is None:
|
| 388 |
+
result_id = f"vector_result_{self.matrix_counter}"
|
| 389 |
+
self.matrix_counter += 1
|
| 390 |
+
|
| 391 |
+
result_vector_id = self.load_matrix(result, result_id)
|
| 392 |
+
|
| 393 |
+
# Update statistics
|
| 394 |
+
compute_time = time.time() - start_time
|
| 395 |
+
self.total_compute_time += compute_time
|
| 396 |
+
self.operations_performed += 1
|
| 397 |
+
self.flops_performed += flops
|
| 398 |
+
|
| 399 |
+
print(f"Vector operation {operation.value} completed in {compute_time:.4f}s")
|
| 400 |
+
print(f"Simulated {flops:,} FLOPs across {self.total_cores} cores")
|
| 401 |
+
|
| 402 |
+
return result_vector_id
|
| 403 |
+
|
| 404 |
+
except Exception as e:
|
| 405 |
+
print(f"Error in vector operation: {e}")
|
| 406 |
+
return None
|
| 407 |
+
|
| 408 |
+
def has_model(self, model_id: str) -> bool:
|
| 409 |
+
"""Check if model is loaded via HTTP storage"""
|
| 410 |
+
return self.storage.is_model_loaded(model_id)
|
| 411 |
+
|
| 412 |
+
def load_model(self, model_id: str, model=None, processor=None) -> bool:
|
| 413 |
+
"""Load model via HTTP storage"""
|
| 414 |
+
try:
|
| 415 |
+
# Prepare model data for storage
|
| 416 |
+
model_data = None
|
| 417 |
+
if model is not None:
|
| 418 |
+
# In a real implementation, this would serialize the model
|
| 419 |
+
model_data = {
|
| 420 |
+
"model_type": type(model).__name__,
|
| 421 |
+
"config": self._serialize_model_config(getattr(model, 'config', None)),
|
| 422 |
+
"loaded_at": time.time()
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
# Use HTTP storage to load model
|
| 426 |
+
success = self.storage.load_model(model_id, model_data=model_data)
|
| 427 |
+
|
| 428 |
+
if success:
|
| 429 |
+
self.model_registry[model_id] = {
|
| 430 |
+
"model_data": model_data,
|
| 431 |
+
"processor": processor,
|
| 432 |
+
"loaded_at": time.time()
|
| 433 |
+
}
|
| 434 |
+
self.resource_monitor['loaded_models'].add(model_id)
|
| 435 |
+
return True
|
| 436 |
+
|
| 437 |
+
return False
|
| 438 |
+
|
| 439 |
+
except Exception as e:
|
| 440 |
+
print(f"Error loading model {model_id}: {str(e)}")
|
| 441 |
+
return False
|
| 442 |
+
|
| 443 |
+
def inference(self, model_id: str, input_tensor_id: str) -> Optional[np.ndarray]:
|
| 444 |
+
"""Run inference using HTTP storage"""
|
| 445 |
+
try:
|
| 446 |
+
# Load input tensor
|
| 447 |
+
input_data = self.storage.load_tensor(input_tensor_id)
|
| 448 |
+
if input_data is None:
|
| 449 |
+
print(f"Could not load input tensor {input_tensor_id}")
|
| 450 |
+
return None
|
| 451 |
+
|
| 452 |
+
# Run inference via HTTP API
|
| 453 |
+
result = self.storage.start_inference(model_id, input_data)
|
| 454 |
+
|
| 455 |
+
if result and result.get('output') is not None:
|
| 456 |
+
return result['output']
|
| 457 |
+
else:
|
| 458 |
+
print(f"Inference failed for model {model_id}")
|
| 459 |
+
return None
|
| 460 |
+
|
| 461 |
+
except Exception as e:
|
| 462 |
+
print(f"Error during inference: {str(e)}")
|
| 463 |
+
return None
|
| 464 |
+
|
| 465 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 466 |
+
"""Get AI accelerator statistics"""
|
| 467 |
+
return {
|
| 468 |
+
"operations_performed": self.operations_performed,
|
| 469 |
+
"total_compute_time": self.total_compute_time,
|
| 470 |
+
"flops_performed": self.flops_performed,
|
| 471 |
+
"avg_ops_per_second": self.operations_performed / max(self.total_compute_time, 0.001),
|
| 472 |
+
"tensor_cores_initialized": self.tensor_cores_initialized,
|
| 473 |
+
"total_cores": self.total_cores,
|
| 474 |
+
"loaded_models": list(self.resource_monitor['loaded_models']),
|
| 475 |
+
"storage_status": self.storage.get_connection_status() if self.storage else None
|
| 476 |
+
}
|
| 477 |
+
|
core.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Physics-inspired digital core model for virtual GPU v2.
|
| 3 |
+
Contains AdvancedCore class and example usage.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from logic_gates import ControlUnit, ALU2Bit, RegisterFile2x2, SimpleMMU
|
| 7 |
+
|
| 8 |
+
class AdvancedCore:
|
| 9 |
+
"""
|
| 10 |
+
Simulates a physics-inspired digital core with:
|
| 11 |
+
- Control unit
|
| 12 |
+
- ALU
|
| 13 |
+
- Register file
|
| 14 |
+
- MMU
|
| 15 |
+
- Clocking and timing at the voltage/physics level
|
| 16 |
+
"""
|
| 17 |
+
def __init__(self, bits=2, num_registers=2):
|
| 18 |
+
self.control = ControlUnit()
|
| 19 |
+
self.alu = ALU2Bit()
|
| 20 |
+
self.regfile = RegisterFile2x2()
|
| 21 |
+
self.mmu = SimpleMMU(num_registers=num_registers, bits=bits)
|
| 22 |
+
self.clk = 0.7 # High voltage for clock
|
| 23 |
+
self.bits = bits
|
| 24 |
+
|
| 25 |
+
def step(self, a, b, cin, opcode, reg_sel):
|
| 26 |
+
# Set control signals
|
| 27 |
+
self.control.set_opcode(opcode)
|
| 28 |
+
ctrl = self.control.get_control_signals()
|
| 29 |
+
# ALU operation
|
| 30 |
+
(r0, r1), cout = self.alu.operate(a[0], a[1], b[0], b[1], cin, ctrl['alu_op'])
|
| 31 |
+
# Write to register file
|
| 32 |
+
self.regfile.write(r0, r1, self.clk, reg_sel)
|
| 33 |
+
# MMU write (simulate memory-mapped register)
|
| 34 |
+
self.mmu.write(reg_sel, [r0, r1], self.clk)
|
| 35 |
+
# Read back
|
| 36 |
+
reg_out = self.regfile.read(reg_sel)
|
| 37 |
+
mmu_out = self.mmu.read(reg_sel)
|
| 38 |
+
return {
|
| 39 |
+
'alu_result': (r0, r1),
|
| 40 |
+
'carry_out': cout,
|
| 41 |
+
'regfile_out': reg_out,
|
| 42 |
+
'mmu_out': mmu_out,
|
| 43 |
+
'control': ctrl
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
print("\n--- Advanced Core Simulation ---")
|
| 48 |
+
core = AdvancedCore(bits=2, num_registers=2)
|
| 49 |
+
# Simulate an ADD operation between (1,0) and (1,1), store in reg0
|
| 50 |
+
result = core.step([0.7, 0.0], [0.7, 0.7], 0.0, 0b10, 0)
|
| 51 |
+
print("Core step (ADD):", result)
|
| 52 |
+
# Simulate an OR operation between (1,0) and (1,1), store in reg1
|
| 53 |
+
result = core.step([0.7, 0.0], [0.7, 0.7], 0.0, 0b01, 1)
|
| 54 |
+
print("Core step (OR):", result)
|
custom_vram.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
class CustomVRAM:
|
| 4 |
+
def __init__(self, global_mem):
|
| 5 |
+
self.global_mem = global_mem
|
| 6 |
+
self.texture_registry = {}
|
| 7 |
+
self.texture_counter = 0
|
| 8 |
+
|
| 9 |
+
def load_texture(self, data: np.ndarray, name: str = None) -> str:
|
| 10 |
+
if name is None:
|
| 11 |
+
name = f"texture_{self.texture_counter}"
|
| 12 |
+
self.texture_counter += 1
|
| 13 |
+
|
| 14 |
+
# Serialize numpy array to bytes
|
| 15 |
+
data_bytes = data.tobytes()
|
| 16 |
+
data_shape = data.shape
|
| 17 |
+
data_dtype = str(data.dtype)
|
| 18 |
+
|
| 19 |
+
# Store metadata and data in global memory
|
| 20 |
+
# For simplicity, we'll store everything contiguously for now.
|
| 21 |
+
# In a real system, this would involve more sophisticated memory management.
|
| 22 |
+
|
| 23 |
+
# Find a suitable address in global memory (very simplified, no actual allocation logic)
|
| 24 |
+
# For this simulation, we'll just use a simple counter for addresses.
|
| 25 |
+
# In a real scenario, you'd need a proper memory allocator.
|
| 26 |
+
address = self.global_mem.allocate_space(len(data_bytes) + 100) # +100 for metadata
|
| 27 |
+
|
| 28 |
+
# Store shape, dtype, and then data
|
| 29 |
+
# This is a very basic serialization. For production, consider more robust methods.
|
| 30 |
+
metadata = f"{data_shape};{data_dtype};{len(data_bytes)}".encode("utf-8")
|
| 31 |
+
self.global_mem.write(address, list(metadata))
|
| 32 |
+
self.global_mem.write(address + len(metadata), list(data_bytes))
|
| 33 |
+
|
| 34 |
+
self.texture_registry[name] = {
|
| 35 |
+
"address": address,
|
| 36 |
+
"size": len(data_bytes),
|
| 37 |
+
"shape": data_shape,
|
| 38 |
+
"dtype": data_dtype,
|
| 39 |
+
"metadata_size": len(metadata)
|
| 40 |
+
}
|
| 41 |
+
return name
|
| 42 |
+
|
| 43 |
+
def get_texture(self, name: str) -> np.ndarray:
|
| 44 |
+
if name not in self.texture_registry:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
texture_info = self.texture_registry[name]
|
| 48 |
+
address = texture_info["address"]
|
| 49 |
+
size = texture_info["size"]
|
| 50 |
+
shape = texture_info["shape"]
|
| 51 |
+
dtype = texture_info["dtype"]
|
| 52 |
+
metadata_size = texture_info["metadata_size"]
|
| 53 |
+
|
| 54 |
+
# Read data from global memory
|
| 55 |
+
data_bytes = bytes(self.global_mem.read(address + metadata_size, size))
|
| 56 |
+
|
| 57 |
+
# Deserialize bytes to numpy array
|
| 58 |
+
return np.frombuffer(data_bytes, dtype=dtype).reshape(shape)
|
| 59 |
+
|
| 60 |
+
def has_texture(self, name: str) -> bool:
|
| 61 |
+
return name in self.texture_registry
|
| 62 |
+
|
| 63 |
+
def delete_texture(self, name: str):
|
| 64 |
+
if name in self.texture_registry:
|
| 65 |
+
# In a real system, you'd deallocate the memory.
|
| 66 |
+
# For this simulation, we just remove the entry.
|
| 67 |
+
del self.texture_registry[name]
|
| 68 |
+
|
| 69 |
+
|
electron_speed.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Calculate electron drift speed and relate it to transistor switching (tick) rate for a modern GPU.
|
| 3 |
+
Assume: We want to simulate 900 quintillion (9e20) transistor switches per second (B200 scale).
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Physical constants
|
| 7 |
+
ELEM_CHARGE = 1.602e-19 # Coulombs
|
| 8 |
+
ELECTRON_MASS = 9.109e-31 # kg
|
| 9 |
+
VACUUM_PERMITTIVITY = 8.854e-12 # F/m
|
| 10 |
+
SILICON_MOBILITY = 0.14 # m^2/(V·s) (typical for electrons in Si at room temp)
|
| 11 |
+
|
| 12 |
+
# Example parameters (can be tuned for realism)
|
| 13 |
+
VOLTAGE = 0.7 # V (typical for advanced nodes)
|
| 14 |
+
CHANNEL_LENGTH = 5e-9 # 5 nm process
|
| 15 |
+
ELECTRIC_FIELD = VOLTAGE / CHANNEL_LENGTH # V/m
|
| 16 |
+
|
| 17 |
+
# Calculate drift velocity (v = μE)
|
| 18 |
+
drift_velocity = SILICON_MOBILITY * ELECTRIC_FIELD # m/s
|
| 19 |
+
|
| 20 |
+
# Calculate time for electron to cross channel (t = L / v)
|
| 21 |
+
transit_time = CHANNEL_LENGTH / drift_velocity # seconds
|
| 22 |
+
|
| 23 |
+
# Calculate max theoretical switching frequency (f = 1 / t)
|
| 24 |
+
max_switch_freq = 1 / transit_time # Hz
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# For 900 quintillion switches/sec, but with 600 billion transistors
|
| 28 |
+
TARGET_SWITCHES_PER_SEC = 9e20
|
| 29 |
+
TRANSISTORS_ON_CHIP = 6e11 # 600 billion
|
| 30 |
+
transistors_needed = TARGET_SWITCHES_PER_SEC / max_switch_freq
|
| 31 |
+
required_switch_freq_per_transistor = TARGET_SWITCHES_PER_SEC / TRANSISTORS_ON_CHIP
|
| 32 |
+
|
| 33 |
+
# Speed of light in silicon (approx 2/3 c)
|
| 34 |
+
SPEED_OF_LIGHT_VACUUM = 3e8 # m/s
|
| 35 |
+
SILICON_REFRACTIVE_INDEX = 3.5
|
| 36 |
+
speed_of_light_silicon = SPEED_OF_LIGHT_VACUUM / SILICON_REFRACTIVE_INDEX
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
print(f"Electron drift velocity: {drift_velocity:.2e} m/s")
|
| 41 |
+
print(f"Channel transit time: {transit_time:.2e} s")
|
| 42 |
+
print(f"Max transistor switching frequency: {max_switch_freq:.2e} Hz")
|
| 43 |
+
print(f"To achieve {TARGET_SWITCHES_PER_SEC:.1e} switches/sec:")
|
| 44 |
+
print(f"- You'd need {transistors_needed:.2e} transistors switching at max speed in parallel.")
|
| 45 |
+
print(f"- For a chip with 600B transistors, each must switch at {required_switch_freq_per_transistor:.2e} Hz.")
|
| 46 |
+
print(f"- Electron drift speed: {drift_velocity:.2e} m/s vs. speed of light in silicon: {speed_of_light_silicon:.2e} m/s")
|
| 47 |
+
print(f"- Electron drift is ~{(drift_velocity/speed_of_light_silicon)*100:.2f}% the speed of light in silicon (photon speed).")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# --- SR, D, JK, T Flip-Flop Physics/Timing Summary ---
|
| 51 |
+
print("\n--- Flip-Flop Types and Switching Physics ---")
|
| 52 |
+
print("SR Flip-Flop: Set-Reset, basic memory, built from NAND/NOR gates.")
|
| 53 |
+
print("D Flip-Flop: Data/Delay, synchronizes input to clock, used in registers.")
|
| 54 |
+
print("JK Flip-Flop: Universal, toggles or sets/resets based on inputs.")
|
| 55 |
+
print("T Flip-Flop: Toggle, divides clock, used in counters.")
|
| 56 |
+
print("All flip-flops are built from logic gates, so their switching speed is limited by the gate delay (set by electron drift and channel length).\n")
|
| 57 |
+
|
| 58 |
+
# Example: Calculate flip-flop switching time (assuming 4 gate delays per flip-flop)
|
| 59 |
+
GATE_DELAY = transit_time # seconds, from above
|
| 60 |
+
FF_GATE_COUNT = 4 # typical for basic flip-flop
|
| 61 |
+
flip_flop_delay = FF_GATE_COUNT * GATE_DELAY
|
| 62 |
+
flip_flop_max_freq = 1 / flip_flop_delay
|
| 63 |
+
|
| 64 |
+
print(f"Estimated flip-flop delay: {flip_flop_delay:.2e} s (for {FF_GATE_COUNT} gates)")
|
| 65 |
+
print(f"Max flip-flop switching frequency: {flip_flop_max_freq:.2e} Hz")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
flip_flops.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hyperrealistic voltage-based flip-flops: SR, D, JK, and T.
|
| 3 |
+
Each flip-flop is built from voltage-based logic gates and simulates real-world behavior.
|
| 4 |
+
"""
|
| 5 |
+
from logic_gates import NANDGate, ANDGate, ORGate, NOTGate, VDD, VSS, VTH, GATE_DELAY
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
class SRFlipFlop:
|
| 9 |
+
"""Set-Reset flip-flop using cross-coupled NAND gates."""
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.nand1 = NANDGate()
|
| 12 |
+
self.nand2 = NANDGate()
|
| 13 |
+
self.q = VSS
|
| 14 |
+
self.q_bar = VDD
|
| 15 |
+
|
| 16 |
+
def update(self, s, r):
|
| 17 |
+
# s, r are voltages
|
| 18 |
+
# Cross-coupled NANDs
|
| 19 |
+
q_new = self.nand1.output(s, self.q_bar)
|
| 20 |
+
q_bar_new = self.nand2.output(r, q_new)
|
| 21 |
+
self.q = q_new
|
| 22 |
+
self.q_bar = q_bar_new
|
| 23 |
+
return self.q, self.q_bar
|
| 24 |
+
|
| 25 |
+
class DFlipFlop:
|
| 26 |
+
"""D (Data) flip-flop using SR flip-flop and NOT gate."""
|
| 27 |
+
def __init__(self):
|
| 28 |
+
self.sr = SRFlipFlop()
|
| 29 |
+
self.notg = NOTGate()
|
| 30 |
+
|
| 31 |
+
def update(self, d, clk):
|
| 32 |
+
# d, clk are voltages
|
| 33 |
+
s = self.nand(d, clk)
|
| 34 |
+
r = self.nand(self.notg.output(d), clk)
|
| 35 |
+
return self.sr.update(s, r)
|
| 36 |
+
|
| 37 |
+
def nand(self, a, b):
|
| 38 |
+
return NANDGate().output(a, b)
|
| 39 |
+
|
| 40 |
+
class JKFlipFlop:
|
| 41 |
+
"""JK flip-flop using NAND gates."""
|
| 42 |
+
def __init__(self):
|
| 43 |
+
self.q = VSS
|
| 44 |
+
self.q_bar = VDD
|
| 45 |
+
self.nand1 = NANDGate()
|
| 46 |
+
self.nand2 = NANDGate()
|
| 47 |
+
self.nand3 = NANDGate()
|
| 48 |
+
self.nand4 = NANDGate()
|
| 49 |
+
|
| 50 |
+
def update(self, j, k, clk):
|
| 51 |
+
# j, k, clk are voltages
|
| 52 |
+
j_in = self.nand1.output(j, clk, self.q_bar)
|
| 53 |
+
k_in = self.nand2.output(k, clk, self.q)
|
| 54 |
+
q_new = self.nand3.output(j_in, self.q_bar)
|
| 55 |
+
q_bar_new = self.nand4.output(k_in, q_new)
|
| 56 |
+
self.q = q_new
|
| 57 |
+
self.q_bar = q_bar_new
|
| 58 |
+
return self.q, self.q_bar
|
| 59 |
+
|
| 60 |
+
class TFlipFlop:
|
| 61 |
+
"""T (Toggle) flip-flop using JK flip-flop."""
|
| 62 |
+
def __init__(self):
|
| 63 |
+
self.jk = JKFlipFlop()
|
| 64 |
+
|
| 65 |
+
def update(self, t, clk):
|
| 66 |
+
# t, clk are voltages
|
| 67 |
+
return self.jk.update(t, t, clk)
|
| 68 |
+
|
| 69 |
+
# Example usage
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
print("SR Flip-Flop:")
|
| 72 |
+
sr = SRFlipFlop()
|
| 73 |
+
print("Set:", sr.update(VDD, VSS))
|
| 74 |
+
print("Reset:", sr.update(VSS, VDD))
|
| 75 |
+
print("Hold:", sr.update(VSS, VSS))
|
| 76 |
+
|
| 77 |
+
print("\nD Flip-Flop:")
|
| 78 |
+
dff = DFlipFlop()
|
| 79 |
+
print("D=1, CLK=1:", dff.update(VDD, VDD))
|
| 80 |
+
print("D=0, CLK=1:", dff.update(VSS, VDD))
|
| 81 |
+
|
| 82 |
+
print("\nJK Flip-Flop:")
|
| 83 |
+
jk = JKFlipFlop()
|
| 84 |
+
print("J=1, K=0, CLK=1:", jk.update(VDD, VSS, VDD))
|
| 85 |
+
print("J=0, K=1, CLK=1:", jk.update(VSS, VDD, VDD))
|
| 86 |
+
print("J=1, K=1, CLK=1 (toggle):", jk.update(VDD, VDD, VDD))
|
| 87 |
+
|
| 88 |
+
print("\nT Flip-Flop:")
|
| 89 |
+
tff = TFlipFlop()
|
| 90 |
+
print("T=1, CLK=1 (toggle):", tff.update(VDD, VDD))
|
| 91 |
+
print("T=0, CLK=1 (hold):", tff.update(VSS, VDD))
|
gpu_arch.py
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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RAM
|
| 6 |
+
from ai import AIAccelerator
|
| 7 |
+
|
| 8 |
+
class TensorCoreDB:
|
| 9 |
+
def __init__(self, tensor_core_id, sm_id, db):
|
| 10 |
+
self.tensor_core_id = tensor_core_id
|
| 11 |
+
self.sm_id = sm_id
|
| 12 |
+
self.db = db
|
| 13 |
+
|
| 14 |
+
def load_state(self):
|
| 15 |
+
state = self.db.load_state("tensor_core", "tensor_core_id", self.tensor_core_id)
|
| 16 |
+
return state or {}
|
| 17 |
+
|
| 18 |
+
def save_state(self, state):
|
| 19 |
+
self.db.save_state("tensor_core", "tensor_core_id", self.tensor_core_id, state)
|
| 20 |
+
|
| 21 |
+
def matmul(self, A, B):
|
| 22 |
+
state = self.load_state()
|
| 23 |
+
# Simulate a matrix multiply (for demo, just sum all elements)
|
| 24 |
+
result = sum(sum(row) for row in A) * sum(sum(row) for row in B)
|
| 25 |
+
state["last_result"] = result
|
| 26 |
+
self.save_state(state)
|
| 27 |
+
return result
|
| 28 |
+
|
| 29 |
+
class OpticalInterconnect:
|
| 30 |
+
def __init__(self, bandwidth_tbps=800, latency_ns=1):
|
| 31 |
+
self.bandwidth_tbps = bandwidth_tbps # TB/s
|
| 32 |
+
self.latency_ns = latency_ns # nanoseconds
|
| 33 |
+
|
| 34 |
+
def transfer_time(self, data_size_bytes):
|
| 35 |
+
# Time = latency + (data_size / bandwidth)
|
| 36 |
+
bandwidth_bytes_per_s = self.bandwidth_tbps * 1e12
|
| 37 |
+
transfer_time_s = self.latency_ns * 1e-9 + (data_size_bytes / bandwidth_bytes_per_s)
|
| 38 |
+
return transfer_time_s
|
| 39 |
+
|
| 40 |
+
class Thread:
|
| 41 |
+
def __init__(self, thread_id, core):
|
| 42 |
+
self.thread_id = thread_id
|
| 43 |
+
self.core = core
|
| 44 |
+
self.active = True
|
| 45 |
+
self.result = None
|
| 46 |
+
|
| 47 |
+
def run(self, a, b, cin, opcode, reg_sel):
|
| 48 |
+
if self.active:
|
| 49 |
+
self.result = self.core.step(a, b, cin, opcode, reg_sel)
|
| 50 |
+
return self.result
|
| 51 |
+
|
| 52 |
+
class Warp:
|
| 53 |
+
def __init__(self, warp_id, threads):
|
| 54 |
+
self.warp_id = warp_id
|
| 55 |
+
self.threads = threads # List of Thread objects
|
| 56 |
+
self.active = True
|
| 57 |
+
|
| 58 |
+
def run(self, a, b, cin, opcode, reg_sel):
|
| 59 |
+
# All threads in a warp execute in lockstep (SIMT)
|
| 60 |
+
return [thread.run(a, b, cin, opcode, reg_sel) for thread in self.threads if thread.active]
|
| 61 |
+
|
| 62 |
+
class WarpScheduler:
|
| 63 |
+
def __init__(self, warps):
|
| 64 |
+
self.warps = warps # List of Warp objects
|
| 65 |
+
self.schedule_ptr = 0
|
| 66 |
+
|
| 67 |
+
def schedule(self):
|
| 68 |
+
# Simple round-robin scheduler
|
| 69 |
+
if not self.warps:
|
| 70 |
+
return None
|
| 71 |
+
warp = self.warps[self.schedule_ptr]
|
| 72 |
+
self.schedule_ptr = (self.schedule_ptr + 1) % len(self.warps)
|
| 73 |
+
return warp
|
| 74 |
+
|
| 75 |
+
class SharedMemory:
|
| 76 |
+
def __init__(self, size):
|
| 77 |
+
self.size = size
|
| 78 |
+
self.mem = [0] * size
|
| 79 |
+
|
| 80 |
+
def read(self, addr):
|
| 81 |
+
return self.mem[addr % self.size]
|
| 82 |
+
|
| 83 |
+
def write(self, addr, value):
|
| 84 |
+
self.mem[addr % self.size] = value
|
| 85 |
+
|
| 86 |
+
def read_matrix(self, addr, n, m):
|
| 87 |
+
# Simulate reading an n x m matrix from shared memory
|
| 88 |
+
# For simplicity, treat addr as row offset
|
| 89 |
+
return [
|
| 90 |
+
[self.mem[(addr + i * m + j) % self.size] for j in range(m)]
|
| 91 |
+
for i in range(n)
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
class L1Cache:
|
| 95 |
+
def __init__(self, size):
|
| 96 |
+
self.size = size
|
| 97 |
+
self.cache = [None] * size
|
| 98 |
+
|
| 99 |
+
def read(self, addr):
|
| 100 |
+
return self.cache[addr % self.size]
|
| 101 |
+
|
| 102 |
+
def write(self, addr, value):
|
| 103 |
+
self.cache[addr % self.size] = value
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# GlobalMemory now uses RAMController and persists to .db
|
| 107 |
+
class GlobalMemory:
|
| 108 |
+
def __init__(self, size_bytes=None, db_path=None):
|
| 109 |
+
if db_path is None:
|
| 110 |
+
import uuid
|
| 111 |
+
db_path = os.path.join(os.path.dirname(__file__), f"global_mem_{uuid.uuid4().hex}.db")
|
| 112 |
+
self.size_bytes = float('inf') # Unlimited size
|
| 113 |
+
self.ram = RAMController(size_bytes=None, db_path=db_path) # Pass None for unlimited size
|
| 114 |
+
self.allocated_address = 0 # Simple allocation pointer
|
| 115 |
+
|
| 116 |
+
def read(self, addr, length=1):
|
| 117 |
+
data = self.ram.read(addr, length)
|
| 118 |
+
# Return as int for compatibility (simulate voltage)
|
| 119 |
+
if length == 1:
|
| 120 |
+
return int(data[0]) if data else 0
|
| 121 |
+
return [int(b) for b in data]
|
| 122 |
+
|
| 123 |
+
def write(self, addr, value):
|
| 124 |
+
# Accepts int, float, or list/bytes
|
| 125 |
+
if isinstance(value, (int, float)):
|
| 126 |
+
data = bytes([int(value) & 0xFF])
|
| 127 |
+
elif isinstance(value, (bytes, bytearray)):
|
| 128 |
+
data = value
|
| 129 |
+
elif isinstance(value, list):
|
| 130 |
+
# Convert list of integers to bytes, assuming each integer is a byte value (0-255)
|
| 131 |
+
data = bytes(value)
|
| 132 |
+
else:
|
| 133 |
+
raise TypeError("Unsupported value type for write")
|
| 134 |
+
self.ram.write(addr, data)
|
| 135 |
+
|
| 136 |
+
def read_matrix(self, addr, n, m):
|
| 137 |
+
# Read n*m bytes and reshape
|
| 138 |
+
data = self.ram.read(addr, n * m)
|
| 139 |
+
return [list(data[i*m:(i+1)*m]) for i in range(n)]
|
| 140 |
+
|
| 141 |
+
def allocate_space(self, size_bytes: int) -> int:
|
| 142 |
+
"""Simulates allocating space in global memory with unlimited capacity."""
|
| 143 |
+
allocated_addr = self.allocated_address
|
| 144 |
+
self.allocated_address += size_bytes
|
| 145 |
+
return allocated_addr # Always succeeds due to unlimited storage
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# StreamingMultiprocessor now only loads state from DB as needed
|
| 149 |
+
class StreamingMultiprocessor:
|
| 150 |
+
def __init__(self, sm_id, chip_id, db: GPUStateDB, num_cores_per_sm=128, warps_per_sm=164, threads_per_warp=700, num_tensor_cores=8):
|
| 151 |
+
self.sm_id = sm_id
|
| 152 |
+
self.chip_id = chip_id
|
| 153 |
+
self.db = db
|
| 154 |
+
self.num_cores_per_sm = num_cores_per_sm
|
| 155 |
+
self.warps_per_sm = warps_per_sm
|
| 156 |
+
self.threads_per_warp = threads_per_warp
|
| 157 |
+
self.num_tensor_cores = num_tensor_cores
|
| 158 |
+
self.global_mem = None # Will be set by GPUMemoryHierarchy
|
| 159 |
+
|
| 160 |
+
def load_state(self):
|
| 161 |
+
state = self.db.load_state("sm", "sm_id", self.sm_id)
|
| 162 |
+
return state or {}
|
| 163 |
+
|
| 164 |
+
def save_state(self, state):
|
| 165 |
+
self.db.save_state("sm", "sm_id", self.sm_id, state)
|
| 166 |
+
|
| 167 |
+
def attach_global_mem(self, global_mem):
|
| 168 |
+
self.global_mem = global_mem
|
| 169 |
+
|
| 170 |
+
def get_core(self, core_id):
|
| 171 |
+
return Core(core_id, self.sm_id, self.db)
|
| 172 |
+
|
| 173 |
+
def get_warp(self, warp_id):
|
| 174 |
+
return WarpDB(warp_id, self.sm_id, self.db)
|
| 175 |
+
|
| 176 |
+
def get_tensor_core(self, tensor_core_id):
|
| 177 |
+
return TensorCoreDB(tensor_core_id, self.sm_id, self.db)
|
| 178 |
+
|
| 179 |
+
def run_next_warp(self, a, b, cin, opcode, reg_sel):
|
| 180 |
+
# Example: load warp 0, run, save
|
| 181 |
+
warp = self.get_warp(0)
|
| 182 |
+
result = warp.run(a, b, cin, opcode, reg_sel)
|
| 183 |
+
return result
|
| 184 |
+
|
| 185 |
+
def tensor_core_matmul(self, A, B, tensor_core_id=0):
|
| 186 |
+
tensor_core = self.get_tensor_core(tensor_core_id)
|
| 187 |
+
return tensor_core.matmul(A, B)
|
| 188 |
+
|
| 189 |
+
class Core:
|
| 190 |
+
def __init__(self, core_id, sm_id, db: GPUStateDB):
|
| 191 |
+
self.core_id = core_id
|
| 192 |
+
self.sm_id = sm_id
|
| 193 |
+
self.db = db
|
| 194 |
+
|
| 195 |
+
def load_state(self):
|
| 196 |
+
state = self.db.load_state("core", "core_id", self.core_id)
|
| 197 |
+
return state or {}
|
| 198 |
+
|
| 199 |
+
def save_state(self, state):
|
| 200 |
+
self.db.save_state("core", "core_id", self.core_id, state)
|
| 201 |
+
|
| 202 |
+
def step(self, a, b, cin, opcode, reg_sel):
|
| 203 |
+
state = self.load_state()
|
| 204 |
+
# Simulate a simple operation
|
| 205 |
+
state["last_result"] = (a[0] + b[0] + cin) if opcode == 0b10 else 0.0
|
| 206 |
+
self.save_state(state)
|
| 207 |
+
return state["last_result"]
|
| 208 |
+
|
| 209 |
+
class WarpDB:
|
| 210 |
+
def __init__(self, warp_id, sm_id, db: GPUStateDB, threads_per_warp=700):
|
| 211 |
+
self.warp_id = warp_id
|
| 212 |
+
self.sm_id = sm_id
|
| 213 |
+
self.db = db
|
| 214 |
+
self.threads_per_warp = threads_per_warp
|
| 215 |
+
|
| 216 |
+
def load_state(self):
|
| 217 |
+
state = self.db.load_state("warp", "warp_id", self.warp_id)
|
| 218 |
+
return state or {}
|
| 219 |
+
|
| 220 |
+
def save_state(self, state):
|
| 221 |
+
self.db.save_state("warp", "warp_id", self.warp_id, state)
|
| 222 |
+
|
| 223 |
+
def get_thread(self, thread_id):
|
| 224 |
+
return ThreadDB(thread_id, self.warp_id, self.db)
|
| 225 |
+
|
| 226 |
+
def run(self, a, b, cin, opcode, reg_sel):
|
| 227 |
+
# For demo, run only first thread
|
| 228 |
+
thread = self.get_thread(0)
|
| 229 |
+
result = thread.run(a, b, cin, opcode, reg_sel)
|
| 230 |
+
return [result]
|
| 231 |
+
|
| 232 |
+
class ThreadDB:
|
| 233 |
+
def __init__(self, thread_id, warp_id, db: GPUStateDB):
|
| 234 |
+
self.thread_id = thread_id
|
| 235 |
+
self.warp_id = warp_id
|
| 236 |
+
self.db = db
|
| 237 |
+
|
| 238 |
+
def load_state(self):
|
| 239 |
+
state = self.db.load_state("thread", "thread_id", self.thread_id)
|
| 240 |
+
return state or {}
|
| 241 |
+
|
| 242 |
+
def save_state(self, state):
|
| 243 |
+
self.db.save_state("thread", "thread_id", self.thread_id, state)
|
| 244 |
+
|
| 245 |
+
def run(self, a, b, cin, opcode, reg_sel):
|
| 246 |
+
state = self.load_state()
|
| 247 |
+
# Simulate a simple operation
|
| 248 |
+
state["result"] = (a[0] + b[0] + cin) if opcode == 0b10 else 0.0
|
| 249 |
+
self.save_state(state)
|
| 250 |
+
return state["result"]
|
| 251 |
+
|
| 252 |
+
def attach_global_mem(self, global_mem):
|
| 253 |
+
self.global_mem = global_mem
|
| 254 |
+
|
| 255 |
+
def run_next_warp(self, a, b, cin, opcode, reg_sel):
|
| 256 |
+
warp = self.scheduler.schedule()
|
| 257 |
+
if warp:
|
| 258 |
+
return warp.run(a, b, cin, opcode, reg_sel)
|
| 259 |
+
return None
|
| 260 |
+
|
| 261 |
+
def tensor_core_matmul(self, A, B):
|
| 262 |
+
return self.tensor_cores.matmul(A, B)
|
| 263 |
+
|
| 264 |
+
def tensor_core_matmul_from_memory(self, srcA, addrA, srcB, addrB, shapeA, shapeB):
|
| 265 |
+
return self.tensor_cores.matmul_from_memory(srcA, addrA, srcB, addrB, shapeA, shapeB)
|
| 266 |
+
|
| 267 |
+
def read_register_matrix(self, addr, n, m):
|
| 268 |
+
# Simulate reading an n x m matrix from registers
|
| 269 |
+
# For simplicity, treat addr as row offset
|
| 270 |
+
return [
|
| 271 |
+
[self.register_file[(addr + i) % len(self.register_file)][(j) % len(self.register_file[0])] for j in range(m)]
|
| 272 |
+
for i in range(n)
|
| 273 |
+
]
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
class GPUMemoryHierarchy:
|
| 278 |
+
def __init__(self, num_sms, global_mem_size_bytes, chip_id, db: GPUStateDB):
|
| 279 |
+
self.global_mem = GlobalMemory(global_mem_size_bytes)
|
| 280 |
+
self.sm_ids = list(range(num_sms))
|
| 281 |
+
self.chip_id = chip_id
|
| 282 |
+
self.db = db
|
| 283 |
+
self.num_sms = num_sms
|
| 284 |
+
|
| 285 |
+
def add_sm(self, sm):
|
| 286 |
+
sm.attach_global_mem(self.global_mem)
|
| 287 |
+
|
| 288 |
+
def read_global(self, addr):
|
| 289 |
+
return self.global_mem.read(addr)
|
| 290 |
+
|
| 291 |
+
def write_global(self, addr, value):
|
| 292 |
+
self.global_mem.write(addr, value)
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
class Chip:
|
| 298 |
+
def __init__(self, chip_id, num_sms=1500, vram_size_gb=16, db_path="gpu_state.db", storage=None):
|
| 299 |
+
self.chip_id = chip_id
|
| 300 |
+
self.db = GPUStateDB(db_path)
|
| 301 |
+
# Handle unlimited VRAM case (when vram_size_gb is None)
|
| 302 |
+
global_mem_size_bytes = None if vram_size_gb is None else vram_size_gb * 1024 * 1024 * 1024
|
| 303 |
+
self.gpu_mem = GPUMemoryHierarchy(num_sms=num_sms, global_mem_size_bytes=global_mem_size_bytes, chip_id=chip_id, db=self.db)
|
| 304 |
+
self.sm_ids = list(range(num_sms))
|
| 305 |
+
self.connected_chips = []
|
| 306 |
+
self.storage = storage # Store shared WebSocket storage
|
| 307 |
+
self.ai_accelerator = AIAccelerator(storage=storage) # Pass shared storage to accelerator
|
| 308 |
+
self.custom_vram = CustomVRAM(self.gpu_mem.global_mem) # Create CustomVRAM instance
|
| 309 |
+
self.ai_accelerator.set_vram(self.custom_vram) # Set VRAM for AIAccelerator
|
| 310 |
+
|
| 311 |
+
def get_sm(self, sm_id):
|
| 312 |
+
return StreamingMultiprocessor(sm_id, self.chip_id, self.db)
|
| 313 |
+
|
| 314 |
+
def connect_chip(self, other_chip, interconnect):
|
| 315 |
+
self.connected_chips.append((other_chip, interconnect))
|
| 316 |
+
|
| 317 |
+
def close(self):
|
| 318 |
+
if hasattr(self, "db") and self.db:
|
| 319 |
+
self.db.close()
|
| 320 |
+
if hasattr(self, "gpu_mem") and hasattr(self.gpu_mem, "global_mem") and hasattr(self.gpu_mem.global_mem, "ram"):
|
| 321 |
+
self.gpu_mem.global_mem.ram.close()
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
if __name__ == "__main__":
|
| 325 |
+
print("\n--- Multi-Chip GPU Simulation (DB-backed) ---")
|
| 326 |
+
num_chips = 10
|
| 327 |
+
vram_size_gb = 16
|
| 328 |
+
chips = [Chip(
|
| 329 |
+
chip_id=i,
|
| 330 |
+
num_sms=100,
|
| 331 |
+
vram_size_gb=vram_size_gb,
|
| 332 |
+
db_path=f"gpu_state_chip_{i}.db"
|
| 333 |
+
) for i in range(num_chips)]
|
| 334 |
+
print(f"Total chips: {len(chips)}")
|
| 335 |
+
optical_link = OpticalInterconnect(bandwidth_tbps=800, latency_ns=1)
|
| 336 |
+
for i in range(num_chips):
|
| 337 |
+
chips[i].connect_chip(chips[(i+1)%num_chips], optical_link)
|
| 338 |
+
for chip in chips:
|
| 339 |
+
sm = chip.get_sm(0)
|
| 340 |
+
results = sm.run_next_warp([0.7, 0.0], [0.7, 0.7], 0.0, 0b10, 0)
|
| 341 |
+
print(f"Chip {chip.chip_id} SM 0 first thread result: {results[0] if results else None}")
|
| 342 |
+
# Example tensor core usage: matrix multiply on SM 0, tensor core 0
|
| 343 |
+
A = [[1.0, 2.0], [3.0, 4.0]]
|
| 344 |
+
B = [[5.0, 6.0], [7.0, 8.0]]
|
| 345 |
+
tc_result = sm.tensor_core_matmul(A, B, tensor_core_id=0)
|
| 346 |
+
print(f"Chip {chip.chip_id} SM 0 tensor core 0 matmul result: {tc_result}")
|
| 347 |
+
print(f"Total SMs in first chip: {len(chips[0].sm_ids)}")
|
| 348 |
+
print(f"Global memory size in first chip: {chips[0].gpu_mem.global_mem.size_bytes} bytes (backed by .db)")
|
| 349 |
+
chips[0].send_data(chips[1], optical_link, 1024*1024*1024*10)
|
| 350 |
+
|
| 351 |
+
|
gpu_chip.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from websocket_storage import WebSocketGPUStorage
|
| 2 |
+
from virtual_vram import VirtualVRAM
|
| 3 |
+
from streaming_multiprocessor import StreamingMultiprocessor
|
| 4 |
+
from typing import Dict, Any, List, Optional
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
class GPUChip:
|
| 8 |
+
def __init__(self, chip_id: int, num_sms: int = 108, vram_gb: int = 24, storage=None):
|
| 9 |
+
self.chip_id = chip_id
|
| 10 |
+
self.storage = storage
|
| 11 |
+
if self.storage is None:
|
| 12 |
+
from websocket_storage import WebSocketGPUStorage
|
| 13 |
+
self.storage = WebSocketGPUStorage()
|
| 14 |
+
if not self.storage.wait_for_connection():
|
| 15 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 16 |
+
|
| 17 |
+
# Initialize components with shared storage
|
| 18 |
+
self.vram = VirtualVRAM(vram_gb, storage=self.storage)
|
| 19 |
+
self.sms = [StreamingMultiprocessor(i, storage=self.storage) for i in range(num_sms)]
|
| 20 |
+
|
| 21 |
+
# Initialize chip state
|
| 22 |
+
self.chip_state = {
|
| 23 |
+
"chip_id": chip_id,
|
| 24 |
+
"num_sms": num_sms,
|
| 25 |
+
"vram_gb": vram_gb,
|
| 26 |
+
"pcie_state": {
|
| 27 |
+
"active_transfers": {},
|
| 28 |
+
"bandwidth_usage": 0
|
| 29 |
+
},
|
| 30 |
+
"power_state": {
|
| 31 |
+
"total_watts": 0,
|
| 32 |
+
"sm_power": [0] * num_sms,
|
| 33 |
+
"vram_power": 0
|
| 34 |
+
},
|
| 35 |
+
"memory_controller": {
|
| 36 |
+
"active_requests": {},
|
| 37 |
+
"bandwidth_usage": 0
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
self.store_chip_state()
|
| 41 |
+
|
| 42 |
+
def store_chip_state(self):
|
| 43 |
+
"""Store chip state in WebSocket storage"""
|
| 44 |
+
self.storage.store_state(f"chip_{self.chip_id}", "state", self.chip_state)
|
| 45 |
+
|
| 46 |
+
def allocate_memory(self, size: int, virtual_addr: Optional[str] = None) -> str:
|
| 47 |
+
"""Allocate memory through VRAM"""
|
| 48 |
+
block_id = self.vram.allocate_block(size)
|
| 49 |
+
if virtual_addr:
|
| 50 |
+
self.vram.map_address(virtual_addr, block_id)
|
| 51 |
+
|
| 52 |
+
# Update memory controller state
|
| 53 |
+
self.chip_state["memory_controller"]["active_requests"][block_id] = {
|
| 54 |
+
"type": "allocation",
|
| 55 |
+
"size": size,
|
| 56 |
+
"timestamp": time.time_ns()
|
| 57 |
+
}
|
| 58 |
+
self.store_chip_state()
|
| 59 |
+
|
| 60 |
+
return block_id
|
| 61 |
+
|
| 62 |
+
def transfer_to_device(self, data: bytes, virtual_addr: Optional[str] = None) -> str:
|
| 63 |
+
"""Transfer data to device through PCIe"""
|
| 64 |
+
# Simulate PCIe transfer
|
| 65 |
+
transfer_id = f"transfer_{time.time_ns()}"
|
| 66 |
+
self.chip_state["pcie_state"]["active_transfers"][transfer_id] = {
|
| 67 |
+
"direction": "to_device",
|
| 68 |
+
"size": len(data),
|
| 69 |
+
"timestamp": time.time_ns()
|
| 70 |
+
}
|
| 71 |
+
self.store_chip_state()
|
| 72 |
+
|
| 73 |
+
# Allocate and store in VRAM
|
| 74 |
+
block_id = self.allocate_memory(len(data), virtual_addr)
|
| 75 |
+
self.storage.store_tensor(block_id, data)
|
| 76 |
+
|
| 77 |
+
# Update transfer state
|
| 78 |
+
self.chip_state["pcie_state"]["active_transfers"][transfer_id]["completed"] = True
|
| 79 |
+
self.store_chip_state()
|
| 80 |
+
|
| 81 |
+
return block_id
|
| 82 |
+
|
| 83 |
+
def schedule_compute(self, sm_index: int, warp_state: Dict[str, Any]) -> str:
|
| 84 |
+
"""Schedule computation on an SM"""
|
| 85 |
+
if 0 <= sm_index < len(self.sms):
|
| 86 |
+
warp_id = f"warp_{time.time_ns()}"
|
| 87 |
+
self.sms[sm_index].schedule_warp(warp_id, warp_state)
|
| 88 |
+
|
| 89 |
+
# Update power state
|
| 90 |
+
self.chip_state["power_state"]["sm_power"][sm_index] += 10 # Simulate power increase
|
| 91 |
+
self.chip_state["power_state"]["total_watts"] = sum(self.chip_state["power_state"]["sm_power"])
|
| 92 |
+
self.store_chip_state()
|
| 93 |
+
|
| 94 |
+
return warp_id
|
| 95 |
+
raise ValueError(f"Invalid SM index: {sm_index}")
|
| 96 |
+
|
| 97 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 98 |
+
"""Get comprehensive chip statistics"""
|
| 99 |
+
stats = {
|
| 100 |
+
"chip_id": self.chip_id,
|
| 101 |
+
"vram": self.vram.get_stats(),
|
| 102 |
+
"sms": [sm.get_stats() for sm in self.sms],
|
| 103 |
+
"pcie": {
|
| 104 |
+
"active_transfers": len(self.chip_state["pcie_state"]["active_transfers"]),
|
| 105 |
+
"bandwidth_usage": self.chip_state["pcie_state"]["bandwidth_usage"]
|
| 106 |
+
},
|
| 107 |
+
"power": {
|
| 108 |
+
"total_watts": self.chip_state["power_state"]["total_watts"],
|
| 109 |
+
"vram_watts": self.chip_state["power_state"]["vram_power"]
|
| 110 |
+
},
|
| 111 |
+
"memory_controller": {
|
| 112 |
+
"active_requests": len(self.chip_state["memory_controller"]["active_requests"]),
|
| 113 |
+
"bandwidth_usage": self.chip_state["memory_controller"]["bandwidth_usage"]
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
return stats
|
gpu_state_db.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import json
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
class GPUStateDB:
|
| 6 |
+
def __init__(self, db_path='gpu_state.db'):
|
| 7 |
+
self.conn = sqlite3.connect(db_path, check_same_thread=False)
|
| 8 |
+
self.lock = threading.Lock()
|
| 9 |
+
self._init_tables()
|
| 10 |
+
|
| 11 |
+
def _init_tables(self):
|
| 12 |
+
with self.lock:
|
| 13 |
+
c = self.conn.cursor()
|
| 14 |
+
c.execute('''CREATE TABLE IF NOT EXISTS sm (
|
| 15 |
+
sm_id INTEGER PRIMARY KEY,
|
| 16 |
+
chip_id INTEGER,
|
| 17 |
+
state_json TEXT
|
| 18 |
+
)''')
|
| 19 |
+
c.execute('''CREATE TABLE IF NOT EXISTS core (
|
| 20 |
+
core_id INTEGER PRIMARY KEY,
|
| 21 |
+
sm_id INTEGER,
|
| 22 |
+
registers BLOB,
|
| 23 |
+
state_json TEXT
|
| 24 |
+
)''')
|
| 25 |
+
c.execute('''CREATE TABLE IF NOT EXISTS warp (
|
| 26 |
+
warp_id INTEGER PRIMARY KEY,
|
| 27 |
+
sm_id INTEGER,
|
| 28 |
+
thread_ids TEXT,
|
| 29 |
+
state_json TEXT
|
| 30 |
+
)''')
|
| 31 |
+
c.execute('''CREATE TABLE IF NOT EXISTS thread (
|
| 32 |
+
thread_id INTEGER PRIMARY KEY,
|
| 33 |
+
warp_id INTEGER,
|
| 34 |
+
core_id INTEGER,
|
| 35 |
+
state_json TEXT
|
| 36 |
+
)''')
|
| 37 |
+
c.execute('''CREATE TABLE IF NOT EXISTS tensor_core (
|
| 38 |
+
tensor_core_id INTEGER PRIMARY KEY,
|
| 39 |
+
sm_id INTEGER,
|
| 40 |
+
memory BLOB,
|
| 41 |
+
state_json TEXT
|
| 42 |
+
)''')
|
| 43 |
+
self.conn.commit()
|
| 44 |
+
|
| 45 |
+
def save_state(self, table, id_name, id_value, state):
|
| 46 |
+
state_json = json.dumps(state)
|
| 47 |
+
with self.lock:
|
| 48 |
+
self.conn.execute(f"INSERT OR REPLACE INTO {table} ({id_name}, state_json) VALUES (?, ?)", (id_value, state_json))
|
| 49 |
+
self.conn.commit()
|
| 50 |
+
|
| 51 |
+
def load_state(self, table, id_name, id_value):
|
| 52 |
+
with self.lock:
|
| 53 |
+
cur = self.conn.execute(f"SELECT state_json FROM {table} WHERE {id_name}=?", (id_value,))
|
| 54 |
+
row = cur.fetchone()
|
| 55 |
+
return json.loads(row[0]) if row else None
|
| 56 |
+
|
| 57 |
+
def close(self):
|
| 58 |
+
if self.conn:
|
| 59 |
+
self.conn.close()
|
| 60 |
+
self.conn = None
|
http_storage.py
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
+
from typing import Dict, Any, Optional, Union
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
+
import hashlib
|
| 8 |
+
import logging
|
| 9 |
+
from requests.adapters import HTTPAdapter
|
| 10 |
+
from urllib3.util.retry import Retry
|
| 11 |
+
|
| 12 |
+
class HTTPGPUStorage:
|
| 13 |
+
"""
|
| 14 |
+
HTTP-based GPU storage client that replaces WebSocket functionality.
|
| 15 |
+
Maintains the same interface as WebSocketGPUStorage for backward compatibility.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# Singleton instance
|
| 19 |
+
_instance = None
|
| 20 |
+
_lock = threading.Lock()
|
| 21 |
+
|
| 22 |
+
def __new__(cls, base_url: str = "http://localhost:7860"):
|
| 23 |
+
with cls._lock:
|
| 24 |
+
if cls._instance is None:
|
| 25 |
+
cls._instance = super().__new__(cls)
|
| 26 |
+
cls._instance._init_singleton(base_url)
|
| 27 |
+
return cls._instance
|
| 28 |
+
|
| 29 |
+
def _init_singleton(self, base_url: str):
|
| 30 |
+
"""Initialize the singleton instance"""
|
| 31 |
+
if hasattr(self, 'initialized'):
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
self.base_url = base_url.rstrip('/')
|
| 35 |
+
self.api_base = f"{self.base_url}/api/v1"
|
| 36 |
+
self.session_token = None
|
| 37 |
+
self.session_id = None
|
| 38 |
+
self.lock = threading.Lock()
|
| 39 |
+
self._closing = False
|
| 40 |
+
self.error_count = 0
|
| 41 |
+
self.last_error_time = 0
|
| 42 |
+
self.max_retries = 5
|
| 43 |
+
|
| 44 |
+
# Tensor and model registries (maintained for compatibility)
|
| 45 |
+
self.tensor_registry: Dict[str, Dict[str, Any]] = {}
|
| 46 |
+
self.model_registry: Dict[str, Dict[str, Any]] = {}
|
| 47 |
+
self.resource_monitor = {
|
| 48 |
+
'vram_used': 0,
|
| 49 |
+
'active_tensors': 0,
|
| 50 |
+
'loaded_models': set()
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Configure HTTP session with connection pooling and retries
|
| 54 |
+
self.http_session = requests.Session()
|
| 55 |
+
|
| 56 |
+
# Configure retry strategy
|
| 57 |
+
retry_strategy = Retry(
|
| 58 |
+
total=3,
|
| 59 |
+
status_forcelist=[429, 500, 502, 503, 504],
|
| 60 |
+
allowed_methods=["HEAD", "GET", "OPTIONS", "POST", "PUT", "DELETE"], # Updated parameter name
|
| 61 |
+
backoff_factor=1
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
adapter = HTTPAdapter(
|
| 65 |
+
max_retries=retry_strategy,
|
| 66 |
+
pool_connections=10,
|
| 67 |
+
pool_maxsize=20
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
self.http_session.mount("http://", adapter)
|
| 71 |
+
self.http_session.mount("https://", adapter)
|
| 72 |
+
|
| 73 |
+
# Set default headers
|
| 74 |
+
self.http_session.headers.update({
|
| 75 |
+
'Content-Type': 'application/json',
|
| 76 |
+
'User-Agent': 'VirtualGPU-HTTP-Client/2.0'
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
# Initialize session
|
| 80 |
+
self._create_session()
|
| 81 |
+
self.initialized = True
|
| 82 |
+
|
| 83 |
+
def __init__(self, base_url: str = "http://localhost:7860"):
|
| 84 |
+
"""This will actually just return the singleton instance"""
|
| 85 |
+
pass
|
| 86 |
+
|
| 87 |
+
def _create_session(self):
|
| 88 |
+
"""Create HTTP session with the server"""
|
| 89 |
+
try:
|
| 90 |
+
response = self.http_session.post(
|
| 91 |
+
f"{self.api_base}/sessions",
|
| 92 |
+
json={"client_id": "virtual_gpu_client"},
|
| 93 |
+
timeout=30
|
| 94 |
+
)
|
| 95 |
+
response.raise_for_status()
|
| 96 |
+
|
| 97 |
+
session_data = response.json()
|
| 98 |
+
self.session_token = session_data['session_token']
|
| 99 |
+
self.session_id = session_data['session_id']
|
| 100 |
+
|
| 101 |
+
# Update session headers
|
| 102 |
+
self.http_session.headers.update({
|
| 103 |
+
'Authorization': f'Bearer {self.session_token}'
|
| 104 |
+
})
|
| 105 |
+
|
| 106 |
+
logging.info(f"HTTP session created: {self.session_id}")
|
| 107 |
+
return True
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logging.error(f"Failed to create HTTP session: {e}")
|
| 111 |
+
self.error_count += 1
|
| 112 |
+
self.last_error_time = time.time()
|
| 113 |
+
return False
|
| 114 |
+
|
| 115 |
+
def _make_request(self, method: str, endpoint: str, **kwargs) -> Optional[Dict[str, Any]]:
|
| 116 |
+
"""Make HTTP request with error handling and retries"""
|
| 117 |
+
if self._closing:
|
| 118 |
+
return {"status": "error", "message": "HTTP client is closing"}
|
| 119 |
+
|
| 120 |
+
url = f"{self.api_base}{endpoint}"
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Ensure we have a valid session
|
| 124 |
+
if not self.session_token:
|
| 125 |
+
if not self._create_session():
|
| 126 |
+
return {"status": "error", "message": "Failed to create session"}
|
| 127 |
+
|
| 128 |
+
response = self.http_session.request(method, url, timeout=30, **kwargs)
|
| 129 |
+
|
| 130 |
+
# Handle authentication errors by recreating session
|
| 131 |
+
if response.status_code == 401:
|
| 132 |
+
logging.warning("Session expired, recreating...")
|
| 133 |
+
if self._create_session():
|
| 134 |
+
response = self.http_session.request(method, url, timeout=30, **kwargs)
|
| 135 |
+
else:
|
| 136 |
+
return {"status": "error", "message": "Failed to recreate session"}
|
| 137 |
+
|
| 138 |
+
response.raise_for_status()
|
| 139 |
+
|
| 140 |
+
# Reset error count on successful request
|
| 141 |
+
self.error_count = 0
|
| 142 |
+
|
| 143 |
+
return response.json()
|
| 144 |
+
|
| 145 |
+
except requests.exceptions.RequestException as e:
|
| 146 |
+
self.error_count += 1
|
| 147 |
+
self.last_error_time = time.time()
|
| 148 |
+
logging.error(f"HTTP request failed: {e}")
|
| 149 |
+
return {"status": "error", "message": f"HTTP request failed: {str(e)}"}
|
| 150 |
+
except Exception as e:
|
| 151 |
+
self.error_count += 1
|
| 152 |
+
self.last_error_time = time.time()
|
| 153 |
+
logging.error(f"Unexpected error in HTTP request: {e}")
|
| 154 |
+
return {"status": "error", "message": f"Unexpected error: {str(e)}"}
|
| 155 |
+
|
| 156 |
+
def store_tensor(self, tensor_id: str, data: np.ndarray, model_size: Optional[int] = None) -> bool:
|
| 157 |
+
"""Store tensor data via HTTP API"""
|
| 158 |
+
try:
|
| 159 |
+
if data is None:
|
| 160 |
+
raise ValueError("Cannot store None tensor")
|
| 161 |
+
|
| 162 |
+
# Calculate tensor metadata
|
| 163 |
+
tensor_shape = data.shape
|
| 164 |
+
tensor_dtype = str(data.dtype)
|
| 165 |
+
tensor_size = data.nbytes
|
| 166 |
+
|
| 167 |
+
request_data = {
|
| 168 |
+
"data": data.tolist(),
|
| 169 |
+
"metadata": {
|
| 170 |
+
'shape': tensor_shape,
|
| 171 |
+
'dtype': tensor_dtype,
|
| 172 |
+
'size': tensor_size,
|
| 173 |
+
'timestamp': time.time()
|
| 174 |
+
},
|
| 175 |
+
"model_size": model_size if model_size is not None else -1
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
response = self._make_request(
|
| 179 |
+
'POST',
|
| 180 |
+
f'/vram/blocks/{tensor_id}',
|
| 181 |
+
json=request_data
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
if response and response.get('status') == 'success':
|
| 185 |
+
# Update tensor registry
|
| 186 |
+
with self.lock:
|
| 187 |
+
self.tensor_registry[tensor_id] = {
|
| 188 |
+
'shape': tensor_shape,
|
| 189 |
+
'dtype': tensor_dtype,
|
| 190 |
+
'size': tensor_size,
|
| 191 |
+
'timestamp': time.time()
|
| 192 |
+
}
|
| 193 |
+
self.resource_monitor['vram_used'] += tensor_size
|
| 194 |
+
self.resource_monitor['active_tensors'] += 1
|
| 195 |
+
return True
|
| 196 |
+
else:
|
| 197 |
+
logging.error(f"Failed to store tensor {tensor_id}: {response.get('message', 'Unknown error')}")
|
| 198 |
+
return False
|
| 199 |
+
|
| 200 |
+
except Exception as e:
|
| 201 |
+
logging.error(f"Error storing tensor {tensor_id}: {str(e)}")
|
| 202 |
+
return False
|
| 203 |
+
|
| 204 |
+
def load_tensor(self, tensor_id: str) -> Optional[np.ndarray]:
|
| 205 |
+
"""Load tensor data via HTTP API"""
|
| 206 |
+
try:
|
| 207 |
+
# Check tensor registry first
|
| 208 |
+
if tensor_id not in self.tensor_registry:
|
| 209 |
+
logging.warning(f"Tensor {tensor_id} not registered in VRAM")
|
| 210 |
+
# Still try to load it in case it exists on server
|
| 211 |
+
|
| 212 |
+
response = self._make_request('GET', f'/vram/blocks/{tensor_id}')
|
| 213 |
+
|
| 214 |
+
if response and response.get('status') == 'success':
|
| 215 |
+
data = response.get('data')
|
| 216 |
+
metadata = response.get('metadata', {})
|
| 217 |
+
|
| 218 |
+
if data is None:
|
| 219 |
+
logging.error(f"No data found for tensor {tensor_id}")
|
| 220 |
+
return None
|
| 221 |
+
|
| 222 |
+
try:
|
| 223 |
+
# Convert to numpy array with correct dtype
|
| 224 |
+
expected_dtype = metadata.get('dtype', 'float32')
|
| 225 |
+
expected_shape = metadata.get('shape')
|
| 226 |
+
|
| 227 |
+
arr = np.array(data, dtype=np.dtype(expected_dtype))
|
| 228 |
+
if expected_shape and arr.shape != tuple(expected_shape):
|
| 229 |
+
arr = arr.reshape(expected_shape)
|
| 230 |
+
|
| 231 |
+
# Update registry if not present
|
| 232 |
+
if tensor_id not in self.tensor_registry:
|
| 233 |
+
with self.lock:
|
| 234 |
+
self.tensor_registry[tensor_id] = metadata
|
| 235 |
+
|
| 236 |
+
return arr
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
logging.error(f"Error converting tensor data: {str(e)}")
|
| 240 |
+
return None
|
| 241 |
+
else:
|
| 242 |
+
logging.error(f"Failed to load tensor {tensor_id}: {response.get('message', 'Unknown error')}")
|
| 243 |
+
return None
|
| 244 |
+
|
| 245 |
+
except Exception as e:
|
| 246 |
+
logging.error(f"Error loading tensor {tensor_id}: {str(e)}")
|
| 247 |
+
return None
|
| 248 |
+
|
| 249 |
+
def store_state(self, component: str, state_id: str, state_data: Dict[str, Any]) -> bool:
|
| 250 |
+
"""Store component state via HTTP API"""
|
| 251 |
+
try:
|
| 252 |
+
request_data = {
|
| 253 |
+
"data": state_data,
|
| 254 |
+
"timestamp": time.time()
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
response = self._make_request(
|
| 258 |
+
'POST',
|
| 259 |
+
f'/state/{component}/{state_id}',
|
| 260 |
+
json=request_data
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
if response and response.get('status') == 'success':
|
| 264 |
+
return True
|
| 265 |
+
else:
|
| 266 |
+
logging.error(f"Failed to store state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
|
| 267 |
+
return False
|
| 268 |
+
|
| 269 |
+
except Exception as e:
|
| 270 |
+
logging.error(f"Error storing state for {component}/{state_id}: {str(e)}")
|
| 271 |
+
return False
|
| 272 |
+
|
| 273 |
+
def load_state(self, component: str, state_id: str) -> Optional[Dict[str, Any]]:
|
| 274 |
+
"""Load component state via HTTP API"""
|
| 275 |
+
try:
|
| 276 |
+
response = self._make_request('GET', f'/state/{component}/{state_id}')
|
| 277 |
+
|
| 278 |
+
if response and response.get('status') == 'success':
|
| 279 |
+
return response.get('data')
|
| 280 |
+
else:
|
| 281 |
+
logging.error(f"Failed to load state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
|
| 282 |
+
return None
|
| 283 |
+
|
| 284 |
+
except Exception as e:
|
| 285 |
+
logging.error(f"Error loading state for {component}/{state_id}: {str(e)}")
|
| 286 |
+
return None
|
| 287 |
+
|
| 288 |
+
def cache_data(self, key: str, data: Any) -> bool:
|
| 289 |
+
"""Cache data via HTTP API"""
|
| 290 |
+
try:
|
| 291 |
+
request_data = {"data": data}
|
| 292 |
+
|
| 293 |
+
response = self._make_request(
|
| 294 |
+
'POST',
|
| 295 |
+
f'/cache/{key}',
|
| 296 |
+
json=request_data
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
return response and response.get('status') == 'success'
|
| 300 |
+
|
| 301 |
+
except Exception as e:
|
| 302 |
+
logging.error(f"Error caching data for key {key}: {str(e)}")
|
| 303 |
+
return False
|
| 304 |
+
|
| 305 |
+
def get_cached_data(self, key: str) -> Optional[Any]:
|
| 306 |
+
"""Get cached data via HTTP API"""
|
| 307 |
+
try:
|
| 308 |
+
response = self._make_request('GET', f'/cache/{key}')
|
| 309 |
+
|
| 310 |
+
if response and response.get('status') == 'success':
|
| 311 |
+
return response.get('data')
|
| 312 |
+
return None
|
| 313 |
+
|
| 314 |
+
except Exception as e:
|
| 315 |
+
logging.error(f"Error getting cached data for key {key}: {str(e)}")
|
| 316 |
+
return None
|
| 317 |
+
|
| 318 |
+
def is_model_loaded(self, model_name: str) -> bool:
|
| 319 |
+
"""Check if a model is loaded via HTTP API"""
|
| 320 |
+
try:
|
| 321 |
+
response = self._make_request('GET', f'/models/{model_name}/status')
|
| 322 |
+
|
| 323 |
+
if response and response.get('status') == 'loaded':
|
| 324 |
+
return True
|
| 325 |
+
return False
|
| 326 |
+
|
| 327 |
+
except Exception as e:
|
| 328 |
+
logging.error(f"Error checking model status for {model_name}: {str(e)}")
|
| 329 |
+
return False
|
| 330 |
+
|
| 331 |
+
def load_model(self, model_name: str, model_path: Optional[str] = None, model_data: Optional[Dict] = None) -> bool:
|
| 332 |
+
"""Load a model via HTTP API"""
|
| 333 |
+
try:
|
| 334 |
+
# Check if model is already loaded
|
| 335 |
+
if self.is_model_loaded(model_name):
|
| 336 |
+
logging.info(f"Model {model_name} already loaded")
|
| 337 |
+
return True
|
| 338 |
+
|
| 339 |
+
# Calculate model hash if path provided
|
| 340 |
+
model_hash = None
|
| 341 |
+
if model_path:
|
| 342 |
+
model_hash = self._calculate_model_hash(model_path)
|
| 343 |
+
|
| 344 |
+
request_data = {
|
| 345 |
+
"model_data": model_data,
|
| 346 |
+
"model_path": model_path,
|
| 347 |
+
"model_hash": model_hash
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
response = self._make_request(
|
| 351 |
+
'POST',
|
| 352 |
+
f'/models/{model_name}/load',
|
| 353 |
+
json=request_data
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
if response and response.get('status') == 'success':
|
| 357 |
+
with self.lock:
|
| 358 |
+
self.model_registry[model_name] = {
|
| 359 |
+
'hash': model_hash,
|
| 360 |
+
'timestamp': time.time(),
|
| 361 |
+
'model_data': model_data
|
| 362 |
+
}
|
| 363 |
+
self.resource_monitor['loaded_models'].add(model_name)
|
| 364 |
+
logging.info(f"Successfully loaded model {model_name}")
|
| 365 |
+
return True
|
| 366 |
+
else:
|
| 367 |
+
logging.error(f"Failed to load model {model_name}: {response.get('message', 'Unknown error')}")
|
| 368 |
+
return False
|
| 369 |
+
|
| 370 |
+
except Exception as e:
|
| 371 |
+
logging.error(f"Error loading model {model_name}: {str(e)}")
|
| 372 |
+
return False
|
| 373 |
+
|
| 374 |
+
def _calculate_model_hash(self, model_path: str) -> str:
|
| 375 |
+
"""Calculate SHA256 hash of model file"""
|
| 376 |
+
try:
|
| 377 |
+
sha256_hash = hashlib.sha256()
|
| 378 |
+
with open(model_path, "rb") as f:
|
| 379 |
+
for byte_block in iter(lambda: f.read(4096), b""):
|
| 380 |
+
sha256_hash.update(byte_block)
|
| 381 |
+
return sha256_hash.hexdigest()
|
| 382 |
+
except Exception as e:
|
| 383 |
+
logging.error(f"Error calculating model hash: {str(e)}")
|
| 384 |
+
return ""
|
| 385 |
+
|
| 386 |
+
def start_inference(self, model_name: str, input_data: np.ndarray) -> Optional[Dict[str, Any]]:
|
| 387 |
+
"""Start inference with a loaded model via HTTP API"""
|
| 388 |
+
try:
|
| 389 |
+
if not self.is_model_loaded(model_name):
|
| 390 |
+
logging.error(f"Model {model_name} not loaded. Please load the model first.")
|
| 391 |
+
return None
|
| 392 |
+
|
| 393 |
+
request_data = {
|
| 394 |
+
"input_data": input_data.tolist() if isinstance(input_data, np.ndarray) else input_data
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
response = self._make_request(
|
| 398 |
+
'POST',
|
| 399 |
+
f'/models/{model_name}/inference',
|
| 400 |
+
json=request_data
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
if response and response.get('status') == 'success':
|
| 404 |
+
return {
|
| 405 |
+
'output': np.array(response['output']) if 'output' in response else None,
|
| 406 |
+
'metrics': response.get('metrics', {}),
|
| 407 |
+
'model_info': self.model_registry.get(model_name, {})
|
| 408 |
+
}
|
| 409 |
+
else:
|
| 410 |
+
logging.error(f"Inference failed for model {model_name}: {response.get('message', 'Unknown error')}")
|
| 411 |
+
return None
|
| 412 |
+
|
| 413 |
+
except Exception as e:
|
| 414 |
+
logging.error(f"Error during inference for model {model_name}: {str(e)}")
|
| 415 |
+
return None
|
| 416 |
+
|
| 417 |
+
def wait_for_connection(self, timeout: float = 30.0) -> bool:
|
| 418 |
+
"""Wait for HTTP connection to be established (compatibility method)"""
|
| 419 |
+
# For HTTP, we just check if we can make a request
|
| 420 |
+
try:
|
| 421 |
+
if not self.session_token:
|
| 422 |
+
return self._create_session()
|
| 423 |
+
|
| 424 |
+
# Test connection with a simple request
|
| 425 |
+
response = self._make_request('GET', '/cache/connection_test')
|
| 426 |
+
return response is not None
|
| 427 |
+
|
| 428 |
+
except Exception as e:
|
| 429 |
+
logging.error(f"Connection test failed: {e}")
|
| 430 |
+
return False
|
| 431 |
+
|
| 432 |
+
def is_connected(self) -> bool:
|
| 433 |
+
"""Check if HTTP connection is active (compatibility method)"""
|
| 434 |
+
return self.session_token is not None and not self._closing
|
| 435 |
+
|
| 436 |
+
def get_connection_status(self) -> Dict[str, Any]:
|
| 437 |
+
"""Get detailed connection status"""
|
| 438 |
+
return {
|
| 439 |
+
"connected": self.is_connected(),
|
| 440 |
+
"closing": self._closing,
|
| 441 |
+
"error_count": self.error_count,
|
| 442 |
+
"base_url": self.base_url,
|
| 443 |
+
"last_error_time": self.last_error_time,
|
| 444 |
+
"loaded_models": list(self.resource_monitor['loaded_models']),
|
| 445 |
+
"session_id": self.session_id
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
def set_keep_alive(self, enabled: bool):
|
| 449 |
+
"""Set keep-alive mode (compatibility method for HTTP)"""
|
| 450 |
+
# HTTP connections are stateless, so this is a no-op
|
| 451 |
+
pass
|
| 452 |
+
|
| 453 |
+
def reconnect(self):
|
| 454 |
+
"""Reconnect to server (recreate session for HTTP)"""
|
| 455 |
+
self.session_token = None
|
| 456 |
+
self.session_id = None
|
| 457 |
+
return self._create_session()
|
| 458 |
+
|
| 459 |
+
def close(self):
|
| 460 |
+
"""Close HTTP client"""
|
| 461 |
+
self._closing = True
|
| 462 |
+
if self.http_session:
|
| 463 |
+
self.http_session.close()
|
| 464 |
+
|
| 465 |
+
# Additional methods for multi-chip coordination
|
| 466 |
+
def transfer_between_chips(self, src_chip: int, dst_chip: int, data_id: str) -> Optional[str]:
|
| 467 |
+
"""Transfer data between chips via HTTP API"""
|
| 468 |
+
try:
|
| 469 |
+
request_data = {"data_id": data_id}
|
| 470 |
+
|
| 471 |
+
response = self._make_request(
|
| 472 |
+
'POST',
|
| 473 |
+
f'/chips/{src_chip}/transfer/{dst_chip}',
|
| 474 |
+
json=request_data
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
if response and response.get('status') == 'success':
|
| 478 |
+
return response.get('new_data_id')
|
| 479 |
+
else:
|
| 480 |
+
logging.error(f"Chip transfer failed: {response.get('message', 'Unknown error')}")
|
| 481 |
+
return None
|
| 482 |
+
|
| 483 |
+
except Exception as e:
|
| 484 |
+
logging.error(f"Error in chip transfer: {str(e)}")
|
| 485 |
+
return None
|
| 486 |
+
|
| 487 |
+
def create_sync_barrier(self, barrier_id: str, num_participants: int) -> bool:
|
| 488 |
+
"""Create synchronization barrier via HTTP API"""
|
| 489 |
+
try:
|
| 490 |
+
request_data = {"num_participants": num_participants}
|
| 491 |
+
|
| 492 |
+
response = self._make_request(
|
| 493 |
+
'POST',
|
| 494 |
+
f'/sync/barrier/{barrier_id}',
|
| 495 |
+
json=request_data
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
return response and response.get('status') == 'success'
|
| 499 |
+
|
| 500 |
+
except Exception as e:
|
| 501 |
+
logging.error(f"Error creating sync barrier: {str(e)}")
|
| 502 |
+
return False
|
| 503 |
+
|
| 504 |
+
def wait_sync_barrier(self, barrier_id: str) -> bool:
|
| 505 |
+
"""Wait at synchronization barrier via HTTP API"""
|
| 506 |
+
try:
|
| 507 |
+
response = self._make_request('PUT', f'/sync/barrier/{barrier_id}/wait')
|
| 508 |
+
|
| 509 |
+
if response:
|
| 510 |
+
status = response.get('status')
|
| 511 |
+
if status == 'released':
|
| 512 |
+
return True
|
| 513 |
+
elif status == 'waiting':
|
| 514 |
+
# In a real implementation, this might poll or use long-polling
|
| 515 |
+
time.sleep(0.1) # Brief delay before next check
|
| 516 |
+
return False
|
| 517 |
+
|
| 518 |
+
return False
|
| 519 |
+
|
| 520 |
+
except Exception as e:
|
| 521 |
+
logging.error(f"Error waiting at sync barrier: {str(e)}")
|
| 522 |
+
return False
|
| 523 |
+
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hyperrealistic voltage-based logic gates for digital simulation.
|
| 3 |
+
Each gate operates on analog voltages, with digital 1/0 determined by thresholding.
|
| 4 |
+
Gate switching speed is parameterized to match target transistor switching rates.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import random
|
| 8 |
+
|
| 9 |
+
# Constants for voltage logic
|
| 10 |
+
VDD = 0.7 # High voltage (V)
|
| 11 |
+
VSS = 0.0 # Low voltage (V)
|
| 12 |
+
VTH = 0.35 # Threshold voltage (V)
|
| 13 |
+
|
| 14 |
+
# Gate switching delay (in seconds) to match fastest possible switching
|
| 15 |
+
# This should be the minimum possible, based on electron_speed.py calculation
|
| 16 |
+
from electron_speed import max_switch_freq
|
| 17 |
+
GATE_DELAY = 1 / max_switch_freq # seconds per switch (theoretical limit)
|
| 18 |
+
|
| 19 |
+
class LogicGate:
|
| 20 |
+
def __init__(self, vdd=VDD, vss=VSS, vth=VTH, delay=GATE_DELAY):
|
| 21 |
+
self.vdd = vdd
|
| 22 |
+
self.vss = vss
|
| 23 |
+
self.vth = vth
|
| 24 |
+
self.delay = delay
|
| 25 |
+
|
| 26 |
+
def interpret(self, voltage):
|
| 27 |
+
"""Return digital 1 if voltage > Vth, else 0."""
|
| 28 |
+
return 1 if voltage > self.vth else 0
|
| 29 |
+
|
| 30 |
+
def voltage(self, bit):
|
| 31 |
+
"""Return voltage for digital bit."""
|
| 32 |
+
return self.vdd if bit else self.vss
|
| 33 |
+
|
| 34 |
+
class NANDGate(LogicGate):
|
| 35 |
+
def output(self, vin1, vin2):
|
| 36 |
+
# Interpret inputs as digital
|
| 37 |
+
in1 = self.interpret(vin1)
|
| 38 |
+
in2 = self.interpret(vin2)
|
| 39 |
+
# NAND logic: output is high unless both inputs are high
|
| 40 |
+
out_bit = 0 if (in1 and in2) else 1
|
| 41 |
+
# Add random noise for realism
|
| 42 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 43 |
+
return self.voltage(out_bit) + noise
|
| 44 |
+
|
| 45 |
+
class ANDGate(LogicGate):
|
| 46 |
+
def output(self, vin1, vin2):
|
| 47 |
+
in1 = self.interpret(vin1)
|
| 48 |
+
in2 = self.interpret(vin2)
|
| 49 |
+
out_bit = 1 if (in1 and in2) else 0
|
| 50 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 51 |
+
return self.voltage(out_bit) + noise
|
| 52 |
+
|
| 53 |
+
class ORGate(LogicGate):
|
| 54 |
+
def output(self, vin1, vin2):
|
| 55 |
+
in1 = self.interpret(vin1)
|
| 56 |
+
in2 = self.interpret(vin2)
|
| 57 |
+
out_bit = 1 if (in1 or in2) else 0
|
| 58 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 59 |
+
return self.voltage(out_bit) + noise
|
| 60 |
+
|
| 61 |
+
class NOTGate(LogicGate):
|
| 62 |
+
def output(self, vin):
|
| 63 |
+
in_bit = self.interpret(vin)
|
| 64 |
+
out_bit = 0 if in_bit else 1
|
| 65 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 66 |
+
return self.voltage(out_bit) + noise
|
| 67 |
+
|
| 68 |
+
# Example usage and test
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
nand = NANDGate()
|
| 71 |
+
andg = ANDGate()
|
| 72 |
+
org = ORGate()
|
| 73 |
+
notg = NOTGate()
|
| 74 |
+
print("NAND(0.7, 0.7):", nand.output(0.7, 0.7))
|
| 75 |
+
print("AND(0.7, 0.7):", andg.output(0.7, 0.7))
|
| 76 |
+
print("OR(0.0, 0.7):", org.output(0.0, 0.7))
|
| 77 |
+
print("NOT(0.7):", notg.output(0.7))
|
| 78 |
+
print(f"Gate delay (s): {GATE_DELAY:.2e}")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# --- Combinational Logic ---
|
| 82 |
+
class XORGate(LogicGate):
|
| 83 |
+
def output(self, vin1, vin2):
|
| 84 |
+
in1 = self.interpret(vin1)
|
| 85 |
+
in2 = self.interpret(vin2)
|
| 86 |
+
out_bit = 1 if (in1 != in2) else 0
|
| 87 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 88 |
+
return self.voltage(out_bit) + noise
|
| 89 |
+
|
| 90 |
+
class NORGate(LogicGate):
|
| 91 |
+
def output(self, vin1, vin2):
|
| 92 |
+
in1 = self.interpret(vin1)
|
| 93 |
+
in2 = self.interpret(vin2)
|
| 94 |
+
out_bit = 0 if (in1 or in2) else 1
|
| 95 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 96 |
+
return self.voltage(out_bit) + noise
|
| 97 |
+
|
| 98 |
+
class XNORGate(LogicGate):
|
| 99 |
+
def output(self, vin1, vin2):
|
| 100 |
+
in1 = self.interpret(vin1)
|
| 101 |
+
in2 = self.interpret(vin2)
|
| 102 |
+
out_bit = 1 if (in1 == in2) else 0
|
| 103 |
+
noise = random.gauss(0, 0.01 * self.vdd)
|
| 104 |
+
return self.voltage(out_bit) + noise
|
| 105 |
+
|
| 106 |
+
# Example: 1-bit Full Adder (combinational logic)
|
| 107 |
+
class FullAdder:
|
| 108 |
+
def __init__(self):
|
| 109 |
+
self.xor1 = XORGate()
|
| 110 |
+
self.xor2 = XORGate()
|
| 111 |
+
self.and1 = ANDGate()
|
| 112 |
+
self.and2 = ANDGate()
|
| 113 |
+
self.or1 = ORGate()
|
| 114 |
+
|
| 115 |
+
def output(self, a, b, cin):
|
| 116 |
+
sum1 = self.xor1.output(a, b)
|
| 117 |
+
sum_bit = self.xor2.output(sum1, cin)
|
| 118 |
+
carry1 = self.and1.output(a, b)
|
| 119 |
+
carry2 = self.and2.output(sum1, cin)
|
| 120 |
+
cout = self.or1.output(carry1, carry2)
|
| 121 |
+
return sum_bit, cout
|
| 122 |
+
|
| 123 |
+
# --- Sequential Logic ---
|
| 124 |
+
# SR, D, JK, T Flip-Flops (voltage-based, using gates)
|
| 125 |
+
class SRFlipFlop:
|
| 126 |
+
def __init__(self):
|
| 127 |
+
self.q = VSS
|
| 128 |
+
self.nand1 = NANDGate()
|
| 129 |
+
self.nand2 = NANDGate()
|
| 130 |
+
|
| 131 |
+
def output(self, s, r):
|
| 132 |
+
# s, r: voltages
|
| 133 |
+
q_bar = self.nand1.output(s, self.q)
|
| 134 |
+
self.q = self.nand2.output(r, q_bar)
|
| 135 |
+
return self.q
|
| 136 |
+
|
| 137 |
+
class DFlipFlop:
|
| 138 |
+
def __init__(self):
|
| 139 |
+
self.sr = SRFlipFlop()
|
| 140 |
+
|
| 141 |
+
def output(self, d, clk):
|
| 142 |
+
# On rising clock, sample d
|
| 143 |
+
s = d if clk > VTH else VSS
|
| 144 |
+
r = NOTGate().output(d) if clk > VTH else VSS
|
| 145 |
+
return self.sr.output(s, r)
|
| 146 |
+
|
| 147 |
+
class JKFlipFlop:
|
| 148 |
+
def __init__(self):
|
| 149 |
+
self.q = VSS
|
| 150 |
+
self.j = None
|
| 151 |
+
self.k = None
|
| 152 |
+
self.nand1 = NANDGate()
|
| 153 |
+
self.nand2 = NANDGate()
|
| 154 |
+
self.nand3 = NANDGate()
|
| 155 |
+
self.nand4 = NANDGate()
|
| 156 |
+
|
| 157 |
+
def output(self, j, k, clk):
|
| 158 |
+
# Simple JK: toggle on J=K=1, set/reset otherwise
|
| 159 |
+
if clk > VTH:
|
| 160 |
+
if j > VTH and k > VTH:
|
| 161 |
+
self.q = VDD if self.q == VSS else VSS
|
| 162 |
+
elif j > VTH:
|
| 163 |
+
self.q = VDD
|
| 164 |
+
elif k > VTH:
|
| 165 |
+
self.q = VSS
|
| 166 |
+
return self.q
|
| 167 |
+
|
| 168 |
+
class TFlipFlop:
|
| 169 |
+
def __init__(self):
|
| 170 |
+
self.q = VSS
|
| 171 |
+
|
| 172 |
+
def output(self, t, clk):
|
| 173 |
+
if clk > VTH and t > VTH:
|
| 174 |
+
self.q = VDD if self.q == VSS else VSS
|
| 175 |
+
return self.q
|
| 176 |
+
|
| 177 |
+
# Example: 2-bit Register (sequential logic)
|
| 178 |
+
class Register2Bit:
|
| 179 |
+
def __init__(self):
|
| 180 |
+
self.dff0 = DFlipFlop()
|
| 181 |
+
self.dff1 = DFlipFlop()
|
| 182 |
+
|
| 183 |
+
def output(self, d0, d1, clk):
|
| 184 |
+
q0 = self.dff0.output(d0, clk)
|
| 185 |
+
q1 = self.dff1.output(d1, clk)
|
| 186 |
+
return q0, q1
|
| 187 |
+
|
| 188 |
+
# Example usage
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
# ...existing code...
|
| 191 |
+
xor = XORGate()
|
| 192 |
+
print("XOR(0.7, 0.0):", xor.output(0.7, 0.0))
|
| 193 |
+
fa = FullAdder()
|
| 194 |
+
s, c = fa.output(0.7, 0.7, 0.0)
|
| 195 |
+
print("FullAdder(1,1,0): sum=", s, "carry=", c)
|
| 196 |
+
sr = SRFlipFlop()
|
| 197 |
+
print("SRFlipFlop S=1, R=0:", sr.output(0.7, 0.0))
|
| 198 |
+
dff = DFlipFlop()
|
| 199 |
+
print("DFlipFlop D=1, CLK=1:", dff.output(0.7, 0.7))
|
| 200 |
+
jk = JKFlipFlop()
|
| 201 |
+
print("JKFlipFlop J=1, K=1, CLK=1:", jk.output(0.7, 0.7, 0.7))
|
| 202 |
+
tff = TFlipFlop()
|
| 203 |
+
print("TFlipFlop T=1, CLK=1:", tff.output(0.7, 0.7))
|
| 204 |
+
reg = Register2Bit()
|
| 205 |
+
print("Register2Bit D0=1, D1=0, CLK=1:", reg.output(0.7, 0.0, 0.7))
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
# --- Functional Units and Modules ---
|
| 209 |
+
# Arithmetic Logic Unit (ALU) - 1-bit (can be extended to n-bit)
|
| 210 |
+
class ALU1Bit:
|
| 211 |
+
def __init__(self):
|
| 212 |
+
self.andg = ANDGate()
|
| 213 |
+
self.org = ORGate()
|
| 214 |
+
self.xorg = XORGate()
|
| 215 |
+
self.fadd = FullAdder()
|
| 216 |
+
|
| 217 |
+
def operate(self, a, b, cin, op):
|
| 218 |
+
"""
|
| 219 |
+
op: 2-bit operation selector
|
| 220 |
+
00 = AND, 01 = OR, 10 = ADD, 11 = XOR
|
| 221 |
+
Returns (result, carry_out)
|
| 222 |
+
"""
|
| 223 |
+
if op == 0b00:
|
| 224 |
+
return self.andg.output(a, b), 0.0
|
| 225 |
+
elif op == 0b01:
|
| 226 |
+
return self.org.output(a, b), 0.0
|
| 227 |
+
elif op == 0b10:
|
| 228 |
+
s, c = self.fadd.output(a, b, cin)
|
| 229 |
+
return s, c
|
| 230 |
+
elif op == 0b11:
|
| 231 |
+
return self.xorg.output(a, b), 0.0
|
| 232 |
+
else:
|
| 233 |
+
raise ValueError("Invalid ALU op")
|
| 234 |
+
|
| 235 |
+
# 2-bit ALU (example of module composition)
|
| 236 |
+
class ALU2Bit:
|
| 237 |
+
def __init__(self):
|
| 238 |
+
self.alu0 = ALU1Bit()
|
| 239 |
+
self.alu1 = ALU1Bit()
|
| 240 |
+
|
| 241 |
+
def operate(self, a0, a1, b0, b1, cin, op):
|
| 242 |
+
# Least significant bit
|
| 243 |
+
r0, c0 = self.alu0.operate(a0, b0, cin, op)
|
| 244 |
+
# Most significant bit
|
| 245 |
+
r1, c1 = self.alu1.operate(a1, b1, c0, op)
|
| 246 |
+
return (r0, r1), c1
|
| 247 |
+
|
| 248 |
+
# 2-bit Counter (using T flip-flops)
|
| 249 |
+
class Counter2Bit:
|
| 250 |
+
def __init__(self):
|
| 251 |
+
self.tff0 = TFlipFlop()
|
| 252 |
+
self.tff1 = TFlipFlop()
|
| 253 |
+
|
| 254 |
+
def tick(self, clk):
|
| 255 |
+
q0 = self.tff0.output(VDD, clk)
|
| 256 |
+
q1 = self.tff1.output(q0, clk)
|
| 257 |
+
return self.tff0.q, self.tff1.q
|
| 258 |
+
|
| 259 |
+
# 2x2-bit Register File (2 registers, 2 bits each)
|
| 260 |
+
class RegisterFile2x2:
|
| 261 |
+
def __init__(self):
|
| 262 |
+
self.reg0 = Register2Bit()
|
| 263 |
+
self.reg1 = Register2Bit()
|
| 264 |
+
self.sel = 0 # select register 0 or 1
|
| 265 |
+
|
| 266 |
+
def write(self, d0, d1, clk, sel):
|
| 267 |
+
if sel == 0:
|
| 268 |
+
self.reg0.output(d0, d1, clk)
|
| 269 |
+
else:
|
| 270 |
+
self.reg1.output(d0, d1, clk)
|
| 271 |
+
|
| 272 |
+
def read(self, sel):
|
| 273 |
+
if sel == 0:
|
| 274 |
+
return self.reg0.dff0.sr.q, self.reg0.dff1.sr.q
|
| 275 |
+
else:
|
| 276 |
+
return self.reg1.dff0.sr.q, self.reg1.dff1.sr.q
|
| 277 |
+
|
| 278 |
+
# Example usage of functional units
|
| 279 |
+
if __name__ == "__main__":
|
| 280 |
+
# ...existing code...
|
| 281 |
+
alu = ALU1Bit()
|
| 282 |
+
res, cout = alu.operate(0.7, 0.0, 0.0, 0b10)
|
| 283 |
+
print("ALU1Bit ADD 1+0: result=", res, "carry=", cout)
|
| 284 |
+
alu2 = ALU2Bit()
|
| 285 |
+
(r0, r1), c = alu2.operate(0.7, 0.0, 0.7, 0.7, 0.0, 0b10)
|
| 286 |
+
print("ALU2Bit ADD (10)+(11): result=", (r0, r1), "carry=", c)
|
| 287 |
+
counter = Counter2Bit()
|
| 288 |
+
print("Counter2Bit tick 1:", counter.tick(0.7))
|
| 289 |
+
print("Counter2Bit tick 2:", counter.tick(0.7))
|
| 290 |
+
regfile = RegisterFile2x2()
|
| 291 |
+
regfile.write(0.7, 0.0, 0.7, 0)
|
| 292 |
+
regfile.write(0.0, 0.7, 0.7, 1)
|
| 293 |
+
print("RegisterFile2x2 read reg0:", regfile.read(0))
|
| 294 |
+
print("RegisterFile2x2 read reg1:", regfile.read(1))
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# --- Control Unit, Registers, and Memory Management Units ---
|
| 298 |
+
|
| 299 |
+
# Simple Control Unit (Finite State Machine for ALU operations)
|
| 300 |
+
class ControlUnit:
|
| 301 |
+
def __init__(self):
|
| 302 |
+
self.state = 0
|
| 303 |
+
self.opcode = 0b00 # default operation
|
| 304 |
+
|
| 305 |
+
def set_opcode(self, opcode):
|
| 306 |
+
self.opcode = opcode
|
| 307 |
+
|
| 308 |
+
def next_state(self):
|
| 309 |
+
self.state = (self.state + 1) % 4
|
| 310 |
+
return self.state
|
| 311 |
+
|
| 312 |
+
def get_control_signals(self):
|
| 313 |
+
# Example: output ALU op and register select
|
| 314 |
+
reg_sel = self.state % 2
|
| 315 |
+
return {'alu_op': self.opcode, 'reg_sel': reg_sel}
|
| 316 |
+
|
| 317 |
+
# General Purpose Register (n-bit, here 2-bit for demo)
|
| 318 |
+
class GeneralPurposeRegister:
|
| 319 |
+
def __init__(self, bits=2):
|
| 320 |
+
self.bits = bits
|
| 321 |
+
self.dffs = [DFlipFlop() for _ in range(bits)]
|
| 322 |
+
|
| 323 |
+
def write(self, data, clk):
|
| 324 |
+
for i in range(self.bits):
|
| 325 |
+
self.dffs[i].output(data[i], clk)
|
| 326 |
+
|
| 327 |
+
def read(self):
|
| 328 |
+
return tuple(self.dffs[i].sr.q for i in range(self.bits))
|
| 329 |
+
|
| 330 |
+
# Simple Memory Management Unit (MMU) - address decode and register file access
|
| 331 |
+
class SimpleMMU:
|
| 332 |
+
def __init__(self, num_registers=2, bits=2):
|
| 333 |
+
self.registers = [GeneralPurposeRegister(bits) for _ in range(num_registers)]
|
| 334 |
+
|
| 335 |
+
def write(self, addr, data, clk):
|
| 336 |
+
if 0 <= addr < len(self.registers):
|
| 337 |
+
self.registers[addr].write(data, clk)
|
| 338 |
+
|
| 339 |
+
def read(self, addr):
|
| 340 |
+
if 0 <= addr < len(self.registers):
|
| 341 |
+
return self.registers[addr].read()
|
| 342 |
+
return None
|
| 343 |
+
|
| 344 |
+
# Example usage of control and memory units
|
| 345 |
+
if __name__ == "__main__":
|
| 346 |
+
# ...existing code...
|
| 347 |
+
cu = ControlUnit()
|
| 348 |
+
cu.set_opcode(0b10) # ADD
|
| 349 |
+
print("ControlUnit state:", cu.next_state(), cu.get_control_signals())
|
| 350 |
+
gpr = GeneralPurposeRegister(bits=2)
|
| 351 |
+
gpr.write([0.7, 0.0], 0.7)
|
| 352 |
+
print("GeneralPurposeRegister read:", gpr.read())
|
| 353 |
+
mmu = SimpleMMU(num_registers=2, bits=2)
|
| 354 |
+
mmu.write(0, [0.7, 0.0], 0.7)
|
| 355 |
+
mmu.write(1, [0.0, 0.7], 0.7)
|
| 356 |
+
print("SimpleMMU read reg0:", mmu.read(0))
|
| 357 |
+
print("SimpleMMU read reg1:", mmu.read(1))
|
multi_gpu_system.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from websocket_storage import WebSocketGPUStorage
|
| 2 |
+
from gpu_chip import GPUChip
|
| 3 |
+
from typing import Dict, Any, List, Optional
|
| 4 |
+
import time
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
class MultiGPUSystem:
|
| 8 |
+
def __init__(self, num_gpus: int = 8, storage=None):
|
| 9 |
+
self.storage = storage
|
| 10 |
+
if self.storage is None:
|
| 11 |
+
from websocket_storage import WebSocketGPUStorage
|
| 12 |
+
self.storage = WebSocketGPUStorage()
|
| 13 |
+
if not self.storage.wait_for_connection():
|
| 14 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 15 |
+
|
| 16 |
+
# Initialize GPUs with shared storage
|
| 17 |
+
self.gpus = [GPUChip(i, storage=self.storage) for i in range(num_gpus)]
|
| 18 |
+
|
| 19 |
+
# Initialize system state
|
| 20 |
+
self.system_state = {
|
| 21 |
+
"num_gpus": num_gpus,
|
| 22 |
+
"nvlink_state": {
|
| 23 |
+
"connections": self._init_nvlink_topology(num_gpus),
|
| 24 |
+
"active_transfers": {}
|
| 25 |
+
},
|
| 26 |
+
"global_memory_state": {
|
| 27 |
+
"total_vram_gb": num_gpus * 24, # Assuming 24GB per GPU
|
| 28 |
+
"allocated_vram_gb": 0
|
| 29 |
+
},
|
| 30 |
+
"power_state": {
|
| 31 |
+
"total_watts": 0,
|
| 32 |
+
"gpu_watts": [0] * num_gpus
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
self.store_system_state()
|
| 36 |
+
|
| 37 |
+
def _init_nvlink_topology(self, num_gpus: int) -> Dict[str, Any]:
|
| 38 |
+
"""Initialize NVLink connection topology"""
|
| 39 |
+
topology = {}
|
| 40 |
+
for i in range(num_gpus):
|
| 41 |
+
for j in range(i + 1, num_gpus):
|
| 42 |
+
link_id = f"nvlink_{i}_{j}"
|
| 43 |
+
topology[link_id] = {
|
| 44 |
+
"gpu_a": i,
|
| 45 |
+
"gpu_b": j,
|
| 46 |
+
"bandwidth_gbps": 300, # NVLink 4.0 speed
|
| 47 |
+
"active": True
|
| 48 |
+
}
|
| 49 |
+
return topology
|
| 50 |
+
|
| 51 |
+
def store_system_state(self):
|
| 52 |
+
"""Store system state in WebSocket storage"""
|
| 53 |
+
self.storage.store_state("multi_gpu_system", "state", self.system_state)
|
| 54 |
+
|
| 55 |
+
def allocate_distributed(self, size: int) -> List[str]:
|
| 56 |
+
"""Allocate memory across multiple GPUs"""
|
| 57 |
+
size_per_gpu = size // len(self.gpus)
|
| 58 |
+
block_ids = []
|
| 59 |
+
|
| 60 |
+
for gpu in self.gpus:
|
| 61 |
+
block_id = gpu.allocate_memory(size_per_gpu)
|
| 62 |
+
block_ids.append(block_id)
|
| 63 |
+
|
| 64 |
+
self.system_state["global_memory_state"]["allocated_vram_gb"] += size / (1024 * 1024 * 1024)
|
| 65 |
+
self.store_system_state()
|
| 66 |
+
|
| 67 |
+
return block_ids
|
| 68 |
+
|
| 69 |
+
def transfer_between_gpus(self, src_gpu: int, dst_gpu: int, data_id: str):
|
| 70 |
+
"""Transfer data between GPUs using NVLink"""
|
| 71 |
+
if not (0 <= src_gpu < len(self.gpus) and 0 <= dst_gpu < len(self.gpus)):
|
| 72 |
+
raise ValueError("Invalid GPU indices")
|
| 73 |
+
|
| 74 |
+
link_id = f"nvlink_{min(src_gpu, dst_gpu)}_{max(src_gpu, dst_gpu)}"
|
| 75 |
+
if link_id not in self.system_state["nvlink_state"]["connections"]:
|
| 76 |
+
raise ValueError("No NVLink connection between specified GPUs")
|
| 77 |
+
|
| 78 |
+
# Start transfer
|
| 79 |
+
transfer_id = f"transfer_{time.time_ns()}"
|
| 80 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id] = {
|
| 81 |
+
"source_gpu": src_gpu,
|
| 82 |
+
"dest_gpu": dst_gpu,
|
| 83 |
+
"data_id": data_id,
|
| 84 |
+
"start_time": time.time_ns()
|
| 85 |
+
}
|
| 86 |
+
self.store_system_state()
|
| 87 |
+
|
| 88 |
+
# Get data from source GPU
|
| 89 |
+
data = self.storage.load_tensor(data_id)
|
| 90 |
+
if data is not None:
|
| 91 |
+
# Store in destination GPU
|
| 92 |
+
new_block_id = self.gpus[dst_gpu].allocate_memory(len(data))
|
| 93 |
+
self.storage.store_tensor(new_block_id, data)
|
| 94 |
+
|
| 95 |
+
# Update transfer state
|
| 96 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id]["completed"] = True
|
| 97 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id]["end_time"] = time.time_ns()
|
| 98 |
+
self.store_system_state()
|
| 99 |
+
|
| 100 |
+
return new_block_id
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
def schedule_distributed_compute(self, compute_graph: Dict[str, Any]):
|
| 104 |
+
"""Schedule computation across multiple GPUs"""
|
| 105 |
+
# Simple round-robin scheduling for now
|
| 106 |
+
scheduled_ops = []
|
| 107 |
+
for i, op in enumerate(compute_graph["operations"]):
|
| 108 |
+
gpu_index = i % len(self.gpus)
|
| 109 |
+
warp_id = self.gpus[gpu_index].schedule_compute(
|
| 110 |
+
sm_index=i % self.gpus[gpu_index].chip_state["num_sms"],
|
| 111 |
+
warp_state=op
|
| 112 |
+
)
|
| 113 |
+
scheduled_ops.append({
|
| 114 |
+
"op": op,
|
| 115 |
+
"gpu": gpu_index,
|
| 116 |
+
"warp_id": warp_id
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
# Store scheduling decision
|
| 120 |
+
self.storage.store_state(
|
| 121 |
+
"compute_schedule",
|
| 122 |
+
f"schedule_{time.time_ns()}",
|
| 123 |
+
{"operations": scheduled_ops}
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
return scheduled_ops
|
| 127 |
+
|
| 128 |
+
def synchronize(self):
|
| 129 |
+
"""Synchronize all GPUs"""
|
| 130 |
+
sync_point = f"sync_{time.time_ns()}"
|
| 131 |
+
for i, gpu in enumerate(self.gpus):
|
| 132 |
+
gpu.chip_state["sync_point"] = sync_point
|
| 133 |
+
gpu.store_chip_state()
|
| 134 |
+
|
| 135 |
+
self.system_state["last_sync"] = sync_point
|
| 136 |
+
self.store_system_state()
|
| 137 |
+
|
| 138 |
+
def get_system_stats(self) -> Dict[str, Any]:
|
| 139 |
+
"""Get comprehensive system statistics"""
|
| 140 |
+
stats = {
|
| 141 |
+
"num_gpus": len(self.gpus),
|
| 142 |
+
"total_vram_gb": self.system_state["global_memory_state"]["total_vram_gb"],
|
| 143 |
+
"allocated_vram_gb": self.system_state["global_memory_state"]["allocated_vram_gb"],
|
| 144 |
+
"gpus": [gpu.get_stats() for gpu in self.gpus],
|
| 145 |
+
"nvlink": {
|
| 146 |
+
"active_connections": sum(1 for conn in self.system_state["nvlink_state"]["connections"].values() if conn["active"]),
|
| 147 |
+
"active_transfers": len(self.system_state["nvlink_state"]["active_transfers"])
|
| 148 |
+
},
|
| 149 |
+
"power": {
|
| 150 |
+
"total_watts": sum(gpu.chip_state["power_state"]["total_watts"] for gpu in self.gpus),
|
| 151 |
+
"per_gpu_watts": [gpu.chip_state["power_state"]["total_watts"] for gpu in self.gpus]
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
return stats
|
multi_gpu_system_http.py
ADDED
|
@@ -0,0 +1,164 @@
|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+
from gpu_chip import GPUChip
|
| 3 |
+
from typing import Dict, Any, List, Optional
|
| 4 |
+
import time
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
class MultiGPUSystem:
|
| 8 |
+
def __init__(self, num_gpus: int = 8, storage=None):
|
| 9 |
+
self.storage = storage
|
| 10 |
+
if self.storage is None:
|
| 11 |
+
from http_storage import HTTPGPUStorage
|
| 12 |
+
self.storage = HTTPGPUStorage()
|
| 13 |
+
if not self.storage.wait_for_connection():
|
| 14 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 15 |
+
|
| 16 |
+
# Initialize GPUs with shared storage
|
| 17 |
+
self.gpus = [GPUChip(i, storage=self.storage) for i in range(num_gpus)]
|
| 18 |
+
|
| 19 |
+
# Initialize system state
|
| 20 |
+
self.system_state = {
|
| 21 |
+
"num_gpus": num_gpus,
|
| 22 |
+
"nvlink_state": {
|
| 23 |
+
"connections": self._init_nvlink_topology(num_gpus),
|
| 24 |
+
"active_transfers": {}
|
| 25 |
+
},
|
| 26 |
+
"global_memory_state": {
|
| 27 |
+
"total_vram_gb": num_gpus * 24, # Assuming 24GB per GPU
|
| 28 |
+
"allocated_vram_gb": 0
|
| 29 |
+
},
|
| 30 |
+
"power_state": {
|
| 31 |
+
"total_watts": 0,
|
| 32 |
+
"gpu_watts": [0] * num_gpus
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
self.store_system_state()
|
| 36 |
+
|
| 37 |
+
def _init_nvlink_topology(self, num_gpus: int) -> Dict[str, Any]:
|
| 38 |
+
"""Initialize NVLink connection topology"""
|
| 39 |
+
topology = {}
|
| 40 |
+
for i in range(num_gpus):
|
| 41 |
+
for j in range(i + 1, num_gpus):
|
| 42 |
+
link_id = f"nvlink_{i}_{j}"
|
| 43 |
+
topology[link_id] = {
|
| 44 |
+
"gpu_a": i,
|
| 45 |
+
"gpu_b": j,
|
| 46 |
+
"bandwidth_gbps": 300, # NVLink 4.0 speed
|
| 47 |
+
"active": True
|
| 48 |
+
}
|
| 49 |
+
return topology
|
| 50 |
+
|
| 51 |
+
def store_system_state(self):
|
| 52 |
+
"""Store system state in HTTP storage"""
|
| 53 |
+
self.storage.store_state("multi_gpu_system", "state", self.system_state)
|
| 54 |
+
|
| 55 |
+
def allocate_distributed(self, size: int) -> List[str]:
|
| 56 |
+
"""Allocate memory across multiple GPUs"""
|
| 57 |
+
size_per_gpu = size // len(self.gpus)
|
| 58 |
+
block_ids = []
|
| 59 |
+
|
| 60 |
+
for gpu in self.gpus:
|
| 61 |
+
block_id = gpu.allocate_memory(size_per_gpu)
|
| 62 |
+
block_ids.append(block_id)
|
| 63 |
+
|
| 64 |
+
self.system_state["global_memory_state"]["allocated_vram_gb"] += size / (1024 * 1024 * 1024)
|
| 65 |
+
self.store_system_state()
|
| 66 |
+
|
| 67 |
+
return block_ids
|
| 68 |
+
|
| 69 |
+
def transfer_between_gpus(self, src_gpu: int, dst_gpu: int, data_id: str):
|
| 70 |
+
"""Transfer data between GPUs using NVLink simulation via HTTP"""
|
| 71 |
+
if not (0 <= src_gpu < len(self.gpus) and 0 <= dst_gpu < len(self.gpus)):
|
| 72 |
+
raise ValueError("Invalid GPU indices")
|
| 73 |
+
|
| 74 |
+
link_id = f"nvlink_{min(src_gpu, dst_gpu)}_{max(src_gpu, dst_gpu)}"
|
| 75 |
+
if link_id not in self.system_state["nvlink_state"]["connections"]:
|
| 76 |
+
raise ValueError("No NVLink connection between specified GPUs")
|
| 77 |
+
|
| 78 |
+
# Start transfer using HTTP API
|
| 79 |
+
transfer_id = f"transfer_{time.time_ns()}"
|
| 80 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id] = {
|
| 81 |
+
"source_gpu": src_gpu,
|
| 82 |
+
"dest_gpu": dst_gpu,
|
| 83 |
+
"data_id": data_id,
|
| 84 |
+
"start_time": time.time_ns()
|
| 85 |
+
}
|
| 86 |
+
self.store_system_state()
|
| 87 |
+
|
| 88 |
+
# Use HTTP storage transfer method
|
| 89 |
+
new_block_id = self.storage.transfer_between_chips(src_gpu, dst_gpu, data_id)
|
| 90 |
+
|
| 91 |
+
if new_block_id:
|
| 92 |
+
# Update transfer state
|
| 93 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id]["completed"] = True
|
| 94 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id]["end_time"] = time.time_ns()
|
| 95 |
+
self.system_state["nvlink_state"]["active_transfers"][transfer_id]["new_data_id"] = new_block_id
|
| 96 |
+
self.store_system_state()
|
| 97 |
+
|
| 98 |
+
return new_block_id
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
def schedule_distributed_compute(self, compute_graph: Dict[str, Any]):
|
| 102 |
+
"""Schedule computation across multiple GPUs"""
|
| 103 |
+
# Simple round-robin scheduling for now
|
| 104 |
+
scheduled_ops = []
|
| 105 |
+
for i, op in enumerate(compute_graph["operations"]):
|
| 106 |
+
gpu_index = i % len(self.gpus)
|
| 107 |
+
warp_id = self.gpus[gpu_index].schedule_compute(
|
| 108 |
+
sm_index=i % self.gpus[gpu_index].chip_state["num_sms"],
|
| 109 |
+
warp_state=op
|
| 110 |
+
)
|
| 111 |
+
scheduled_ops.append({
|
| 112 |
+
"op": op,
|
| 113 |
+
"gpu": gpu_index,
|
| 114 |
+
"warp_id": warp_id
|
| 115 |
+
})
|
| 116 |
+
|
| 117 |
+
# Store scheduling decision
|
| 118 |
+
self.storage.store_state(
|
| 119 |
+
"compute_schedule",
|
| 120 |
+
f"schedule_{time.time_ns()}",
|
| 121 |
+
{"operations": scheduled_ops}
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
return scheduled_ops
|
| 125 |
+
|
| 126 |
+
def synchronize(self):
|
| 127 |
+
"""Synchronize all GPUs using HTTP barrier"""
|
| 128 |
+
sync_point = f"sync_{time.time_ns()}"
|
| 129 |
+
|
| 130 |
+
# Create synchronization barrier
|
| 131 |
+
if not self.storage.create_sync_barrier(sync_point, len(self.gpus)):
|
| 132 |
+
raise RuntimeError("Failed to create synchronization barrier")
|
| 133 |
+
|
| 134 |
+
# Each GPU reaches the barrier
|
| 135 |
+
for i, gpu in enumerate(self.gpus):
|
| 136 |
+
gpu.chip_state["sync_point"] = sync_point
|
| 137 |
+
gpu.store_chip_state()
|
| 138 |
+
|
| 139 |
+
# Wait at barrier (in real implementation, this would be done in parallel)
|
| 140 |
+
while not self.storage.wait_sync_barrier(sync_point):
|
| 141 |
+
time.sleep(0.01) # Brief delay
|
| 142 |
+
|
| 143 |
+
self.system_state["last_sync"] = sync_point
|
| 144 |
+
self.store_system_state()
|
| 145 |
+
|
| 146 |
+
def get_system_stats(self) -> Dict[str, Any]:
|
| 147 |
+
"""Get comprehensive system statistics"""
|
| 148 |
+
stats = {
|
| 149 |
+
"num_gpus": len(self.gpus),
|
| 150 |
+
"total_vram_gb": self.system_state["global_memory_state"]["total_vram_gb"],
|
| 151 |
+
"allocated_vram_gb": self.system_state["global_memory_state"]["allocated_vram_gb"],
|
| 152 |
+
"gpus": [gpu.get_stats() for gpu in self.gpus],
|
| 153 |
+
"nvlink": {
|
| 154 |
+
"active_connections": sum(1 for conn in self.system_state["nvlink_state"]["connections"].values() if conn["active"]),
|
| 155 |
+
"active_transfers": len(self.system_state["nvlink_state"]["active_transfers"])
|
| 156 |
+
},
|
| 157 |
+
"power": {
|
| 158 |
+
"total_watts": sum(gpu.chip_state["power_state"]["total_watts"] for gpu in self.gpus),
|
| 159 |
+
"per_gpu_watts": [gpu.chip_state["power_state"]["total_watts"] for gpu in self.gpus]
|
| 160 |
+
},
|
| 161 |
+
"connection_status": self.storage.get_connection_status()
|
| 162 |
+
}
|
| 163 |
+
return stats
|
| 164 |
+
|
multicore.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Multicore system simulation for virtual GPU v2.
|
| 3 |
+
Simulates 50,000 identical AdvancedCore instances in parallel.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from core import AdvancedCore
|
| 7 |
+
|
| 8 |
+
class MultiCoreSystem:
|
| 9 |
+
def __init__(self, num_cores=50000, bits=2, num_registers=2):
|
| 10 |
+
self.cores = [AdvancedCore(bits=bits, num_registers=num_registers) for _ in range(num_cores)]
|
| 11 |
+
self.num_cores = num_cores
|
| 12 |
+
|
| 13 |
+
def step_all(self, a, b, cin, opcode, reg_sel):
|
| 14 |
+
"""
|
| 15 |
+
Steps all cores in parallel with the same input.
|
| 16 |
+
a, b: lists of voltages (length 2)
|
| 17 |
+
cin: carry in
|
| 18 |
+
opcode: ALU operation
|
| 19 |
+
reg_sel: register select
|
| 20 |
+
Returns: list of results from all cores
|
| 21 |
+
"""
|
| 22 |
+
return [core.step(a, b, cin, opcode, reg_sel) for core in self.cores]
|
| 23 |
+
|
| 24 |
+
def step_all_custom(self, inputs):
|
| 25 |
+
"""
|
| 26 |
+
Steps all cores in parallel with custom input for each core.
|
| 27 |
+
inputs: list of dicts with keys 'a', 'b', 'cin', 'opcode', 'reg_sel'
|
| 28 |
+
Returns: list of results from all cores
|
| 29 |
+
"""
|
| 30 |
+
return [core.step(inp['a'], inp['b'], inp['cin'], inp['opcode'], inp['reg_sel']) for core, inp in zip(self.cores, inputs)]
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
print("\n--- MultiCore System Simulation (50,000 cores) ---")
|
| 34 |
+
system = MultiCoreSystem(num_cores=50000, bits=2, num_registers=2)
|
| 35 |
+
# Example: Step all cores with the same ADD operation
|
| 36 |
+
results = system.step_all([0.7, 0.0], [0.7, 0.7], 0.0, 0b10, 0)
|
| 37 |
+
print(f"First core result: {results[0]}")
|
| 38 |
+
print(f"Total cores simulated: {len(results)}")
|
streaming_multiprocessor.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from websocket_storage import WebSocketGPUStorage
|
| 2 |
+
import numpy as np
|
| 3 |
+
from typing import Dict, Any, Optional, List
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
class StreamingMultiprocessor:
|
| 7 |
+
def __init__(self, sm_id: int, num_cores: int = 128, storage=None):
|
| 8 |
+
self.sm_id = sm_id
|
| 9 |
+
self.num_cores = num_cores
|
| 10 |
+
self.storage = storage
|
| 11 |
+
if self.storage is None:
|
| 12 |
+
from websocket_storage import WebSocketGPUStorage
|
| 13 |
+
self.storage = WebSocketGPUStorage()
|
| 14 |
+
if not self.storage.wait_for_connection():
|
| 15 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 16 |
+
|
| 17 |
+
# Initialize SM state
|
| 18 |
+
self.sm_state = {
|
| 19 |
+
"sm_id": sm_id,
|
| 20 |
+
"num_cores": num_cores,
|
| 21 |
+
"active_warps": {},
|
| 22 |
+
"shared_memory": {},
|
| 23 |
+
"register_file": {},
|
| 24 |
+
"l1_cache": {},
|
| 25 |
+
"warp_scheduler_state": {
|
| 26 |
+
"active_warps": [],
|
| 27 |
+
"pending_warps": [],
|
| 28 |
+
"completed_warps": []
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
self.store_sm_state()
|
| 32 |
+
|
| 33 |
+
def store_sm_state(self):
|
| 34 |
+
"""Store SM state in WebSocket storage"""
|
| 35 |
+
self.storage.store_state(f"sm_{self.sm_id}", "state", self.sm_state)
|
| 36 |
+
|
| 37 |
+
def allocate_shared_memory(self, size: int, block_id: str) -> str:
|
| 38 |
+
"""Allocate shared memory for a block"""
|
| 39 |
+
shared_id = f"shared_{block_id}_{time.time_ns()}"
|
| 40 |
+
self.sm_state["shared_memory"][shared_id] = {
|
| 41 |
+
"size": size,
|
| 42 |
+
"block_id": block_id,
|
| 43 |
+
"allocated_at": time.time_ns()
|
| 44 |
+
}
|
| 45 |
+
self.store_sm_state()
|
| 46 |
+
return shared_id
|
| 47 |
+
|
| 48 |
+
def write_shared_memory(self, shared_id: str, data: np.ndarray):
|
| 49 |
+
"""Write to shared memory"""
|
| 50 |
+
if shared_id not in self.sm_state["shared_memory"]:
|
| 51 |
+
raise ValueError(f"Shared memory block {shared_id} not allocated")
|
| 52 |
+
|
| 53 |
+
return self.storage.store_tensor(shared_id, data)
|
| 54 |
+
|
| 55 |
+
def read_shared_memory(self, shared_id: str) -> Optional[np.ndarray]:
|
| 56 |
+
"""Read from shared memory"""
|
| 57 |
+
if shared_id not in self.sm_state["shared_memory"]:
|
| 58 |
+
raise ValueError(f"Shared memory block {shared_id} not allocated")
|
| 59 |
+
|
| 60 |
+
return self.storage.load_tensor(shared_id)
|
| 61 |
+
|
| 62 |
+
def schedule_warp(self, warp_id: str, warp_state: Dict[str, Any]):
|
| 63 |
+
"""Schedule a warp for execution"""
|
| 64 |
+
self.sm_state["warp_scheduler_state"]["active_warps"].append(warp_id)
|
| 65 |
+
self.sm_state["active_warps"][warp_id] = warp_state
|
| 66 |
+
self.store_sm_state()
|
| 67 |
+
|
| 68 |
+
# Store warp state
|
| 69 |
+
self.storage.store_state(f"warp_{warp_id}", "state", warp_state)
|
| 70 |
+
|
| 71 |
+
def complete_warp(self, warp_id: str):
|
| 72 |
+
"""Mark a warp as completed"""
|
| 73 |
+
if warp_id in self.sm_state["active_warps"]:
|
| 74 |
+
self.sm_state["warp_scheduler_state"]["active_warps"].remove(warp_id)
|
| 75 |
+
self.sm_state["warp_scheduler_state"]["completed_warps"].append(warp_id)
|
| 76 |
+
warp_state = self.sm_state["active_warps"].pop(warp_id)
|
| 77 |
+
self.store_sm_state()
|
| 78 |
+
|
| 79 |
+
# Store completed state
|
| 80 |
+
self.storage.store_state(f"warp_{warp_id}", "completed", warp_state)
|
| 81 |
+
|
| 82 |
+
def write_register(self, warp_id: str, reg_id: str, data: np.ndarray):
|
| 83 |
+
"""Write to register file"""
|
| 84 |
+
reg_key = f"reg_{warp_id}_{reg_id}"
|
| 85 |
+
self.sm_state["register_file"][reg_key] = {
|
| 86 |
+
"warp_id": warp_id,
|
| 87 |
+
"reg_id": reg_id,
|
| 88 |
+
"last_accessed": time.time_ns()
|
| 89 |
+
}
|
| 90 |
+
self.store_sm_state()
|
| 91 |
+
|
| 92 |
+
return self.storage.store_tensor(reg_key, data)
|
| 93 |
+
|
| 94 |
+
def read_register(self, warp_id: str, reg_id: str) -> Optional[np.ndarray]:
|
| 95 |
+
"""Read from register file"""
|
| 96 |
+
reg_key = f"reg_{warp_id}_{reg_id}"
|
| 97 |
+
if reg_key in self.sm_state["register_file"]:
|
| 98 |
+
self.sm_state["register_file"][reg_key]["last_accessed"] = time.time_ns()
|
| 99 |
+
self.store_sm_state()
|
| 100 |
+
return self.storage.load_tensor(reg_key)
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 104 |
+
"""Get SM statistics"""
|
| 105 |
+
return {
|
| 106 |
+
"sm_id": self.sm_id,
|
| 107 |
+
"num_cores": self.num_cores,
|
| 108 |
+
"active_warps": len(self.sm_state["active_warps"]),
|
| 109 |
+
"shared_memory_blocks": len(self.sm_state["shared_memory"]),
|
| 110 |
+
"register_file_entries": len(self.sm_state["register_file"]),
|
| 111 |
+
"completed_warps": len(self.sm_state["warp_scheduler_state"]["completed_warps"])
|
| 112 |
+
}
|
tensor_core.py
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tensor Core subsystem for hyperrealistic GPU simulation.
|
| 3 |
+
Models hardware-level matrix multiply-accumulate, scheduling, and memory integration.
|
| 4 |
+
Uses WebSocket-based storage for zero CPU involvement.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import time
|
| 8 |
+
import sys
|
| 9 |
+
import os
|
| 10 |
+
import numpy as np
|
| 11 |
+
from typing import Optional, Dict, Any, Tuple
|
| 12 |
+
from websocket_storage import WebSocketGPUStorage
|
| 13 |
+
|
| 14 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 15 |
+
try:
|
| 16 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP
|
| 17 |
+
except ImportError:
|
| 18 |
+
TARGET_SWITCHES_PER_SEC = 9e20
|
| 19 |
+
TRANSISTORS_ON_CHIP = 6e11
|
| 20 |
+
|
| 21 |
+
class TensorCore:
|
| 22 |
+
"""
|
| 23 |
+
Pure virtual tensor core for matrix operations with zero CPU involvement.
|
| 24 |
+
All operations happen in virtual space at electron speed with WebSocket-based storage.
|
| 25 |
+
"""
|
| 26 |
+
def __init__(self, bits=2, memory_size=800*1024*1024*1024, bandwidth_tbps=10000, sm=None, storage=None):
|
| 27 |
+
from electron_speed import drift_velocity, TARGET_SWITCHES_PER_SEC
|
| 28 |
+
|
| 29 |
+
self.bits = bits
|
| 30 |
+
# WebSocket-based storage
|
| 31 |
+
self.storage = storage
|
| 32 |
+
if self.storage is None:
|
| 33 |
+
from websocket_storage import WebSocketGPUStorage
|
| 34 |
+
self.storage = WebSocketGPUStorage()
|
| 35 |
+
if not self.storage.wait_for_connection():
|
| 36 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 37 |
+
|
| 38 |
+
# Virtual memory space (WebSocket-backed)
|
| 39 |
+
self.virtual_memory_map: Dict[str, str] = {} # Maps virtual addresses to tensor IDs
|
| 40 |
+
self.virtual_registers: Dict[str, np.ndarray] = {}
|
| 41 |
+
|
| 42 |
+
# Direct electron-speed parameters
|
| 43 |
+
self.drift_velocity = drift_velocity
|
| 44 |
+
self.switches_per_sec = TARGET_SWITCHES_PER_SEC
|
| 45 |
+
self.bandwidth_tbps = drift_velocity / 1e-12 # Bandwidth scaled to electron speed
|
| 46 |
+
self.sm = sm
|
| 47 |
+
|
| 48 |
+
# Virtual execution tracking
|
| 49 |
+
self.virtual_ops_count = 0
|
| 50 |
+
self.electron_cycles = 0
|
| 51 |
+
|
| 52 |
+
# Component state ID for this core
|
| 53 |
+
self.core_id = f"tensor_core_{id(self)}"
|
| 54 |
+
|
| 55 |
+
def store_virtual_matrix(self, data: np.ndarray, virtual_addr: Optional[str] = None) -> str:
|
| 56 |
+
"""Store matrix data in WebSocket storage with virtual addressing"""
|
| 57 |
+
if virtual_addr is None:
|
| 58 |
+
virtual_addr = f"vaddr_{id(data)}_{time.time_ns()}"
|
| 59 |
+
|
| 60 |
+
tensor_id = f"tensor_{virtual_addr}"
|
| 61 |
+
self.storage.store_tensor(tensor_id, data)
|
| 62 |
+
self.virtual_memory_map[virtual_addr] = tensor_id
|
| 63 |
+
return virtual_addr
|
| 64 |
+
|
| 65 |
+
def load_virtual_matrix(self, virtual_addr: str) -> Optional[np.ndarray]:
|
| 66 |
+
"""Load matrix data from WebSocket storage using virtual address"""
|
| 67 |
+
if virtual_addr not in self.virtual_memory_map:
|
| 68 |
+
return None
|
| 69 |
+
|
| 70 |
+
tensor_id = self.virtual_memory_map[virtual_addr]
|
| 71 |
+
return self.storage.load_tensor(tensor_id)
|
| 72 |
+
|
| 73 |
+
def fetch_operand(self, source, addr, shape):
|
| 74 |
+
"""
|
| 75 |
+
Fetches a matrix operand from a given source (registers, shared, global).
|
| 76 |
+
Now uses WebSocket storage for global memory access.
|
| 77 |
+
"""
|
| 78 |
+
n, m = shape
|
| 79 |
+
if source == 'register':
|
| 80 |
+
# Virtual registers are kept in memory for ultra-fast access
|
| 81 |
+
matrix = self.virtual_registers.get(addr, np.zeros((n, m)))
|
| 82 |
+
latency = 1e-9 # 1ns
|
| 83 |
+
elif source == 'shared':
|
| 84 |
+
# Shared memory is also WebSocket-backed for consistency
|
| 85 |
+
matrix = self.sm.shared_mem.read_matrix(addr, n, m)
|
| 86 |
+
latency = 10e-9 # 10ns
|
| 87 |
+
elif source == 'global':
|
| 88 |
+
# Simulate VRAM/global memory fetch
|
| 89 |
+
matrix = self.sm.global_mem.read_matrix(addr, n, m)
|
| 90 |
+
latency = 200e-9 # 200ns
|
| 91 |
+
else:
|
| 92 |
+
raise ValueError(f"Unknown source: {source}")
|
| 93 |
+
# Simulate bandwidth (TB/s)
|
| 94 |
+
data_size_bytes = n * m * (self.bits // 8)
|
| 95 |
+
transfer_time = data_size_bytes / (self.bandwidth_tbps * 1e12)
|
| 96 |
+
# No delay: run as fast as possible in virtual mode
|
| 97 |
+
return matrix
|
| 98 |
+
|
| 99 |
+
def matmul(self, A, B):
|
| 100 |
+
# A, B: 2D lists (matrices) of voltages
|
| 101 |
+
n = len(A)
|
| 102 |
+
m = len(B[0])
|
| 103 |
+
p = len(B)
|
| 104 |
+
C = [[0.0 for _ in range(m)] for _ in range(n)]
|
| 105 |
+
for i in range(n):
|
| 106 |
+
for j in range(m):
|
| 107 |
+
acc = 0.0
|
| 108 |
+
for k in range(p):
|
| 109 |
+
acc += A[i][k] * B[k][j]
|
| 110 |
+
C[i][j] = acc
|
| 111 |
+
return C
|
| 112 |
+
|
| 113 |
+
def matmul_from_memory(self, srcA, addrA, srcB, addrB, shapeA, shapeB):
|
| 114 |
+
"""
|
| 115 |
+
Fetches operands from WebSocket storage and performs matmul.
|
| 116 |
+
srcA/srcB: 'register', 'shared', or 'global'
|
| 117 |
+
addrA/addrB: tensor_ids or virtual addresses
|
| 118 |
+
shapeA/shapeB: (n, p), (p, m)
|
| 119 |
+
"""
|
| 120 |
+
# Load matrices from WebSocket storage
|
| 121 |
+
A = self.storage.load_tensor(addrA) if srcA == 'global' else self.fetch_operand(srcA, addrA, shapeA)
|
| 122 |
+
B = self.storage.load_tensor(addrB) if srcB == 'global' else self.fetch_operand(srcB, addrB, shapeB)
|
| 123 |
+
|
| 124 |
+
if A is None or B is None:
|
| 125 |
+
raise ValueError("Could not load input tensors")
|
| 126 |
+
|
| 127 |
+
result = self.matmul(A, B)
|
| 128 |
+
|
| 129 |
+
# Store result in WebSocket storage for future use
|
| 130 |
+
result_id = f"matmul_result_{time.time_ns()}"
|
| 131 |
+
self.storage.store_tensor(result_id, result)
|
| 132 |
+
|
| 133 |
+
return result
|
| 134 |
+
|
| 135 |
+
def load_matrix(self, matrix, row_offset=0, col_offset=0):
|
| 136 |
+
# Loads a matrix into local memory (sparse)
|
| 137 |
+
for i, row in enumerate(matrix):
|
| 138 |
+
for j, val in enumerate(row):
|
| 139 |
+
self.memory[(row_offset+i, col_offset+j)] = val
|
| 140 |
+
|
| 141 |
+
def read_matrix(self, n, m, row_offset=0, col_offset=0):
|
| 142 |
+
# Reads an n x m matrix from local memory (sparse)
|
| 143 |
+
return [
|
| 144 |
+
[self.memory.get((row_offset+i, col_offset+j), 0.0) for j in range(m)]
|
| 145 |
+
for i in range(n)
|
| 146 |
+
]
|
| 147 |
+
|
| 148 |
+
class TensorCoreArray:
|
| 149 |
+
"""
|
| 150 |
+
Pure virtual tensor core array operating at electron speed with zero CPU usage.
|
| 151 |
+
All operations happen in virtual space using WebSocket-based storage for zero host memory usage.
|
| 152 |
+
"""
|
| 153 |
+
def __init__(self, num_tensor_cores=8000, bits=2, memory_size=800*1024*1024*1024, bandwidth_tbps=10000, sm=None):
|
| 154 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity, speed_of_light_silicon
|
| 155 |
+
|
| 156 |
+
# Initialize pure virtual tensor cores with WebSocket storage
|
| 157 |
+
self.tensor_cores = [TensorCore(bits=bits, memory_size=memory_size, bandwidth_tbps=bandwidth_tbps, sm=sm)
|
| 158 |
+
for _ in range(num_tensor_cores)]
|
| 159 |
+
|
| 160 |
+
# WebSocket-based virtual memory management
|
| 161 |
+
self.storage = WebSocketGPUStorage()
|
| 162 |
+
if not self.storage.wait_for_connection():
|
| 163 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 164 |
+
|
| 165 |
+
# Virtual memory mapping
|
| 166 |
+
self.virtual_tensor_map = {} # Maps tensor IDs to their metadata
|
| 167 |
+
self.virtual_execution_units = [] # Track execution units
|
| 168 |
+
|
| 169 |
+
# Direct electron-speed configuration
|
| 170 |
+
self.drift_velocity = drift_velocity
|
| 171 |
+
self.target_switches = TARGET_SWITCHES_PER_SEC
|
| 172 |
+
self.transistors = TRANSISTORS_ON_CHIP
|
| 173 |
+
self.light_speed_si = speed_of_light_silicon
|
| 174 |
+
|
| 175 |
+
# No CPU scheduling - pure virtual dispatch
|
| 176 |
+
self.virtual_dispatch_ptr = 0
|
| 177 |
+
self.sm = sm
|
| 178 |
+
|
| 179 |
+
# Electron-speed aware performance calculations
|
| 180 |
+
self.drift_velocity = drift_velocity
|
| 181 |
+
self.photon_speed = speed_of_light_silicon
|
| 182 |
+
self.electron_photon_ratio = drift_velocity / speed_of_light_silicon
|
| 183 |
+
|
| 184 |
+
# Ultra-deep realism: ops based on electron transit time
|
| 185 |
+
transistors_per_core = TRANSISTORS_ON_CHIP // num_tensor_cores
|
| 186 |
+
self.ops_per_cycle = 1024 * (drift_velocity / 1e9) # Scale with electron speed
|
| 187 |
+
self.switches_per_sec = TARGET_SWITCHES_PER_SEC / num_tensor_cores
|
| 188 |
+
self.clock_ghz = (self.switches_per_sec / transistors_per_core) / 1e9
|
| 189 |
+
|
| 190 |
+
# Calculate theoretical peak performance
|
| 191 |
+
self.pflops = (num_tensor_cores * self.ops_per_cycle * self.clock_ghz) / 1e6
|
| 192 |
+
|
| 193 |
+
# Enable parallel electron-speed matrix operations
|
| 194 |
+
self.parallel_enabled = True
|
| 195 |
+
self.quantum_corrected = True # Enable quantum tunneling corrections
|
| 196 |
+
|
| 197 |
+
def schedule(self):
|
| 198 |
+
"""Schedule tensor core with WebSocket state tracking"""
|
| 199 |
+
tc = self.tensor_cores[self.schedule_ptr]
|
| 200 |
+
self.schedule_ptr = (self.schedule_ptr + 1) % len(self.tensor_cores)
|
| 201 |
+
|
| 202 |
+
# Store scheduling state
|
| 203 |
+
state = {
|
| 204 |
+
"core_index": self.schedule_ptr,
|
| 205 |
+
"timestamp": time.time_ns(),
|
| 206 |
+
"active_tensors": list(self.virtual_tensor_map.keys())
|
| 207 |
+
}
|
| 208 |
+
self.storage.store_state("scheduler", f"schedule_{time.time_ns()}", state)
|
| 209 |
+
|
| 210 |
+
return tc
|
| 211 |
+
|
| 212 |
+
def get_tensor(self, tensor_id: str) -> Optional[np.ndarray]:
|
| 213 |
+
"""Get tensor data from WebSocket storage"""
|
| 214 |
+
return self.storage.load_tensor(tensor_id)
|
| 215 |
+
|
| 216 |
+
def update_tensor(self, tensor_id: str, data: np.ndarray):
|
| 217 |
+
"""Update tensor data in WebSocket storage"""
|
| 218 |
+
self.storage.store_tensor(tensor_id, data)
|
| 219 |
+
|
| 220 |
+
# Update metadata
|
| 221 |
+
if tensor_id in self.virtual_tensor_map:
|
| 222 |
+
metadata = self.virtual_tensor_map[tensor_id]
|
| 223 |
+
metadata["last_updated"] = time.time_ns()
|
| 224 |
+
self.storage.store_state("tensor_metadata", tensor_id, metadata)
|
| 225 |
+
|
| 226 |
+
def allocate_virtual_tensor(self, shape, name, direct_load=True):
|
| 227 |
+
"""Allocate tensor directly in virtual space using WebSocket storage."""
|
| 228 |
+
tensor_id = f"virtual_tensor_{len(self.virtual_tensor_map)}_{time.time_ns()}"
|
| 229 |
+
|
| 230 |
+
# Create metadata
|
| 231 |
+
metadata = {
|
| 232 |
+
"shape": shape,
|
| 233 |
+
"name": name,
|
| 234 |
+
"created_at": time.time_ns(),
|
| 235 |
+
"tensor_id": tensor_id
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
# Store metadata in WebSocket storage
|
| 239 |
+
self.storage.store_state("tensor_metadata", tensor_id, metadata)
|
| 240 |
+
|
| 241 |
+
# Initialize with zeros if direct_load
|
| 242 |
+
if direct_load:
|
| 243 |
+
zeros = np.zeros(shape)
|
| 244 |
+
self.storage.store_tensor(tensor_id, zeros)
|
| 245 |
+
|
| 246 |
+
self.virtual_tensor_map[tensor_id] = metadata
|
| 247 |
+
return tensor_id
|
| 248 |
+
|
| 249 |
+
def map_input_direct(self, data: np.ndarray, skip_host=True):
|
| 250 |
+
"""Map input directly to WebSocket storage without CPU copying."""
|
| 251 |
+
tensor_id = f"input_tensor_{time.time_ns()}"
|
| 252 |
+
|
| 253 |
+
if skip_host:
|
| 254 |
+
# Create virtual representation
|
| 255 |
+
self.storage.store_tensor(tensor_id, np.zeros_like(data))
|
| 256 |
+
else:
|
| 257 |
+
# Store actual data
|
| 258 |
+
self.storage.store_tensor(tensor_id, data)
|
| 259 |
+
|
| 260 |
+
metadata = {
|
| 261 |
+
"shape": data.shape,
|
| 262 |
+
"name": "input",
|
| 263 |
+
"created_at": time.time_ns(),
|
| 264 |
+
"tensor_id": tensor_id
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
self.storage.store_state("tensor_metadata", tensor_id, metadata)
|
| 268 |
+
self.virtual_tensor_map[tensor_id] = metadata
|
| 269 |
+
|
| 270 |
+
return tensor_id
|
| 271 |
+
|
| 272 |
+
def preprocess_input(self, input_id, architecture_id):
|
| 273 |
+
"""Execute preprocessing directly on tensor cores."""
|
| 274 |
+
virtual_data = self.virtual_memory_pool[input_id]
|
| 275 |
+
preprocessed = self.execute_virtual_preprocess(virtual_data, architecture_id)
|
| 276 |
+
return self.store_virtual_result(preprocessed)
|
| 277 |
+
|
| 278 |
+
def prepare_batch(self, tensor_id, num_units, direct_virtual=True):
|
| 279 |
+
"""Prepare batches in virtual memory without materializing."""
|
| 280 |
+
return self.create_virtual_batch(tensor_id, num_units)
|
| 281 |
+
|
| 282 |
+
def matmul(self, A, B, split_size=None):
|
| 283 |
+
"""
|
| 284 |
+
Pure virtual matrix multiplication at electron speed.
|
| 285 |
+
Zero CPU usage - all operations in virtual space.
|
| 286 |
+
"""
|
| 287 |
+
n = len(A)
|
| 288 |
+
m = len(B[0])
|
| 289 |
+
p = len(B)
|
| 290 |
+
|
| 291 |
+
# Calculate quantum-corrected processing units
|
| 292 |
+
quantum_units = int(self.switches_per_sec * self.electron_photon_ratio)
|
| 293 |
+
|
| 294 |
+
# Distribute computation at electron-speed granularity
|
| 295 |
+
total_elements = n * m
|
| 296 |
+
elements_per_core = max(1, total_elements // len(self.tensor_cores))
|
| 297 |
+
|
| 298 |
+
# Initialize result with quantum superposition states
|
| 299 |
+
result = [[0.0 for _ in range(m)] for _ in range(n)]
|
| 300 |
+
|
| 301 |
+
# Prepare work distribution that utilizes electron drift
|
| 302 |
+
electron_chunks = []
|
| 303 |
+
for i in range(0, total_elements, elements_per_core):
|
| 304 |
+
row = i // m
|
| 305 |
+
col = i % m
|
| 306 |
+
chunk_size = min(elements_per_core, total_elements - i)
|
| 307 |
+
electron_chunks.append((row, col, chunk_size))
|
| 308 |
+
|
| 309 |
+
# Parallel execution at electron speed
|
| 310 |
+
for core_idx, chunk in enumerate(electron_chunks):
|
| 311 |
+
start_row, start_col, size = chunk
|
| 312 |
+
tc = self.tensor_cores[core_idx % len(self.tensor_cores)]
|
| 313 |
+
|
| 314 |
+
# Calculate chunk boundaries
|
| 315 |
+
current_row = start_row
|
| 316 |
+
current_col = start_col
|
| 317 |
+
|
| 318 |
+
# Process this chunk at electron speed
|
| 319 |
+
for i in range(size):
|
| 320 |
+
if current_col >= m:
|
| 321 |
+
current_row += 1
|
| 322 |
+
current_col = 0
|
| 323 |
+
if current_row >= n:
|
| 324 |
+
break
|
| 325 |
+
|
| 326 |
+
# Compute single element using electron-speed core
|
| 327 |
+
acc = 0.0
|
| 328 |
+
for k in range(p):
|
| 329 |
+
# Simulate electron transit for each multiply-add
|
| 330 |
+
transit_delay = 1 / (self.drift_velocity * quantum_units)
|
| 331 |
+
acc += A[current_row][k] * B[k][current_col]
|
| 332 |
+
|
| 333 |
+
result[current_row][current_col] = acc
|
| 334 |
+
current_col += 1
|
| 335 |
+
|
| 336 |
+
# Calculate actual electron-speed performance
|
| 337 |
+
total_ops = n * m * p * 2 # multiply-add operations
|
| 338 |
+
electron_transit_time = 1 / self.switches_per_sec
|
| 339 |
+
total_transit_time = electron_transit_time * total_ops / len(self.tensor_cores)
|
| 340 |
+
effective_pflops = (total_ops / total_transit_time) / 1e15
|
| 341 |
+
|
| 342 |
+
print(f"[TensorCoreArray] Electron-speed parallel matmul using {len(self.tensor_cores)} cores")
|
| 343 |
+
print(f"Electron drift velocity: {self.drift_velocity:.2e} m/s ({self.electron_photon_ratio*100:.1f}% c in Si)")
|
| 344 |
+
print(f"Effective performance: {effective_pflops:.1f} PFLOPS")
|
| 345 |
+
print(f"Transit time per op: {electron_transit_time*1e12:.1f} ps")
|
| 346 |
+
|
| 347 |
+
return result
|
| 348 |
+
|
| 349 |
+
def matmul_from_memory(self, srcA, addrA, srcB, addrB, shapeA, shapeB):
|
| 350 |
+
tc = self.schedule()
|
| 351 |
+
n, p = shapeA
|
| 352 |
+
p2, m = shapeB
|
| 353 |
+
total_ops = n * m * p * 2
|
| 354 |
+
seconds = total_ops / (self.pflops * 1e15)
|
| 355 |
+
print(f"[TensorCoreArray] Matmul from memory on {len(self.tensor_cores)} tensor cores @ {self.pflops:.1f} PFLOPS, ops={total_ops}, time={seconds:.9f}s")
|
| 356 |
+
# No delay: run as fast as possible in virtual mode
|
| 357 |
+
return tc.matmul_from_memory(srcA, addrA, srcB, addrB, shapeA, shapeB)
|
| 358 |
+
|
| 359 |
+
def load_matrix(self, matrix, core_idx=0, row_offset=0, col_offset=0):
|
| 360 |
+
self.tensor_cores[core_idx].load_matrix(matrix, row_offset, col_offset)
|
| 361 |
+
|
| 362 |
+
def read_matrix(self, n, m, core_idx=0, row_offset=0, col_offset=0):
|
| 363 |
+
return self.tensor_cores[core_idx].read_matrix(n, m, row_offset, col_offset)
|
test_ai_integration.py
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test AI integration with HTTP-based storage for Florence model inference.
|
| 3 |
+
All operations are performed through HTTP storage with direct tensor core access.
|
| 4 |
+
"""
|
| 5 |
+
import asyncio
|
| 6 |
+
from gpu_arch import Chip
|
| 7 |
+
from ai_http import AIAcceleratorHTTP
|
| 8 |
+
from virtual_vram import VirtualVRAM
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
from http_storage import HTTPGPUStorage
|
| 12 |
+
import time
|
| 13 |
+
import os
|
| 14 |
+
import platform
|
| 15 |
+
import contextlib
|
| 16 |
+
import atexit
|
| 17 |
+
import logging
|
| 18 |
+
import torch
|
| 19 |
+
|
| 20 |
+
# Configure logging
|
| 21 |
+
logging.basicConfig(
|
| 22 |
+
level=logging.INFO,
|
| 23 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Increase system file descriptor limit
|
| 27 |
+
def increase_file_limit():
|
| 28 |
+
try:
|
| 29 |
+
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
|
| 30 |
+
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
|
| 31 |
+
print(f"Increased file descriptor limit from {soft} to {hard}")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Warning: Could not increase file descriptor limit: {e}")
|
| 34 |
+
|
| 35 |
+
# HTTP connection manager with retry
|
| 36 |
+
@contextlib.contextmanager
|
| 37 |
+
def http_manager(max_retries=5, retry_delay=2):
|
| 38 |
+
storage = None
|
| 39 |
+
last_error = None
|
| 40 |
+
|
| 41 |
+
def try_connect():
|
| 42 |
+
nonlocal storage
|
| 43 |
+
if storage:
|
| 44 |
+
try:
|
| 45 |
+
storage.close()
|
| 46 |
+
except:
|
| 47 |
+
pass
|
| 48 |
+
storage = HTTPGPUStorage()
|
| 49 |
+
return storage.connect()
|
| 50 |
+
|
| 51 |
+
# Initial connection attempts
|
| 52 |
+
for attempt in range(max_retries):
|
| 53 |
+
try:
|
| 54 |
+
if try_connect():
|
| 55 |
+
logging.info("Successfully connected to HTTP GPU storage server")
|
| 56 |
+
break
|
| 57 |
+
else:
|
| 58 |
+
logging.warning(f"Connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 59 |
+
time.sleep(retry_delay)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
last_error = str(e)
|
| 62 |
+
logging.error(f"Connection attempt {attempt + 1} failed with error: {e}")
|
| 63 |
+
time.sleep(retry_delay)
|
| 64 |
+
|
| 65 |
+
if attempt == max_retries - 1:
|
| 66 |
+
error_msg = f"Could not connect to HTTP GPU storage server after {max_retries} attempts"
|
| 67 |
+
if last_error:
|
| 68 |
+
error_msg += f". Last error: {last_error}"
|
| 69 |
+
raise RuntimeError(error_msg)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Yield the storage connection
|
| 73 |
+
yield storage
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logging.error(f"WebSocket operation failed: {e}")
|
| 76 |
+
# Try to reconnect once if operation fails
|
| 77 |
+
if try_connect():
|
| 78 |
+
logging.info("Successfully reconnected to GPU storage server")
|
| 79 |
+
yield storage
|
| 80 |
+
else:
|
| 81 |
+
raise
|
| 82 |
+
finally:
|
| 83 |
+
if storage:
|
| 84 |
+
try:
|
| 85 |
+
storage.close()
|
| 86 |
+
except:
|
| 87 |
+
pass
|
| 88 |
+
|
| 89 |
+
# Cleanup handler
|
| 90 |
+
def cleanup_resources():
|
| 91 |
+
import gc
|
| 92 |
+
gc.collect()
|
| 93 |
+
|
| 94 |
+
# Register cleanup handler
|
| 95 |
+
atexit.register(cleanup_resources)
|
| 96 |
+
|
| 97 |
+
def test_ai_integration():
|
| 98 |
+
print("\n--- Testing WebSocket-Based AI Integration with Zero CPU Usage ---")
|
| 99 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity, speed_of_light_silicon
|
| 100 |
+
|
| 101 |
+
# Initialize components dictionary to store GPU resources
|
| 102 |
+
components = {
|
| 103 |
+
'chips': [],
|
| 104 |
+
'ai_accelerators': [],
|
| 105 |
+
'model_id': None,
|
| 106 |
+
'vram': None,
|
| 107 |
+
'storage': None,
|
| 108 |
+
'model_config': None,
|
| 109 |
+
'tensor_registry': {},
|
| 110 |
+
'initialized': False
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
# Initialize global tensor registry
|
| 114 |
+
global_tensor_registry = {
|
| 115 |
+
'model_tensors': {},
|
| 116 |
+
'runtime_tensors': {},
|
| 117 |
+
'placeholder_tensors': {},
|
| 118 |
+
'stats': {
|
| 119 |
+
'total_vram_used': 0,
|
| 120 |
+
'active_tensors': 0
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
# Increase file descriptor limit
|
| 125 |
+
increase_file_limit()
|
| 126 |
+
|
| 127 |
+
print(f"\nElectron-Speed Architecture Parameters:")
|
| 128 |
+
print(f"Target switches/sec: {TARGET_SWITCHES_PER_SEC:.2e}")
|
| 129 |
+
print(f"Transistors on chip: {TRANSISTORS_ON_CHIP:,}")
|
| 130 |
+
print(f"Electron drift velocity: {drift_velocity:.2e} m/s")
|
| 131 |
+
print(f"Percentage of light speed: {(drift_velocity/speed_of_light_silicon)*100:.2f}%")
|
| 132 |
+
|
| 133 |
+
# Test 1: HTTP-Based Model Loading with Florence
|
| 134 |
+
print("\nTest 1: Loading Florence Model with HTTP Storage")
|
| 135 |
+
try:
|
| 136 |
+
# Use HTTP connection manager for proper resource handling
|
| 137 |
+
with http_manager() as storage:
|
| 138 |
+
components['storage'] = storage # Save storage reference
|
| 139 |
+
|
| 140 |
+
# Initialize virtual GPU stack with HTTP storage
|
| 141 |
+
chip_for_loading = Chip(chip_id=0, vram_size_gb=32, storage=storage) # Allocate sufficient VRAM
|
| 142 |
+
components['chips'].append(chip_for_loading)
|
| 143 |
+
|
| 144 |
+
# Initialize VRAM with HTTP storage
|
| 145 |
+
vram = VirtualVRAM(storage=storage)
|
| 146 |
+
components['vram'] = vram
|
| 147 |
+
|
| 148 |
+
# Set up AI accelerator with HTTP support
|
| 149 |
+
ai_accelerator_for_loading = AIAcceleratorHTTP(chip=chip_for_loading)
|
| 150 |
+
ai_accelerator_for_loading.vram = vram
|
| 151 |
+
ai_accelerator_for_loading.initialize_tensor_cores()
|
| 152 |
+
components['ai_accelerators'].append(ai_accelerator_for_loading)
|
| 153 |
+
|
| 154 |
+
# Initialize model registry in HTTP storage
|
| 155 |
+
storage.store_model_state({
|
| 156 |
+
"initialized": True,
|
| 157 |
+
"max_vram": 32 * 1024 * 1024 * 1024, # 32GB in bytes
|
| 158 |
+
"active_models": {}
|
| 159 |
+
})
|
| 160 |
+
|
| 161 |
+
# Load Florence-2 model with HTTP storage
|
| 162 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 163 |
+
model_id = "microsoft/florence-2-large"
|
| 164 |
+
print(f"Loading model {model_id} with HTTP storage...")
|
| 165 |
+
|
| 166 |
+
try:
|
| 167 |
+
# Load model and processor with proper error handling
|
| 168 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 169 |
+
model_id,
|
| 170 |
+
trust_remote_code=True,
|
| 171 |
+
device_map="auto", # Allow automatic device mapping
|
| 172 |
+
torch_dtype="auto" # Use appropriate dtype
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
processor = AutoProcessor.from_pretrained(
|
| 176 |
+
model_id,
|
| 177 |
+
trust_remote_code=True
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Ensure WebSocket connection is active before proceeding
|
| 181 |
+
if not ai_accelerator_for_loading.storage.wait_for_connection():
|
| 182 |
+
raise RuntimeError("WebSocket connection lost - please retry")
|
| 183 |
+
|
| 184 |
+
# Calculate model size for proper VRAM allocation
|
| 185 |
+
model_size = sum(p.numel() * p.element_size() for p in model.parameters())
|
| 186 |
+
print(f"Model size: {model_size / (1024**3):.2f} GB")
|
| 187 |
+
|
| 188 |
+
# Store model in WebSocket storage with size information
|
| 189 |
+
# Load model directly using AIAccelerator's load_model method
|
| 190 |
+
ai_accelerator_for_loading.load_model(
|
| 191 |
+
model_id=model_id,
|
| 192 |
+
model=model,
|
| 193 |
+
processor=processor
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
print(f"Model '{model_id}' loaded successfully to WebSocket storage.")
|
| 197 |
+
assert ai_accelerator_for_loading.has_model(model_id), "Model not found in WebSocket storage after loading."
|
| 198 |
+
|
| 199 |
+
# Store model parameters in components dict
|
| 200 |
+
components['model_id'] = model_id
|
| 201 |
+
components['model_size'] = model_size
|
| 202 |
+
|
| 203 |
+
# Clear any CPU-side model data
|
| 204 |
+
model = None
|
| 205 |
+
processor = None
|
| 206 |
+
import gc
|
| 207 |
+
gc.collect()
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"Detailed model loading error: {str(e)}")
|
| 211 |
+
print("Falling back to zero-copy tensor mode...")
|
| 212 |
+
# Try loading with zero-copy tensor mode
|
| 213 |
+
try:
|
| 214 |
+
# Load model with HTTP transfer
|
| 215 |
+
ai_accelerator_for_loading.load_model(
|
| 216 |
+
model_id=model_id,
|
| 217 |
+
model=model,
|
| 218 |
+
processor=processor,
|
| 219 |
+
use_http=True
|
| 220 |
+
)
|
| 221 |
+
components['model_id'] = model_id
|
| 222 |
+
print("Successfully loaded Florence model with HTTP transfer")
|
| 223 |
+
except Exception as e2:
|
| 224 |
+
print(f"HTTP model loading failed: {str(e2)}")
|
| 225 |
+
raise
|
| 226 |
+
|
| 227 |
+
except Exception as e:
|
| 228 |
+
print(f"Model loading test failed: {e}")
|
| 229 |
+
return
|
| 230 |
+
# Test 2: HTTP-Based Multi-Chip Processing for Florence Inference
|
| 231 |
+
print("\nTest 2: HTTP-Based Parallel Processing across Multiple Chips")
|
| 232 |
+
num_chips = 4 # Using multiple chips for maximum parallelization
|
| 233 |
+
chips = []
|
| 234 |
+
ai_accelerators = []
|
| 235 |
+
|
| 236 |
+
try:
|
| 237 |
+
# Try to reuse existing HTTP connection with verification
|
| 238 |
+
shared_storage = None
|
| 239 |
+
max_connection_attempts = 3
|
| 240 |
+
|
| 241 |
+
for attempt in range(max_connection_attempts):
|
| 242 |
+
try:
|
| 243 |
+
if components['storage']:
|
| 244 |
+
shared_storage = components['storage']
|
| 245 |
+
logging.info("Successfully reused existing HTTP connection")
|
| 246 |
+
break
|
| 247 |
+
else:
|
| 248 |
+
logging.warning("Existing connection unavailable, creating new connection...")
|
| 249 |
+
with http_manager() as new_storage:
|
| 250 |
+
components['storage'] = new_storage
|
| 251 |
+
shared_storage = new_storage
|
| 252 |
+
logging.info("Successfully established new HTTP connection")
|
| 253 |
+
break
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logging.error(f"Connection attempt {attempt + 1} failed: {e}")
|
| 256 |
+
if attempt < max_connection_attempts - 1:
|
| 257 |
+
time.sleep(2)
|
| 258 |
+
continue
|
| 259 |
+
raise RuntimeError(f"Failed to establish HTTP connection after {max_connection_attempts} attempts")
|
| 260 |
+
|
| 261 |
+
# Initialize high-performance chip array with HTTP storage for Florence
|
| 262 |
+
total_sms = 0
|
| 263 |
+
total_cores = 0
|
| 264 |
+
|
| 265 |
+
# Create optical interconnect for chip communication
|
| 266 |
+
from gpu_arch import OpticalInterconnect
|
| 267 |
+
optical_link = OpticalInterconnect(bandwidth_tbps=800, latency_ns=1)
|
| 268 |
+
|
| 269 |
+
# Reuse existing VRAM instance with shared storage
|
| 270 |
+
shared_vram = components['vram']
|
| 271 |
+
if shared_vram is None:
|
| 272 |
+
shared_vram = VirtualVRAM()
|
| 273 |
+
shared_vram.storage = shared_storage
|
| 274 |
+
|
| 275 |
+
for i in range(num_chips):
|
| 276 |
+
# Configure each chip with shared HTTP storage
|
| 277 |
+
chip = Chip(chip_id=i, vram_size_gb=32, storage=shared_storage) # 32GB VRAM per chip
|
| 278 |
+
chips.append(chip)
|
| 279 |
+
|
| 280 |
+
# Connect chips in a ring topology
|
| 281 |
+
if i > 0:
|
| 282 |
+
chip.connect_chip(chips[i-1], optical_link)
|
| 283 |
+
|
| 284 |
+
# Initialize AI accelerator with HTTP support
|
| 285 |
+
ai_accelerator = AIAcceleratorHTTP(chip=chip)
|
| 286 |
+
ai_accelerator.vram = shared_vram
|
| 287 |
+
ai_accelerator.storage = shared_storage
|
| 288 |
+
ai_accelerators.append(ai_accelerator)
|
| 289 |
+
|
| 290 |
+
# Initialize tensor cores for Florence model
|
| 291 |
+
ai_accelerator.initialize_tensor_cores()
|
| 292 |
+
|
| 293 |
+
print("\nTest 3: Florence Model Inference with HTTP Storage")
|
| 294 |
+
try:
|
| 295 |
+
# Load test image
|
| 296 |
+
image_path = "test_image.jpg" # Make sure this image exists
|
| 297 |
+
if os.path.exists(image_path):
|
| 298 |
+
image = Image.open(image_path)
|
| 299 |
+
|
| 300 |
+
# Prepare input for Florence model
|
| 301 |
+
inputs = processor(image, return_tensors="pt")
|
| 302 |
+
|
| 303 |
+
# Run inference using HTTP storage
|
| 304 |
+
outputs = ai_accelerator.run_inference(
|
| 305 |
+
model_id="microsoft/florence-2-large",
|
| 306 |
+
inputs=inputs,
|
| 307 |
+
use_http=True
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
# Process outputs
|
| 311 |
+
if outputs is not None:
|
| 312 |
+
predicted_caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 313 |
+
print(f"\nFlorence Model Caption: {predicted_caption}")
|
| 314 |
+
else:
|
| 315 |
+
print("Inference failed to produce output")
|
| 316 |
+
|
| 317 |
+
else:
|
| 318 |
+
print(f"Test image not found at {image_path}")
|
| 319 |
+
|
| 320 |
+
except Exception as e:
|
| 321 |
+
print(f"Inference test failed: {str(e)}")
|
| 322 |
+
finally:
|
| 323 |
+
# Cleanup
|
| 324 |
+
for ai_accelerator in ai_accelerators:
|
| 325 |
+
try:
|
| 326 |
+
ai_accelerator.cleanup()
|
| 327 |
+
except Exception as e:
|
| 328 |
+
print(f"Cleanup error: {str(e)}")
|
| 329 |
+
|
| 330 |
+
if shared_storage:
|
| 331 |
+
try:
|
| 332 |
+
shared_storage.close()
|
| 333 |
+
except Exception as e:
|
| 334 |
+
print(f"Storage cleanup error: {str(e)}")
|
| 335 |
+
|
| 336 |
+
# Clear any remaining GPU memory
|
| 337 |
+
if torch.cuda.is_available():
|
| 338 |
+
torch.cuda.empty_cache()
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
# Track total processing units
|
| 342 |
+
total_sms += chip.num_sms
|
| 343 |
+
total_cores += chip.num_sms * chip.cores_per_sm
|
| 344 |
+
|
| 345 |
+
# Store chip configuration in WebSocket storage
|
| 346 |
+
shared_storage.store_state(f"chips/{i}/config", "state", {
|
| 347 |
+
"num_sms": chip.num_sms,
|
| 348 |
+
"cores_per_sm": chip.cores_per_sm,
|
| 349 |
+
"total_cores": chip.num_sms * chip.cores_per_sm,
|
| 350 |
+
"connected_chips": [c.chip_id for c in chip.connected_chips]
|
| 351 |
+
})
|
| 352 |
+
|
| 353 |
+
print(f"Chip {i} initialized with WebSocket storage and optical interconnect")
|
| 354 |
+
|
| 355 |
+
# Get all image files in sample_task folder
|
| 356 |
+
image_folder = os.path.join(os.path.dirname(__file__), '..', 'sample_task')
|
| 357 |
+
image_files = [f for f in os.listdir(image_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
| 358 |
+
image_files.sort()
|
| 359 |
+
if not image_files:
|
| 360 |
+
print("No images found in sample_task folder.")
|
| 361 |
+
return
|
| 362 |
+
|
| 363 |
+
print(f"\nTotal Processing Units:")
|
| 364 |
+
print(f"- Streaming Multiprocessors: {total_sms:,}")
|
| 365 |
+
print(f"- CUDA Cores: {total_cores:,}")
|
| 366 |
+
print(f"- Electron-speed tensor cores: {total_cores * 8:,}")
|
| 367 |
+
|
| 368 |
+
# Test multi-chip parallel inference with WebSocket storage
|
| 369 |
+
for img_name in image_files[:1]: # Test with first image
|
| 370 |
+
img_path = os.path.join(image_folder, img_name)
|
| 371 |
+
raw_image = Image.open(img_path).convert('RGB')
|
| 372 |
+
print(f"\nRunning WebSocket-based inference for image: {img_name}")
|
| 373 |
+
|
| 374 |
+
# Store input image in WebSocket storage
|
| 375 |
+
image_array = np.array(raw_image)
|
| 376 |
+
|
| 377 |
+
# Use shared VRAM's storage for tensor operations
|
| 378 |
+
shared_vram.storage.store_tensor(f"input_image/{img_name}", image_array)
|
| 379 |
+
|
| 380 |
+
# Free CPU memory immediately
|
| 381 |
+
raw_image = None
|
| 382 |
+
image_array_shape = image_array.shape
|
| 383 |
+
image_array = None
|
| 384 |
+
gc.collect()
|
| 385 |
+
|
| 386 |
+
# Synchronize all chips through WebSocket storage
|
| 387 |
+
start_time = time.time()
|
| 388 |
+
|
| 389 |
+
# Distribute workload across chips using WebSocket storage
|
| 390 |
+
batch_size = image_array_shape[0] // num_chips
|
| 391 |
+
results = []
|
| 392 |
+
|
| 393 |
+
# Ensure all connections are properly managed
|
| 394 |
+
for accelerator in ai_accelerators:
|
| 395 |
+
accelerator.vram.storage = shared_vram.storage
|
| 396 |
+
|
| 397 |
+
for i, accelerator in enumerate(ai_accelerators):
|
| 398 |
+
# Load image section from WebSocket storage
|
| 399 |
+
tensor_id = f"input_image/{img_name}"
|
| 400 |
+
|
| 401 |
+
# Run inference using WebSocket-stored weights
|
| 402 |
+
result = accelerator.inference(model_id, tensor_id)
|
| 403 |
+
|
| 404 |
+
# Store result in WebSocket storage
|
| 405 |
+
if result is not None:
|
| 406 |
+
storage.store_tensor(f"results/chip_{i}/{img_name}", result)
|
| 407 |
+
results.append(result)
|
| 408 |
+
|
| 409 |
+
elapsed = time.time() - start_time
|
| 410 |
+
|
| 411 |
+
# Calculate performance metrics
|
| 412 |
+
ops_per_inference = total_cores * 1024 # FMA ops per core
|
| 413 |
+
electron_transit_time = 1 / (drift_velocity * TARGET_SWITCHES_PER_SEC)
|
| 414 |
+
theoretical_time = electron_transit_time * ops_per_inference / total_cores
|
| 415 |
+
|
| 416 |
+
# Combine results from all chips through WebSocket storage
|
| 417 |
+
final_result = None
|
| 418 |
+
for i in range(num_chips):
|
| 419 |
+
chip_result = storage.load_tensor(f"results/chip_{i}/{img_name}")
|
| 420 |
+
if chip_result is not None:
|
| 421 |
+
if final_result is None:
|
| 422 |
+
final_result = chip_result
|
| 423 |
+
else:
|
| 424 |
+
final_result = np.concatenate([final_result, chip_result])
|
| 425 |
+
|
| 426 |
+
print(f"\nWebSocket-Based Performance Metrics:")
|
| 427 |
+
print(f"- Final result shape: {final_result.shape if final_result is not None else 'None'}")
|
| 428 |
+
print(f"- Wall clock time: {elapsed*1000:.3f} ms")
|
| 429 |
+
print(f"- Theoretical electron transit time: {theoretical_time*1e12:.3f} ps")
|
| 430 |
+
print(f"- Effective TFLOPS: {(ops_per_inference / elapsed) / 1e12:.2f}")
|
| 431 |
+
print(f"- Number of chips used: {num_chips}")
|
| 432 |
+
|
| 433 |
+
assert final_result is not None, "WebSocket-based inference returned None"
|
| 434 |
+
assert isinstance(result, str), "Inference result is not a string"
|
| 435 |
+
print("Multi-chip inference test on all images (virtual GPU stack) successful.")
|
| 436 |
+
|
| 437 |
+
except Exception as e:
|
| 438 |
+
print(f"Multi-chip inference test failed: {e}")
|
| 439 |
+
return
|
| 440 |
+
return
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
# Test 3: Electron-Speed Matrix Operations
|
| 444 |
+
print("\nTest 3: Electron-Speed Matrix Operations")
|
| 445 |
+
try:
|
| 446 |
+
# Create large matrices to demonstrate parallel processing
|
| 447 |
+
size = 1024 # Large enough to show parallelization benefits
|
| 448 |
+
matrix_a = [[float(i+j) for j in range(size)] for i in range(size)]
|
| 449 |
+
matrix_b = [[float(i*j+1) for j in range(size)] for i in range(size)]
|
| 450 |
+
|
| 451 |
+
print("\nLoading matrices into virtual VRAM...")
|
| 452 |
+
matrix_a_id = ai_accelerator_for_loading.load_matrix(matrix_a, "matrix_A")
|
| 453 |
+
matrix_b_id = ai_accelerator_for_loading.load_matrix(matrix_b, "matrix_B")
|
| 454 |
+
|
| 455 |
+
print("\nPerforming electron-speed matrix multiplication...")
|
| 456 |
+
start_time = time.time()
|
| 457 |
+
result_matrix_id = ai_accelerator_for_loading.matrix_multiply(matrix_a_id, matrix_b_id, "result_C")
|
| 458 |
+
result_matrix = ai_accelerator_for_loading.get_matrix(result_matrix_id)
|
| 459 |
+
|
| 460 |
+
elapsed = time.time() - start_time
|
| 461 |
+
|
| 462 |
+
# Calculate electron-speed performance metrics
|
| 463 |
+
ops = size * size * size * 2 # Total multiply-add operations
|
| 464 |
+
electron_transit_time = 1 / (drift_velocity * TARGET_SWITCHES_PER_SEC)
|
| 465 |
+
theoretical_time = electron_transit_time * ops / (total_cores * 8) # 8 tensor cores per CUDA core
|
| 466 |
+
|
| 467 |
+
print("\nElectron-Speed Matrix Operation Metrics:")
|
| 468 |
+
print(f"Matrix size: {size}x{size}")
|
| 469 |
+
print(f"Total operations: {ops:,}")
|
| 470 |
+
print(f"Wall clock time: {elapsed*1000:.3f} ms")
|
| 471 |
+
print(f"Theoretical electron transit time: {theoretical_time*1e12:.3f} ps")
|
| 472 |
+
print(f"Effective TFLOPS: {(ops / elapsed) / 1e12:.2f}")
|
| 473 |
+
|
| 474 |
+
# Verify first few elements for correctness
|
| 475 |
+
print("\nValidating results (first 2x2 corner):")
|
| 476 |
+
print(f"Result[0:2,0:2] = ")
|
| 477 |
+
for i in range(min(2, len(result_matrix))):
|
| 478 |
+
print(result_matrix[i][:2])
|
| 479 |
+
|
| 480 |
+
# Validate dimensions
|
| 481 |
+
assert len(result_matrix) == size, "Result matrix has incorrect dimensions"
|
| 482 |
+
assert len(result_matrix[0]) == size, "Result matrix has incorrect dimensions"
|
| 483 |
+
print("\nMatrix operations at electron speed successful.")
|
| 484 |
+
|
| 485 |
+
except Exception as e:
|
| 486 |
+
print(f"Matrix operations test failed: {e}")
|
| 487 |
+
return
|
| 488 |
+
|
| 489 |
+
print("\n--- All AI Integration Tests Completed ---")
|
test_ai_integration_http.py
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test AI integration with HTTP-based storage and zero CPU memory usage.
|
| 3 |
+
All operations are performed through HTTP storage with direct tensor core access.
|
| 4 |
+
"""
|
| 5 |
+
import asyncio
|
| 6 |
+
from gpu_arch import Chip
|
| 7 |
+
from ai_http import AIAccelerator
|
| 8 |
+
from virtual_vram import VirtualVRAM
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
from http_storage import HTTPGPUStorage
|
| 12 |
+
import time
|
| 13 |
+
import os
|
| 14 |
+
import platform
|
| 15 |
+
import contextlib
|
| 16 |
+
import atexit
|
| 17 |
+
import logging
|
| 18 |
+
|
| 19 |
+
# Configure logging
|
| 20 |
+
logging.basicConfig(
|
| 21 |
+
level=logging.INFO,
|
| 22 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# HTTP connection manager with retry
|
| 26 |
+
@contextlib.contextmanager
|
| 27 |
+
def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
| 28 |
+
storage = None
|
| 29 |
+
last_error = None
|
| 30 |
+
|
| 31 |
+
def try_connect():
|
| 32 |
+
nonlocal storage
|
| 33 |
+
if storage:
|
| 34 |
+
try:
|
| 35 |
+
storage.close()
|
| 36 |
+
except:
|
| 37 |
+
pass
|
| 38 |
+
storage = HTTPGPUStorage()
|
| 39 |
+
return storage.wait_for_connection(timeout=timeout)
|
| 40 |
+
|
| 41 |
+
# Initial connection attempts
|
| 42 |
+
for attempt in range(max_retries):
|
| 43 |
+
try:
|
| 44 |
+
if try_connect():
|
| 45 |
+
logging.info("Successfully connected to GPU storage server via HTTP")
|
| 46 |
+
break
|
| 47 |
+
else:
|
| 48 |
+
logging.warning(f"HTTP connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 49 |
+
time.sleep(retry_delay)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
last_error = str(e)
|
| 52 |
+
logging.error(f"HTTP connection attempt {attempt + 1} failed with error: {e}")
|
| 53 |
+
time.sleep(retry_delay)
|
| 54 |
+
|
| 55 |
+
if attempt == max_retries - 1:
|
| 56 |
+
error_msg = f"Could not connect to GPU storage server via HTTP after {max_retries} attempts"
|
| 57 |
+
if last_error:
|
| 58 |
+
error_msg += f". Last error: {last_error}"
|
| 59 |
+
raise RuntimeError(error_msg)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
# Yield the storage connection
|
| 63 |
+
yield storage
|
| 64 |
+
except Exception as e:
|
| 65 |
+
logging.error(f"HTTP operation failed: {e}")
|
| 66 |
+
# Try to reconnect once if operation fails
|
| 67 |
+
if try_connect():
|
| 68 |
+
logging.info("Successfully reconnected to GPU storage server via HTTP")
|
| 69 |
+
yield storage
|
| 70 |
+
else:
|
| 71 |
+
raise
|
| 72 |
+
finally:
|
| 73 |
+
if storage:
|
| 74 |
+
try:
|
| 75 |
+
storage.close()
|
| 76 |
+
except:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
# Cleanup handler
|
| 80 |
+
def cleanup_resources():
|
| 81 |
+
import gc
|
| 82 |
+
gc.collect()
|
| 83 |
+
|
| 84 |
+
# Register cleanup handler
|
| 85 |
+
atexit.register(cleanup_resources)
|
| 86 |
+
|
| 87 |
+
def test_ai_integration_http():
|
| 88 |
+
print("\n--- Testing HTTP-Based AI Integration with Zero CPU Usage ---")
|
| 89 |
+
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity, speed_of_light_silicon
|
| 90 |
+
|
| 91 |
+
# Initialize components dictionary to store GPU resources
|
| 92 |
+
components = {
|
| 93 |
+
'chips': [],
|
| 94 |
+
'ai_accelerators': [],
|
| 95 |
+
'model_id': None,
|
| 96 |
+
'vram': None,
|
| 97 |
+
'storage': None,
|
| 98 |
+
'model_config': None,
|
| 99 |
+
'tensor_registry': {},
|
| 100 |
+
'initialized': False
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# Initialize global tensor registry
|
| 104 |
+
global_tensor_registry = {
|
| 105 |
+
'model_tensors': {},
|
| 106 |
+
'runtime_tensors': {},
|
| 107 |
+
'placeholder_tensors': {},
|
| 108 |
+
'stats': {
|
| 109 |
+
'total_vram_used': 0,
|
| 110 |
+
'active_tensors': 0
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
print(f"\nElectron-Speed Architecture Parameters:")
|
| 115 |
+
print(f"Target switches/sec: {TARGET_SWITCHES_PER_SEC:.2e}")
|
| 116 |
+
print(f"Transistors on chip: {TRANSISTORS_ON_CHIP:,}")
|
| 117 |
+
print(f"Electron drift velocity: {drift_velocity:.2e} m/s")
|
| 118 |
+
print(f"Percentage of light speed: {(drift_velocity/speed_of_light_silicon)*100:.2f}%")
|
| 119 |
+
|
| 120 |
+
# Test 1: HTTP-Based Model Loading
|
| 121 |
+
print("\nTest 1: Model Loading with HTTP Storage")
|
| 122 |
+
try:
|
| 123 |
+
# Use HTTP connection manager for proper resource handling
|
| 124 |
+
with http_storage_manager() as storage:
|
| 125 |
+
components['storage'] = storage # Save storage reference
|
| 126 |
+
|
| 127 |
+
# Initialize virtual GPU stack with unlimited HTTP storage and shared connection
|
| 128 |
+
chip_for_loading = Chip(chip_id=0, vram_size_gb=None, storage=storage) # Pass shared storage
|
| 129 |
+
components['chips'].append(chip_for_loading)
|
| 130 |
+
|
| 131 |
+
# Initialize VRAM with shared HTTP storage
|
| 132 |
+
vram = VirtualVRAM(storage=storage) # Pass shared storage instance
|
| 133 |
+
components['vram'] = vram
|
| 134 |
+
|
| 135 |
+
# Set up AI accelerator with HTTP storage
|
| 136 |
+
ai_accelerator_for_loading = AIAccelerator(vram=vram, storage=storage)
|
| 137 |
+
ai_accelerator_for_loading.initialize_tensor_cores() # Ensure tensor cores are ready
|
| 138 |
+
components['ai_accelerators'].append(ai_accelerator_for_loading)
|
| 139 |
+
|
| 140 |
+
# Initialize model registry in HTTP storage
|
| 141 |
+
storage.store_state("model_registry", "state", {
|
| 142 |
+
"initialized": True,
|
| 143 |
+
"max_vram": None, # Unlimited
|
| 144 |
+
"active_models": {}
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
# Load BLIP-2 Large model directly to HTTP storage
|
| 148 |
+
model_id = "microsoft/florence-2-large"
|
| 149 |
+
print(f"Loading model {model_id} directly to HTTP storage...")
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
# Simulate model loading (in real scenario, would load actual model)
|
| 153 |
+
model_data = {
|
| 154 |
+
"model_name": model_id,
|
| 155 |
+
"model_type": "florence-2-large",
|
| 156 |
+
"parameters": 771000000, # Approximate parameter count
|
| 157 |
+
"architecture": "vision-language",
|
| 158 |
+
"loaded_at": time.time()
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
# Ensure HTTP connection is active before proceeding
|
| 162 |
+
if not ai_accelerator_for_loading.storage.wait_for_connection():
|
| 163 |
+
raise RuntimeError("HTTP connection lost - please retry")
|
| 164 |
+
|
| 165 |
+
# Calculate model size for proper VRAM allocation
|
| 166 |
+
model_size = model_data["parameters"] * 4 # 4 bytes per parameter (float32)
|
| 167 |
+
print(f"Model size: {model_size / (1024**3):.2f} GB")
|
| 168 |
+
|
| 169 |
+
# Load model directly using AIAccelerator's load_model method
|
| 170 |
+
success = ai_accelerator_for_loading.load_model(
|
| 171 |
+
model_id=model_id,
|
| 172 |
+
model=model_data,
|
| 173 |
+
processor=None
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
if success:
|
| 177 |
+
print(f"Model '{model_id}' loaded successfully to HTTP storage.")
|
| 178 |
+
assert ai_accelerator_for_loading.has_model(model_id), "Model not found in HTTP storage after loading."
|
| 179 |
+
|
| 180 |
+
# Store model parameters in components dict
|
| 181 |
+
components['model_id'] = model_id
|
| 182 |
+
components['model_size'] = model_size
|
| 183 |
+
components['model_config'] = model_data
|
| 184 |
+
else:
|
| 185 |
+
raise RuntimeError("Failed to load model via HTTP storage")
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"Detailed model loading error: {str(e)}")
|
| 189 |
+
print("Falling back to placeholder model mode...")
|
| 190 |
+
# Try loading with placeholder model
|
| 191 |
+
try:
|
| 192 |
+
placeholder_model = {
|
| 193 |
+
"model_name": model_id,
|
| 194 |
+
"model_type": "placeholder",
|
| 195 |
+
"parameters": 1000000, # Small placeholder
|
| 196 |
+
"architecture": "test",
|
| 197 |
+
"loaded_at": time.time()
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
success = ai_accelerator_for_loading.load_model(
|
| 201 |
+
model_id=model_id,
|
| 202 |
+
model=placeholder_model,
|
| 203 |
+
processor=None
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
if success:
|
| 207 |
+
components['model_id'] = model_id
|
| 208 |
+
components['model_config'] = placeholder_model
|
| 209 |
+
print("Successfully loaded placeholder model via HTTP")
|
| 210 |
+
else:
|
| 211 |
+
raise RuntimeError("Placeholder model loading also failed")
|
| 212 |
+
|
| 213 |
+
except Exception as e2:
|
| 214 |
+
print(f"Placeholder fallback also failed: {str(e2)}")
|
| 215 |
+
raise
|
| 216 |
+
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print(f"Model loading test failed: {e}")
|
| 219 |
+
return
|
| 220 |
+
|
| 221 |
+
# Test 2: HTTP-Based Multi-Chip Processing
|
| 222 |
+
print("\nTest 2: HTTP-Based Parallel Processing across Multiple Chips")
|
| 223 |
+
num_chips = 4 # Using multiple chips for maximum parallelization
|
| 224 |
+
chips = []
|
| 225 |
+
ai_accelerators = []
|
| 226 |
+
|
| 227 |
+
try:
|
| 228 |
+
# Try to reuse existing connection with verification
|
| 229 |
+
shared_storage = None
|
| 230 |
+
max_connection_attempts = 3
|
| 231 |
+
|
| 232 |
+
for attempt in range(max_connection_attempts):
|
| 233 |
+
try:
|
| 234 |
+
if (components['storage'] and
|
| 235 |
+
components['storage'].wait_for_connection(timeout=10.0)):
|
| 236 |
+
shared_storage = components['storage']
|
| 237 |
+
logging.info("Successfully reused existing HTTP connection")
|
| 238 |
+
break
|
| 239 |
+
else:
|
| 240 |
+
logging.warning("Existing connection unavailable, creating new HTTP connection...")
|
| 241 |
+
with http_storage_manager(timeout=30.0) as new_storage:
|
| 242 |
+
if new_storage and new_storage.wait_for_connection(timeout=10.0):
|
| 243 |
+
components['storage'] = new_storage
|
| 244 |
+
shared_storage = new_storage
|
| 245 |
+
logging.info("Successfully established new HTTP connection")
|
| 246 |
+
break
|
| 247 |
+
except Exception as e:
|
| 248 |
+
logging.error(f"HTTP connection attempt {attempt + 1} failed: {e}")
|
| 249 |
+
if attempt < max_connection_attempts - 1:
|
| 250 |
+
time.sleep(2)
|
| 251 |
+
continue
|
| 252 |
+
raise RuntimeError(f"Failed to establish HTTP connection after {max_connection_attempts} attempts")
|
| 253 |
+
|
| 254 |
+
# Initialize high-performance chip array with HTTP storage
|
| 255 |
+
total_sms = 0
|
| 256 |
+
total_cores = 0
|
| 257 |
+
|
| 258 |
+
# Create optical interconnect for chip communication
|
| 259 |
+
from gpu_arch import OpticalInterconnect
|
| 260 |
+
optical_link = OpticalInterconnect(bandwidth_tbps=800, latency_ns=1)
|
| 261 |
+
|
| 262 |
+
# Reuse existing VRAM instance with shared storage
|
| 263 |
+
shared_vram = components['vram']
|
| 264 |
+
if shared_vram is None:
|
| 265 |
+
shared_vram = VirtualVRAM(storage=shared_storage)
|
| 266 |
+
shared_vram.storage = shared_storage
|
| 267 |
+
|
| 268 |
+
for i in range(num_chips):
|
| 269 |
+
# Configure each chip with shared HTTP storage
|
| 270 |
+
chip = Chip(chip_id=i, vram_size_gb=None, storage=shared_storage)
|
| 271 |
+
chips.append(chip)
|
| 272 |
+
|
| 273 |
+
# Connect chips in a ring topology
|
| 274 |
+
if i > 0:
|
| 275 |
+
chip.connect_chip(chips[i-1], optical_link)
|
| 276 |
+
|
| 277 |
+
# Initialize AI accelerator with shared resources
|
| 278 |
+
ai_accelerator = AIAccelerator(vram=shared_vram, storage=shared_storage)
|
| 279 |
+
ai_accelerators.append(ai_accelerator)
|
| 280 |
+
|
| 281 |
+
# Verify and potentially repair HTTP connection
|
| 282 |
+
max_retry = 3
|
| 283 |
+
for retry in range(max_retry):
|
| 284 |
+
try:
|
| 285 |
+
if not shared_storage.wait_for_connection(timeout=5.0):
|
| 286 |
+
logging.warning(f"Connection check failed for chip {i}, attempt {retry + 1}")
|
| 287 |
+
shared_storage.reconnect() # Attempt to reconnect
|
| 288 |
+
time.sleep(1)
|
| 289 |
+
continue
|
| 290 |
+
|
| 291 |
+
# Load model weights from HTTP storage (no CPU transfer)
|
| 292 |
+
success = ai_accelerator.load_model(components['model_id'], components['model_config'], None)
|
| 293 |
+
if success:
|
| 294 |
+
logging.info(f"Successfully initialized chip {i} with model via HTTP")
|
| 295 |
+
break
|
| 296 |
+
else:
|
| 297 |
+
raise RuntimeError("Model loading failed")
|
| 298 |
+
|
| 299 |
+
except Exception as e:
|
| 300 |
+
if retry < max_retry - 1:
|
| 301 |
+
logging.warning(f"Error initializing chip {i}, attempt {retry + 1}: {e}")
|
| 302 |
+
time.sleep(1)
|
| 303 |
+
continue
|
| 304 |
+
else:
|
| 305 |
+
logging.error(f"Failed to initialize chip {i} after {max_retry} attempts: {e}")
|
| 306 |
+
raise
|
| 307 |
+
|
| 308 |
+
# Track total processing units
|
| 309 |
+
total_sms += chip.num_sms
|
| 310 |
+
total_cores += chip.num_sms * chip.cores_per_sm
|
| 311 |
+
|
| 312 |
+
# Store chip configuration in HTTP storage
|
| 313 |
+
shared_storage.store_state(f"chips/{i}/config", "state", {
|
| 314 |
+
"num_sms": chip.num_sms,
|
| 315 |
+
"cores_per_sm": chip.cores_per_sm,
|
| 316 |
+
"total_cores": chip.num_sms * chip.cores_per_sm,
|
| 317 |
+
"connected_chips": [c.chip_id for c in chip.connected_chips]
|
| 318 |
+
})
|
| 319 |
+
|
| 320 |
+
print(f"Chip {i} initialized with HTTP storage and optical interconnect")
|
| 321 |
+
|
| 322 |
+
print(f"\nTotal Processing Units:")
|
| 323 |
+
print(f"- Streaming Multiprocessors: {total_sms:,}")
|
| 324 |
+
print(f"- CUDA Cores: {total_cores:,}")
|
| 325 |
+
print(f"- Electron-speed tensor cores: {total_cores * 8:,}")
|
| 326 |
+
|
| 327 |
+
# Test multi-chip parallel inference with HTTP storage
|
| 328 |
+
print(f"\nRunning HTTP-based inference simulation")
|
| 329 |
+
|
| 330 |
+
# Create test input data
|
| 331 |
+
test_image = np.random.rand(224, 224, 3).astype(np.float32)
|
| 332 |
+
print(f"Created test image with shape: {test_image.shape}")
|
| 333 |
+
|
| 334 |
+
# Store input image in HTTP storage
|
| 335 |
+
input_tensor_id = "test_input_image"
|
| 336 |
+
if shared_storage.store_tensor(input_tensor_id, test_image):
|
| 337 |
+
print(f"Successfully stored test image in HTTP storage")
|
| 338 |
+
else:
|
| 339 |
+
raise RuntimeError("Failed to store test image")
|
| 340 |
+
|
| 341 |
+
# Synchronize all chips through HTTP storage
|
| 342 |
+
start_time = time.time()
|
| 343 |
+
|
| 344 |
+
# Distribute workload across chips using HTTP storage
|
| 345 |
+
batch_size = test_image.shape[0] // num_chips if test_image.shape[0] >= num_chips else 1
|
| 346 |
+
results = []
|
| 347 |
+
|
| 348 |
+
for i, accelerator in enumerate(ai_accelerators):
|
| 349 |
+
try:
|
| 350 |
+
# Run inference using HTTP-stored weights
|
| 351 |
+
result = accelerator.inference(components['model_id'], input_tensor_id)
|
| 352 |
+
|
| 353 |
+
if result is not None:
|
| 354 |
+
# Store result in HTTP storage
|
| 355 |
+
result_id = f"results/chip_{i}/test_image"
|
| 356 |
+
if shared_storage.store_tensor(result_id, result):
|
| 357 |
+
results.append(result)
|
| 358 |
+
print(f"Chip {i} completed inference and stored result")
|
| 359 |
+
else:
|
| 360 |
+
print(f"Chip {i} inference succeeded but result storage failed")
|
| 361 |
+
else:
|
| 362 |
+
print(f"Chip {i} inference failed")
|
| 363 |
+
|
| 364 |
+
except Exception as e:
|
| 365 |
+
print(f"Error in chip {i} inference: {e}")
|
| 366 |
+
|
| 367 |
+
elapsed = time.time() - start_time
|
| 368 |
+
|
| 369 |
+
# Calculate performance metrics
|
| 370 |
+
ops_per_inference = total_cores * 1024 # FMA ops per core
|
| 371 |
+
from electron_speed import drift_velocity, TARGET_SWITCHES_PER_SEC
|
| 372 |
+
electron_transit_time = 1 / (drift_velocity * TARGET_SWITCHES_PER_SEC)
|
| 373 |
+
theoretical_time = electron_transit_time * ops_per_inference / total_cores
|
| 374 |
+
|
| 375 |
+
print(f"\nHTTP-Based Multi-Chip Inference Results:")
|
| 376 |
+
print(f"- Chips used: {num_chips}")
|
| 377 |
+
print(f"- Results collected: {len(results)}")
|
| 378 |
+
print(f"- Total time: {elapsed:.4f}s")
|
| 379 |
+
print(f"- Theoretical electron-speed time: {theoretical_time:.6f}s")
|
| 380 |
+
print(f"- Speed ratio: {theoretical_time/elapsed:.2f}x theoretical")
|
| 381 |
+
print(f"- Operations per second: {ops_per_inference/elapsed:.2e}")
|
| 382 |
+
|
| 383 |
+
# Test 3: HTTP Storage Performance
|
| 384 |
+
print(f"\nTest 3: HTTP Storage Performance Evaluation")
|
| 385 |
+
|
| 386 |
+
# Test tensor storage/retrieval performance
|
| 387 |
+
test_sizes = [1024, 4096, 16384, 65536] # Different tensor sizes
|
| 388 |
+
storage_times = []
|
| 389 |
+
retrieval_times = []
|
| 390 |
+
|
| 391 |
+
for size in test_sizes:
|
| 392 |
+
test_tensor = np.random.rand(size).astype(np.float32)
|
| 393 |
+
tensor_id = f"perf_test_{size}"
|
| 394 |
+
|
| 395 |
+
# Test storage time
|
| 396 |
+
start = time.time()
|
| 397 |
+
success = shared_storage.store_tensor(tensor_id, test_tensor)
|
| 398 |
+
storage_time = time.time() - start
|
| 399 |
+
|
| 400 |
+
if success:
|
| 401 |
+
storage_times.append(storage_time)
|
| 402 |
+
|
| 403 |
+
# Test retrieval time
|
| 404 |
+
start = time.time()
|
| 405 |
+
retrieved = shared_storage.load_tensor(tensor_id)
|
| 406 |
+
retrieval_time = time.time() - start
|
| 407 |
+
|
| 408 |
+
if retrieved is not None and np.array_equal(test_tensor, retrieved):
|
| 409 |
+
retrieval_times.append(retrieval_time)
|
| 410 |
+
print(f"Size {size}: Store {storage_time:.4f}s, Retrieve {retrieval_time:.4f}s")
|
| 411 |
+
else:
|
| 412 |
+
print(f"Size {size}: Retrieval verification failed")
|
| 413 |
+
else:
|
| 414 |
+
print(f"Size {size}: Storage failed")
|
| 415 |
+
|
| 416 |
+
if storage_times and retrieval_times:
|
| 417 |
+
avg_storage = sum(storage_times) / len(storage_times)
|
| 418 |
+
avg_retrieval = sum(retrieval_times) / len(retrieval_times)
|
| 419 |
+
print(f"Average storage time: {avg_storage:.4f}s")
|
| 420 |
+
print(f"Average retrieval time: {avg_retrieval:.4f}s")
|
| 421 |
+
|
| 422 |
+
# Test 4: Multi-chip coordination via HTTP
|
| 423 |
+
print(f"\nTest 4: Multi-Chip Coordination via HTTP")
|
| 424 |
+
|
| 425 |
+
# Test cross-chip data transfer
|
| 426 |
+
test_data_id = "cross_chip_test_data"
|
| 427 |
+
test_data = np.array([1, 2, 3, 4, 5], dtype=np.float32)
|
| 428 |
+
|
| 429 |
+
if shared_storage.store_tensor(test_data_id, test_data):
|
| 430 |
+
print("Stored test data for cross-chip transfer")
|
| 431 |
+
|
| 432 |
+
# Transfer data between chips
|
| 433 |
+
new_data_id = shared_storage.transfer_between_chips(0, 1, test_data_id)
|
| 434 |
+
if new_data_id:
|
| 435 |
+
print(f"Successfully transferred data from chip 0 to chip 1: {new_data_id}")
|
| 436 |
+
|
| 437 |
+
# Verify transferred data
|
| 438 |
+
transferred_data = shared_storage.load_tensor(new_data_id)
|
| 439 |
+
if transferred_data is not None and np.array_equal(test_data, transferred_data):
|
| 440 |
+
print("Cross-chip transfer verification successful")
|
| 441 |
+
else:
|
| 442 |
+
print("Cross-chip transfer verification failed")
|
| 443 |
+
else:
|
| 444 |
+
print("Cross-chip transfer failed")
|
| 445 |
+
|
| 446 |
+
# Test synchronization barriers
|
| 447 |
+
barrier_id = "test_barrier"
|
| 448 |
+
num_participants = num_chips
|
| 449 |
+
|
| 450 |
+
if shared_storage.create_sync_barrier(barrier_id, num_participants):
|
| 451 |
+
print(f"Created synchronization barrier for {num_participants} participants")
|
| 452 |
+
|
| 453 |
+
# Simulate participants arriving at barrier
|
| 454 |
+
for i in range(num_participants):
|
| 455 |
+
result = shared_storage.wait_sync_barrier(barrier_id)
|
| 456 |
+
if i == num_participants - 1:
|
| 457 |
+
if result:
|
| 458 |
+
print("All participants reached barrier - synchronization successful")
|
| 459 |
+
else:
|
| 460 |
+
print("Barrier synchronization failed")
|
| 461 |
+
else:
|
| 462 |
+
print(f"Participant {i+1} reached barrier")
|
| 463 |
+
|
| 464 |
+
print(f"\nHTTP-based AI integration test completed successfully!")
|
| 465 |
+
|
| 466 |
+
# Final statistics
|
| 467 |
+
final_stats = {
|
| 468 |
+
"chips_initialized": len(chips),
|
| 469 |
+
"ai_accelerators": len(ai_accelerators),
|
| 470 |
+
"total_cores": total_cores,
|
| 471 |
+
"model_loaded": components['model_id'] is not None,
|
| 472 |
+
"storage_type": "HTTP",
|
| 473 |
+
"connection_status": shared_storage.get_connection_status()
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
print(f"\nFinal System Statistics:")
|
| 477 |
+
for key, value in final_stats.items():
|
| 478 |
+
print(f"- {key}: {value}")
|
| 479 |
+
|
| 480 |
+
except Exception as e:
|
| 481 |
+
print(f"Multi-chip processing test failed: {e}")
|
| 482 |
+
import traceback
|
| 483 |
+
traceback.print_exc()
|
| 484 |
+
return
|
| 485 |
+
|
| 486 |
+
if __name__ == "__main__":
|
| 487 |
+
test_ai_integration_http()
|
| 488 |
+
|
test_multi_chip_gpu.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test for hyperrealistic multi-chip GPU system with full SM and tensor core realism,
|
| 3 |
+
using WebSocket-based storage for zero CPU usage.
|
| 4 |
+
"""
|
| 5 |
+
import time
|
| 6 |
+
import numpy as np
|
| 7 |
+
from gpu_arch import Chip, OpticalInterconnect
|
| 8 |
+
|
| 9 |
+
def test_multi_chip_gpu():
|
| 10 |
+
print("\n=== Multi-Chip GPU System with WebSocket Storage Test ===")
|
| 11 |
+
num_chips = 2 # Use 2 for realism, scale up as needed
|
| 12 |
+
num_sms = 4 # Use 4 for realism, scale up as needed
|
| 13 |
+
|
| 14 |
+
# Initialize WebSocket storage for all chips
|
| 15 |
+
from websocket_storage import WebSocketGPUStorage
|
| 16 |
+
storage = WebSocketGPUStorage()
|
| 17 |
+
if not storage.wait_for_connection():
|
| 18 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 19 |
+
|
| 20 |
+
chips = [Chip(
|
| 21 |
+
chip_id=i,
|
| 22 |
+
num_sms=num_sms,
|
| 23 |
+
vram_size_gb=None # Use unlimited WebSocket storage
|
| 24 |
+
) for i in range(num_chips)]
|
| 25 |
+
print(f"Created {num_chips} chips with unlimited WebSocket storage, each with {num_sms} SMs.")
|
| 26 |
+
|
| 27 |
+
# Connect chips in a ring topology with optical interconnect
|
| 28 |
+
optical_link = OpticalInterconnect(bandwidth_tbps=800, latency_ns=1)
|
| 29 |
+
for i in range(num_chips):
|
| 30 |
+
chips[i].connect_chip(chips[(i+1)%num_chips], optical_link)
|
| 31 |
+
|
| 32 |
+
# Initialize shared WebSocket storage for cross-chip communication
|
| 33 |
+
for chip in chips:
|
| 34 |
+
chip_state = {
|
| 35 |
+
"chip_id": chip.chip_id,
|
| 36 |
+
"num_sms": num_sms,
|
| 37 |
+
"connected_chips": [(c.chip_id, "optical") for c in chip.connected_chips]
|
| 38 |
+
}
|
| 39 |
+
storage.store_state(f"chips/{chip.chip_id}", "config", chip_state)
|
| 40 |
+
|
| 41 |
+
# Run tensor core operations with WebSocket-backed storage
|
| 42 |
+
print("\n=== Testing WebSocket-backed Multi-Chip Operations ===")
|
| 43 |
+
|
| 44 |
+
# Create test matrices
|
| 45 |
+
matrix_a = [[1.0, 2.0], [3.0, 4.0]]
|
| 46 |
+
matrix_b = [[5.0, 6.0], [7.0, 8.0]]
|
| 47 |
+
|
| 48 |
+
for chip in chips:
|
| 49 |
+
print(f"\n--- Chip {chip.chip_id} ---")
|
| 50 |
+
|
| 51 |
+
# Store matrices in WebSocket storage for this chip
|
| 52 |
+
storage.store_tensor(f"chip_{chip.chip_id}/matrix_a", np.array(matrix_a))
|
| 53 |
+
storage.store_tensor(f"chip_{chip.chip_id}/matrix_b", np.array(matrix_b))
|
| 54 |
+
|
| 55 |
+
# Process using each SM
|
| 56 |
+
for sm_id in range(num_sms):
|
| 57 |
+
sm = chip.get_sm(sm_id)
|
| 58 |
+
|
| 59 |
+
# Load matrices from WebSocket storage
|
| 60 |
+
matrix_a_data = storage.load_tensor(f"chip_{chip.chip_id}/matrix_a")
|
| 61 |
+
matrix_b_data = storage.load_tensor(f"chip_{chip.chip_id}/matrix_b")
|
| 62 |
+
|
| 63 |
+
# Perform tensor core operation
|
| 64 |
+
result = sm.tensor_core_matmul(matrix_a_data.tolist(), matrix_b_data.tolist())
|
| 65 |
+
|
| 66 |
+
# Store result back in WebSocket storage
|
| 67 |
+
storage.store_tensor(f"chip_{chip.chip_id}/sm_{sm_id}/result", np.array(result))
|
| 68 |
+
print(f"SM {sm_id} tensor core matmul result: {result}")
|
| 69 |
+
|
| 70 |
+
# Test cross-chip communication
|
| 71 |
+
if len(chip.connected_chips) > 0:
|
| 72 |
+
next_chip, link = chip.connected_chips[0]
|
| 73 |
+
|
| 74 |
+
# Get result from this chip
|
| 75 |
+
result_data = storage.load_tensor(f"chip_{chip.chip_id}/sm_0/result")
|
| 76 |
+
|
| 77 |
+
# Transfer to next chip through optical link
|
| 78 |
+
transfer_id = f"transfer_chip_{chip.chip_id}_to_{next_chip.chip_id}"
|
| 79 |
+
storage.store_tensor(transfer_id, result_data)
|
| 80 |
+
print(f"Transferred result from Chip {chip.chip_id} to Chip {next_chip.chip_id} via {link.__class__.__name__}")
|
| 81 |
+
for i in range(len(sm.register_file)):
|
| 82 |
+
for j in range(len(sm.register_file[0])):
|
| 83 |
+
sm.register_file[i][j] = float(i + j)
|
| 84 |
+
for addr in range(sm.shared_mem.size):
|
| 85 |
+
sm.shared_mem.write(addr, float(addr % 10))
|
| 86 |
+
for addr in range(sm.global_mem.size_bytes if sm.global_mem else 0):
|
| 87 |
+
sm.global_mem.write(addr, float(addr % 100))
|
| 88 |
+
# Test tensor core matmul from registers
|
| 89 |
+
reg_result = sm.tensor_core_matmul_from_memory('register', 0, 'register', 0, (2,2), (2,2))
|
| 90 |
+
print(f"SM {sm.sm_id} tensor core matmul from registers: {reg_result}")
|
| 91 |
+
# Test tensor core matmul from shared memory
|
| 92 |
+
shared_result = sm.tensor_core_matmul_from_memory('shared', 0, 'shared', 0, (2,2), (2,2))
|
| 93 |
+
print(f"SM {sm.sm_id} tensor core matmul from shared memory: {shared_result}")
|
| 94 |
+
# Test tensor core matmul from global memory
|
| 95 |
+
global_result = sm.tensor_core_matmul_from_memory('global', 0, 'global', 0, (2,2), (2,2))
|
| 96 |
+
print(f"SM {sm.sm_id} tensor core matmul from global memory: {global_result}")
|
| 97 |
+
print("\n=== Multi-Chip GPU System Test Complete ===")
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
start = time.time()
|
| 101 |
+
test_multi_chip_gpu()
|
| 102 |
+
print(f"Test runtime: {time.time()-start:.3f} seconds")
|
virtual_vram.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from websocket_storage import WebSocketGPUStorage
|
| 2 |
+
import numpy as np
|
| 3 |
+
from typing import Dict, Any, Optional
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
class VirtualVRAM:
|
| 7 |
+
def __init__(self, size_gb: int = None, storage=None):
|
| 8 |
+
"""Initialize virtual VRAM with unlimited storage capability"""
|
| 9 |
+
self.storage = storage
|
| 10 |
+
if self.storage is None:
|
| 11 |
+
from websocket_storage import WebSocketGPUStorage
|
| 12 |
+
self.storage = WebSocketGPUStorage()
|
| 13 |
+
if not self.storage.wait_for_connection():
|
| 14 |
+
raise RuntimeError("Could not connect to GPU storage server")
|
| 15 |
+
|
| 16 |
+
# Initialize VRAM state with unlimited capacity
|
| 17 |
+
self.vram_state = {
|
| 18 |
+
"total_size": float('inf'), # Unlimited size
|
| 19 |
+
"allocated": 0,
|
| 20 |
+
"blocks": {},
|
| 21 |
+
"memory_map": {},
|
| 22 |
+
"is_unlimited": True
|
| 23 |
+
}
|
| 24 |
+
self.store_vram_state()
|
| 25 |
+
|
| 26 |
+
def store_vram_state(self, max_retries=3):
|
| 27 |
+
"""Store VRAM state in WebSocket storage with retry logic"""
|
| 28 |
+
for attempt in range(max_retries):
|
| 29 |
+
try:
|
| 30 |
+
# Wait for connection if needed
|
| 31 |
+
if not self.storage.wait_for_connection(timeout=5):
|
| 32 |
+
print(f"Waiting for WebSocket connection (attempt {attempt + 1}/{max_retries})")
|
| 33 |
+
time.sleep(1)
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
# Ensure state is JSON serializable
|
| 37 |
+
safe_state = {
|
| 38 |
+
"total_size": str(self.vram_state["total_size"]) if isinstance(self.vram_state["total_size"], float) and self.vram_state["total_size"] == float('inf') else self.vram_state["total_size"],
|
| 39 |
+
"allocated": self.vram_state["allocated"],
|
| 40 |
+
"blocks": self.vram_state["blocks"],
|
| 41 |
+
"memory_map": self.vram_state["memory_map"],
|
| 42 |
+
"is_unlimited": self.vram_state["is_unlimited"]
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
success = self.storage.store_state("vram", "state", safe_state)
|
| 46 |
+
if success:
|
| 47 |
+
return True
|
| 48 |
+
|
| 49 |
+
print(f"Failed to store VRAM state (attempt {attempt + 1}/{max_retries})")
|
| 50 |
+
time.sleep(1)
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error storing VRAM state (attempt {attempt + 1}/{max_retries}): {str(e)}")
|
| 54 |
+
time.sleep(1)
|
| 55 |
+
|
| 56 |
+
raise RuntimeError("Failed to store VRAM state after multiple attempts")
|
| 57 |
+
|
| 58 |
+
def allocate_block(self, size: int, block_id: Optional[str] = None) -> str:
|
| 59 |
+
"""Allocate a block of VRAM"""
|
| 60 |
+
if self.vram_state["allocated"] + size > self.vram_state["total_size"]:
|
| 61 |
+
raise MemoryError("Not enough VRAM available")
|
| 62 |
+
|
| 63 |
+
if block_id is None:
|
| 64 |
+
block_id = f"block_{time.time_ns()}"
|
| 65 |
+
|
| 66 |
+
self.vram_state["blocks"][block_id] = {
|
| 67 |
+
"size": size,
|
| 68 |
+
"allocated_at": time.time_ns(),
|
| 69 |
+
"last_accessed": time.time_ns()
|
| 70 |
+
}
|
| 71 |
+
self.vram_state["allocated"] += size
|
| 72 |
+
|
| 73 |
+
# Store updated state
|
| 74 |
+
self.store_vram_state()
|
| 75 |
+
return block_id
|
| 76 |
+
|
| 77 |
+
def free_block(self, block_id: str):
|
| 78 |
+
"""Free a block of VRAM"""
|
| 79 |
+
if block_id in self.vram_state["blocks"]:
|
| 80 |
+
self.vram_state["allocated"] -= self.vram_state["blocks"][block_id]["size"]
|
| 81 |
+
del self.vram_state["blocks"][block_id]
|
| 82 |
+
self.store_vram_state()
|
| 83 |
+
|
| 84 |
+
# Remove block data from storage
|
| 85 |
+
self.storage.store_tensor(block_id, None)
|
| 86 |
+
|
| 87 |
+
def write_block(self, block_id: str, data: np.ndarray):
|
| 88 |
+
"""Write data to a VRAM block"""
|
| 89 |
+
if block_id not in self.vram_state["blocks"]:
|
| 90 |
+
raise ValueError(f"Block {block_id} not allocated")
|
| 91 |
+
|
| 92 |
+
self.vram_state["blocks"][block_id]["last_accessed"] = time.time_ns()
|
| 93 |
+
self.store_vram_state()
|
| 94 |
+
|
| 95 |
+
return self.storage.store_tensor(block_id, data)
|
| 96 |
+
|
| 97 |
+
def read_block(self, block_id: str) -> Optional[np.ndarray]:
|
| 98 |
+
"""Read data from a VRAM block"""
|
| 99 |
+
if block_id not in self.vram_state["blocks"]:
|
| 100 |
+
raise ValueError(f"Block {block_id} not allocated")
|
| 101 |
+
|
| 102 |
+
self.vram_state["blocks"][block_id]["last_accessed"] = time.time_ns()
|
| 103 |
+
self.store_vram_state()
|
| 104 |
+
|
| 105 |
+
return self.storage.load_tensor(block_id)
|
| 106 |
+
|
| 107 |
+
def map_address(self, virtual_addr: str, block_id: str):
|
| 108 |
+
"""Map virtual address to VRAM block"""
|
| 109 |
+
self.vram_state["memory_map"][virtual_addr] = block_id
|
| 110 |
+
self.store_vram_state()
|
| 111 |
+
|
| 112 |
+
def get_block_from_address(self, virtual_addr: str) -> Optional[str]:
|
| 113 |
+
"""Get block ID from virtual address"""
|
| 114 |
+
return self.vram_state["memory_map"].get(virtual_addr)
|
| 115 |
+
|
| 116 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 117 |
+
"""Get VRAM statistics"""
|
| 118 |
+
return {
|
| 119 |
+
"total_gb": self.size_gb,
|
| 120 |
+
"used_gb": self.vram_state["allocated"] / (1024 * 1024 * 1024),
|
| 121 |
+
"free_gb": (self.vram_state["total_size"] - self.vram_state["allocated"]) / (1024 * 1024 * 1024),
|
| 122 |
+
"num_blocks": len(self.vram_state["blocks"]),
|
| 123 |
+
"mappings": len(self.vram_state["memory_map"])
|
| 124 |
+
}
|
vram/__pycache__/ram_controller.cpython-311.pyc
ADDED
|
Binary file (3.92 kB). View file
|
|
|
vram/__pycache__/ram_controller.cpython-312.pyc
ADDED
|
Binary file (3.25 kB). View file
|
|
|
vram/__pycache__/ram_controller.cpython-313.pyc
ADDED
|
Binary file (3.4 kB). View file
|
|
|
vram/dram_cache.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class DRAMCache:
|
| 2 |
+
def __init__(self, size_mb=None):
|
| 3 |
+
"""Initialize DRAM cache with unlimited capacity"""
|
| 4 |
+
self.cache = {}
|
| 5 |
+
self.access_order = []
|
| 6 |
+
self.is_unlimited = True
|
| 7 |
+
|
| 8 |
+
def read(self, key):
|
| 9 |
+
if key in self.cache:
|
| 10 |
+
self.access_order.remove(key)
|
| 11 |
+
self.access_order.append(key)
|
| 12 |
+
return self.cache[key]
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
def write(self, key, value):
|
| 16 |
+
"""Write to cache with unlimited capacity - no eviction needed"""
|
| 17 |
+
if key in self.cache:
|
| 18 |
+
self.access_order.remove(key)
|
| 19 |
+
self.cache[key] = value
|
| 20 |
+
self.access_order.append(key)
|
| 21 |
+
|
| 22 |
+
class Buffer:
|
| 23 |
+
def __init__(self, size_mb=None):
|
| 24 |
+
"""Initialize buffer with unlimited capacity"""
|
| 25 |
+
self.buffer = []
|
| 26 |
+
self.is_unlimited = True
|
| 27 |
+
|
| 28 |
+
def add(self, data):
|
| 29 |
+
"""Add data to buffer - no size restrictions"""
|
| 30 |
+
self.buffer.append(data)
|
| 31 |
+
|
| 32 |
+
def flush(self):
|
| 33 |
+
"""Flush buffer and return all data"""
|
| 34 |
+
flushed = self.buffer[:]
|
| 35 |
+
self.buffer = []
|
| 36 |
+
return flushed
|
vram/electron_speed.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Calculate electron drift speed and relate it to transistor switching (tick) rate for a modern GPU.
|
| 3 |
+
Assume: We want to simulate 900 quintillion (9e20) transistor switches per second (B200 scale).
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Physical constants
|
| 7 |
+
ELEM_CHARGE = 1.602e-19 # Coulombs
|
| 8 |
+
ELECTRON_MASS = 9.109e-31 # kg
|
| 9 |
+
VACUUM_PERMITTIVITY = 8.854e-12 # F/m
|
| 10 |
+
SILICON_MOBILITY = 0.14 # m^2/(V·s) (typical for electrons in Si at room temp)
|
| 11 |
+
|
| 12 |
+
# Example parameters (can be tuned for realism)
|
| 13 |
+
VOLTAGE = 0.7 # V (typical for advanced nodes)
|
| 14 |
+
CHANNEL_LENGTH = 5e-9 # 5 nm process
|
| 15 |
+
ELECTRIC_FIELD = VOLTAGE / CHANNEL_LENGTH # V/m
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
SPEED_OF_LIGHT_VACUUM = 3e8 # m/s
|
| 19 |
+
SILICON_REFRACTIVE_INDEX = 3.5
|
| 20 |
+
speed_of_light_silicon = SPEED_OF_LIGHT_VACUUM / SILICON_REFRACTIVE_INDEX
|
| 21 |
+
|
| 22 |
+
# Calculate drift velocity (v = μE)
|
| 23 |
+
drift_velocity = speed_of_light_silicon # m/s
|
| 24 |
+
|
| 25 |
+
# Calculate time for electron to cross channel (t = L / v)
|
| 26 |
+
transit_time = CHANNEL_LENGTH / drift_velocity # seconds
|
| 27 |
+
|
| 28 |
+
# Calculate max theoretical switching frequency (f = 1 / t)
|
| 29 |
+
max_switch_freq = 1 / transit_time # Hz
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# For 900 quintillion switches/sec, but with 600 billion transistors
|
| 33 |
+
TARGET_SWITCHES_PER_SEC = 9e20
|
| 34 |
+
TRANSISTORS_ON_CHIP = 6e11 # 600 billion
|
| 35 |
+
transistors_needed = TARGET_SWITCHES_PER_SEC / max_switch_freq
|
| 36 |
+
required_switch_freq_per_transistor = TARGET_SWITCHES_PER_SEC / TRANSISTORS_ON_CHIP
|
| 37 |
+
|
| 38 |
+
# Speed of light in silicon (approx 2/3 c)
|
| 39 |
+
|
| 40 |
+
# --- NAND Flash Floating Gate Transistor Model ---
|
| 41 |
+
class FloatingGateTransistor:
|
| 42 |
+
def __init__(self, channel_length, drift_velocity):
|
| 43 |
+
self.channel_length = channel_length
|
| 44 |
+
self.drift_velocity = drift_velocity
|
| 45 |
+
self.trapped_electrons = 0 # Number of electrons trapped
|
| 46 |
+
self.state = 0 # 0 or 1, representing data
|
| 47 |
+
|
| 48 |
+
def program(self, electrons):
|
| 49 |
+
self.trapped_electrons += electrons
|
| 50 |
+
self.state = 1 if self.trapped_electrons > 0 else 0
|
| 51 |
+
prog_time = self.channel_length / self.drift_velocity
|
| 52 |
+
return prog_time
|
| 53 |
+
|
| 54 |
+
def erase(self):
|
| 55 |
+
self.trapped_electrons = 0
|
| 56 |
+
self.state = 0
|
| 57 |
+
erase_time = self.channel_length / self.drift_velocity
|
| 58 |
+
return erase_time
|
| 59 |
+
|
| 60 |
+
def read(self):
|
| 61 |
+
return self.state
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
print(f"Electron drift velocity: {drift_velocity:.2e} m/s")
|
| 67 |
+
print(f"Channel transit time: {transit_time:.2e} s")
|
| 68 |
+
print(f"Max transistor switching frequency: {max_switch_freq:.2e} Hz")
|
| 69 |
+
print(f"To achieve {TARGET_SWITCHES_PER_SEC:.1e} switches/sec:")
|
| 70 |
+
print(f"- You'd need {transistors_needed:.2e} transistors switching at max speed in parallel.")
|
| 71 |
+
print(f"- For a chip with 600B transistors, each must switch at {required_switch_freq_per_transistor:.2e} Hz.")
|
| 72 |
+
print(f"- Electron drift speed: {drift_velocity:.2e} m/s vs. speed of light in silicon: {speed_of_light_silicon:.2e} m/s")
|
| 73 |
+
print(f"- Electron drift is ~{(drift_velocity/speed_of_light_silicon)*100:.2f}% the speed of light in silicon (photon speed).")
|
| 74 |
+
|
| 75 |
+
# NAND Flash Floating Gate Transistor Demo
|
| 76 |
+
print("\n--- NAND Flash Floating Gate Transistor Demo ---")
|
| 77 |
+
fgt = FloatingGateTransistor(CHANNEL_LENGTH, drift_velocity)
|
| 78 |
+
electrons_to_trap = 1000
|
| 79 |
+
|
| 80 |
+
# Real-time trapping analysis (simulated)
|
| 81 |
+
print("\nSimulating electron trapping in real time:")
|
| 82 |
+
electrons_per_step = 100
|
| 83 |
+
total_steps = electrons_to_trap // electrons_per_step
|
| 84 |
+
for step in range(1, total_steps + 1):
|
| 85 |
+
prog_time = fgt.program(electrons_per_step)
|
| 86 |
+
print(f"Step {step}: Trapped electrons = {fgt.trapped_electrons}, State = {fgt.read()}, Time for this step = {prog_time:.2e} s")
|
| 87 |
+
# Final state after all electrons trapped
|
| 88 |
+
print(f"Final: Trapped electrons = {fgt.trapped_electrons}, State = {fgt.read()}")
|
| 89 |
+
erase_time = fgt.erase()
|
| 90 |
+
print(f"Erasing: State = {fgt.read()}, Time = {erase_time:.2e} s")
|
| 91 |
+
print(f"(Operation speed is limited by electron drift velocity: {drift_velocity:.2e} m/s)")
|
| 92 |
+
print("Higher drift velocity = faster programming/erasing; lower drift velocity = slower data ops.")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# --- SR, D, JK, T Flip-Flop Physics/Timing Summary ---
|
| 96 |
+
print("\n--- Flip-Flop Types and Switching Physics ---")
|
| 97 |
+
print("SR Flip-Flop: Set-Reset, basic memory, built from NAND/NOR gates.")
|
| 98 |
+
print("D Flip-Flop: Data/Delay, synchronizes input to clock, used in registers.")
|
| 99 |
+
print("JK Flip-Flop: Universal, toggles or sets/resets based on inputs.")
|
| 100 |
+
print("T Flip-Flop: Toggle, divides clock, used in counters.")
|
| 101 |
+
print("All flip-flops are built from logic gates, so their switching speed is limited by the gate delay (set by electron drift and channel length).\n")
|
| 102 |
+
|
| 103 |
+
# Example: Calculate flip-flop switching time (assuming 4 gate delays per flip-flop)
|
| 104 |
+
GATE_DELAY = transit_time # seconds, from above
|
| 105 |
+
FF_GATE_COUNT = 4 # typical for basic flip-flop
|
| 106 |
+
flip_flop_delay = FF_GATE_COUNT * GATE_DELAY
|
| 107 |
+
flip_flop_max_freq = 1 / flip_flop_delay
|
| 108 |
+
|
| 109 |
+
print(f"Estimated flip-flop delay: {flip_flop_delay:.2e} s (for {FF_GATE_COUNT} gates)")
|
| 110 |
+
print(f"Max flip-flop switching frequency: {flip_flop_max_freq:.2e} Hz")
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
vram/ftl.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class FTL:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.lba_to_phys = {}
|
| 4 |
+
self.phys_to_lba = {}
|
| 5 |
+
|
| 6 |
+
def map(self, lba, phys):
|
| 7 |
+
self.lba_to_phys[lba] = phys
|
| 8 |
+
self.phys_to_lba[phys] = lba
|
| 9 |
+
|
| 10 |
+
def get_phys(self, lba):
|
| 11 |
+
return self.lba_to_phys.get(lba, None)
|
| 12 |
+
|
| 13 |
+
def get_lba(self, phys):
|
| 14 |
+
return self.phys_to_lba.get(phys, None)
|
| 15 |
+
|
| 16 |
+
def invalidate(self, lba):
|
| 17 |
+
phys = self.lba_to_phys.pop(lba, None)
|
| 18 |
+
if phys:
|
| 19 |
+
self.phys_to_lba.pop(phys, None)
|
vram/interface.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class PCIeInterface:
|
| 2 |
+
def __init__(self, version='4.0', lanes=4, max_gbps=15):
|
| 3 |
+
self.version = version
|
| 4 |
+
self.lanes = lanes
|
| 5 |
+
self.max_gbps = max_gbps # GB/s
|
| 6 |
+
self.latency_us = 2 # microseconds, typical for PCIe 4.0
|
| 7 |
+
|
| 8 |
+
def transfer_time(self, size_bytes):
|
| 9 |
+
# Calculate time to transfer size_bytes at max_gbps (in seconds)
|
| 10 |
+
gb = size_bytes / 1e9
|
| 11 |
+
time_s = gb / self.max_gbps
|
| 12 |
+
return time_s
|
| 13 |
+
|
| 14 |
+
def simulate_transfer(self, size_bytes, direction='write'):
|
| 15 |
+
t = self.transfer_time(size_bytes)
|
| 16 |
+
print(f"[PCIe] {direction.title()} {size_bytes/1e6:.2f} MB over PCIe {self.version} x{self.lanes} at {self.max_gbps} GB/s: {t*1e3:.3f} ms + {self.latency_us} us latency")
|
| 17 |
+
return t + self.latency_us / 1e6
|
vram/main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ram_controller import RAMController
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
RAM_SIZE_BYTES = 1024 * 1024 * 16 # 16 MB of RAM
|
| 5 |
+
|
| 6 |
+
def demo():
|
| 7 |
+
print(f"Virtual RAM Demo: {RAM_SIZE_BYTES / (1024 * 1024):.2f} MB")
|
| 8 |
+
ram = RAMController(RAM_SIZE_BYTES)
|
| 9 |
+
|
| 10 |
+
print("\nWriting sequential data to RAM:")
|
| 11 |
+
for i in range(0, 1024, 16):
|
| 12 |
+
data = [random.randint(0, 255) for _ in range(16)]
|
| 13 |
+
ram.write(i, data)
|
| 14 |
+
if i < 64:
|
| 15 |
+
print(f"Address {i}: Data (first 16 bytes) {data}")
|
| 16 |
+
|
| 17 |
+
print("\nReading sequential data from RAM:")
|
| 18 |
+
for i in range(0, 1024, 16):
|
| 19 |
+
read_data = ram.read(i, 16)
|
| 20 |
+
if i < 64:
|
| 21 |
+
print(f"Address {i}: Read Data (first 16 bytes) {list(read_data)}")
|
| 22 |
+
|
| 23 |
+
print("\nWriting random data to RAM:")
|
| 24 |
+
for _ in range(10):
|
| 25 |
+
address = random.randint(0, RAM_SIZE_BYTES - 16)
|
| 26 |
+
data = [random.randint(0, 255) for _ in range(16)]
|
| 27 |
+
ram.write(address, data)
|
| 28 |
+
print(f"Address {address}: Data (first 16 bytes) {data}")
|
| 29 |
+
|
| 30 |
+
print("\nReading random data from RAM:")
|
| 31 |
+
for _ in range(10):
|
| 32 |
+
address = random.randint(0, RAM_SIZE_BYTES - 16)
|
| 33 |
+
read_data = ram.read(address, 16)
|
| 34 |
+
print(f"Address {address}: Read Data (first 16 bytes) {list(read_data)}")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
demo()
|
| 38 |
+
|
| 39 |
+
|
vram/nand_block.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nand_page import Page
|
| 2 |
+
|
| 3 |
+
class Block:
|
| 4 |
+
def __init__(self, num_pages, num_cells_per_page, channel_length, drift_velocity, levels):
|
| 5 |
+
self.pages = [Page(num_cells_per_page, channel_length, drift_velocity, levels) for _ in range(num_pages)]
|
| 6 |
+
self.wear_count = 0
|
| 7 |
+
|
| 8 |
+
def erase(self):
|
| 9 |
+
for page in self.pages:
|
| 10 |
+
page.erase()
|
| 11 |
+
self.wear_count += 1
|
vram/nand_cell.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class MultiLevelCell:
|
| 2 |
+
def __init__(self, channel_length, drift_velocity, levels):
|
| 3 |
+
self.channel_length = channel_length
|
| 4 |
+
self.drift_velocity = drift_velocity
|
| 5 |
+
self.levels = levels
|
| 6 |
+
self.trapped_electrons = 0
|
| 7 |
+
self.value = 0
|
| 8 |
+
self.wear_count = 0
|
| 9 |
+
self.retention_loss = 0.0
|
| 10 |
+
|
| 11 |
+
def program(self, value):
|
| 12 |
+
self.value = max(0, min(self.levels-1, value))
|
| 13 |
+
self.trapped_electrons = self.value
|
| 14 |
+
self.wear_count += 1
|
| 15 |
+
self.retention_loss = 0.0
|
| 16 |
+
prog_time = self.channel_length / self.drift_velocity
|
| 17 |
+
return prog_time
|
| 18 |
+
|
| 19 |
+
def erase(self):
|
| 20 |
+
self.trapped_electrons = 0
|
| 21 |
+
self.value = 0
|
| 22 |
+
self.wear_count += 1
|
| 23 |
+
self.retention_loss = 0.0
|
| 24 |
+
erase_time = self.channel_length / self.drift_velocity
|
| 25 |
+
return erase_time
|
| 26 |
+
|
| 27 |
+
def read(self):
|
| 28 |
+
import random
|
| 29 |
+
if self.value > 0:
|
| 30 |
+
self.retention_loss += random.uniform(0, 0.01)
|
| 31 |
+
if self.retention_loss > 0.5:
|
| 32 |
+
self.value = max(0, self.value - 1)
|
| 33 |
+
self.trapped_electrons = self.value
|
| 34 |
+
self.retention_loss = 0.0
|
| 35 |
+
return self.value
|
vram/nand_memory.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
"""
|
| 3 |
+
NAND Flash SSD Simulation (Modular)
|
| 4 |
+
-----------------------------------
|
| 5 |
+
This file documents the SSD architecture and usage for the modular simulation.
|
| 6 |
+
|
| 7 |
+
Components:
|
| 8 |
+
- nand_cell.py: MultiLevelCell (single cell physics/logic)
|
| 9 |
+
- nand_page.py: Page (group of cells, ECC)
|
| 10 |
+
- nand_block.py: Block (group of pages)
|
| 11 |
+
- nand_plane.py: Plane (group of blocks)
|
| 12 |
+
- dram_cache.py: DRAMCache, Buffer (cache, buffer, metadata)
|
| 13 |
+
- ftl.py: FTL (Flash Translation Layer, mapping table)
|
| 14 |
+
- ssd_controller.py: SSDController (manages all above, FTL, cache, buffer)
|
| 15 |
+
- main.py: Demo/entry point
|
| 16 |
+
|
| 17 |
+
Usage:
|
| 18 |
+
------
|
| 19 |
+
Import and use the SSDController and other components in your own scripts, or run main.py for a demo.
|
| 20 |
+
|
| 21 |
+
Example:
|
| 22 |
+
from ssd_controller import SSDController
|
| 23 |
+
ssd = SSDController(...)
|
| 24 |
+
ssd.program(lba, data)
|
| 25 |
+
ssd.read(lba)
|
| 26 |
+
|
| 27 |
+
See main.py for a full demonstration of SSD features, including DRAM cache, buffer, FTL, wear leveling, garbage collection, and retention simulation.
|
| 28 |
+
"""
|
vram/nand_page.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nand_cell import MultiLevelCell
|
| 2 |
+
|
| 3 |
+
class Page:
|
| 4 |
+
def __init__(self, num_cells, channel_length, drift_velocity, levels):
|
| 5 |
+
self.cells = [MultiLevelCell(channel_length, drift_velocity, levels) for _ in range(num_cells)]
|
| 6 |
+
self.ecc = 0 # Placeholder for ECC bits
|
| 7 |
+
|
| 8 |
+
def program(self, data):
|
| 9 |
+
for i, value in enumerate(data):
|
| 10 |
+
self.cells[i].program(value)
|
| 11 |
+
self.ecc = self.calculate_ecc(data)
|
| 12 |
+
|
| 13 |
+
def erase(self):
|
| 14 |
+
for cell in self.cells:
|
| 15 |
+
cell.erase()
|
| 16 |
+
self.ecc = 0
|
| 17 |
+
|
| 18 |
+
def read(self):
|
| 19 |
+
data = [cell.read() for cell in self.cells]
|
| 20 |
+
return data, self.ecc
|
| 21 |
+
|
| 22 |
+
def calculate_ecc(self, data):
|
| 23 |
+
return sum(data) % 2
|
vram/nand_plane.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nand_block import Block
|
| 2 |
+
|
| 3 |
+
class Plane:
|
| 4 |
+
def __init__(self, num_blocks, num_pages, num_cells_per_page, channel_length, drift_velocity, levels):
|
| 5 |
+
self.blocks = [Block(num_pages, num_cells_per_page, channel_length, drift_velocity, levels) for _ in range(num_blocks)]
|
vram/nvme.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from interface import PCIeInterface
|
| 2 |
+
import threading
|
| 3 |
+
import queue
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
class NVMeCommand:
|
| 7 |
+
def __init__(self, cmd_type, lba, data=None):
|
| 8 |
+
self.cmd_type = cmd_type # 'read' or 'write'
|
| 9 |
+
self.lba = lba
|
| 10 |
+
self.data = data
|
| 11 |
+
self.result = None
|
| 12 |
+
self.completed = threading.Event()
|
| 13 |
+
|
| 14 |
+
class NVMeController:
|
| 15 |
+
def __init__(self, ssd_controller, queue_depth=64):
|
| 16 |
+
self.ssd = ssd_controller
|
| 17 |
+
self.submission_queue = queue.Queue(maxsize=queue_depth)
|
| 18 |
+
self.completion_queue = queue.Queue(maxsize=queue_depth)
|
| 19 |
+
self.running = True
|
| 20 |
+
self.worker = threading.Thread(target=self.process_commands)
|
| 21 |
+
self.worker.daemon = True
|
| 22 |
+
self.worker.start()
|
| 23 |
+
self.interface = PCIeInterface()
|
| 24 |
+
|
| 25 |
+
def submit(self, cmd):
|
| 26 |
+
self.submission_queue.put(cmd)
|
| 27 |
+
|
| 28 |
+
def process_commands(self):
|
| 29 |
+
while self.running:
|
| 30 |
+
try:
|
| 31 |
+
cmd = self.submission_queue.get(timeout=0.1)
|
| 32 |
+
if cmd.cmd_type == 'write':
|
| 33 |
+
self.ssd.program(cmd.lba, cmd.data)
|
| 34 |
+
self.interface.simulate_transfer(len(cmd.data) * 32 // 8, direction='write')
|
| 35 |
+
cmd.result = 'write_complete'
|
| 36 |
+
elif cmd.cmd_type == 'read':
|
| 37 |
+
data = self.ssd.read(cmd.lba)
|
| 38 |
+
self.interface.simulate_transfer(len(data) * 32 // 8, direction='read')
|
| 39 |
+
cmd.result = data
|
| 40 |
+
self.completion_queue.put(cmd)
|
| 41 |
+
cmd.completed.set()
|
| 42 |
+
except queue.Empty:
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
def get_completion(self, timeout=1.0):
|
| 46 |
+
try:
|
| 47 |
+
cmd = self.completion_queue.get(timeout=timeout)
|
| 48 |
+
return cmd
|
| 49 |
+
except queue.Empty:
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
def shutdown(self):
|
| 53 |
+
self.running = False
|
| 54 |
+
self.worker.join()
|
vram/ram_controller.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import sqlite3
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
class RAMController:
|
| 6 |
+
def __init__(self, size_bytes, db_path='ram_storage.db'):
|
| 7 |
+
self.size_bytes = size_bytes
|
| 8 |
+
self.conn = sqlite3.connect(db_path, check_same_thread=False)
|
| 9 |
+
self.db_lock = threading.Lock()
|
| 10 |
+
with self.db_lock:
|
| 11 |
+
self.conn.execute('''CREATE TABLE IF NOT EXISTS ram_cells (
|
| 12 |
+
address INTEGER PRIMARY KEY,
|
| 13 |
+
data BLOB
|
| 14 |
+
)''')
|
| 15 |
+
self.conn.commit()
|
| 16 |
+
|
| 17 |
+
def read(self, address, length):
|
| 18 |
+
if address < 0 or address + length > self.size_bytes:
|
| 19 |
+
raise IndexError("Memory access out of bounds")
|
| 20 |
+
with self.db_lock:
|
| 21 |
+
cur = self.conn.execute(
|
| 22 |
+
"SELECT address, data FROM ram_cells WHERE address >= ? AND address < ? ORDER BY address ASC",
|
| 23 |
+
(address, address + length)
|
| 24 |
+
)
|
| 25 |
+
# Build a bytearray of the requested range
|
| 26 |
+
result = bytearray([0] * length)
|
| 27 |
+
for row in cur:
|
| 28 |
+
addr = row[0]
|
| 29 |
+
data = row[1]
|
| 30 |
+
if address <= addr < address + length:
|
| 31 |
+
result[addr - address] = data[0] if isinstance(data, (bytes, bytearray)) else data
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
def write(self, address, data):
|
| 35 |
+
if address < 0 or address + len(data) > self.size_bytes:
|
| 36 |
+
raise IndexError("Memory access out of bounds")
|
| 37 |
+
with self.db_lock:
|
| 38 |
+
for offset, value in enumerate(data):
|
| 39 |
+
self.conn.execute(
|
| 40 |
+
"INSERT OR REPLACE INTO ram_cells (address, data) VALUES (?, ?)",
|
| 41 |
+
(address + offset, sqlite3.Binary(bytes([value])))
|
| 42 |
+
)
|
| 43 |
+
self.conn.commit()
|
| 44 |
+
|
| 45 |
+
def close(self):
|
| 46 |
+
with self.db_lock:
|
| 47 |
+
if self.conn:
|
| 48 |
+
self.conn.close()
|
| 49 |
+
self.conn = None
|
| 50 |
+
|
| 51 |
+
|
vram_server.py
ADDED
|
File without changes
|
websocket_storage.py
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import websockets
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
+
from typing import Dict, Any, Optional, Union
|
| 5 |
+
import threading
|
| 6 |
+
from queue import Queue
|
| 7 |
+
import time
|
| 8 |
+
import asyncio
|
| 9 |
+
import hashlib
|
| 10 |
+
|
| 11 |
+
class WebSocketGPUStorage:
|
| 12 |
+
# Singleton instance
|
| 13 |
+
_instance = None
|
| 14 |
+
_lock = threading.Lock()
|
| 15 |
+
|
| 16 |
+
def __new__(cls, url: str = "wss://factorst-wbs1.hf.space/ws"):
|
| 17 |
+
with cls._lock:
|
| 18 |
+
if cls._instance is None:
|
| 19 |
+
cls._instance = super().__new__(cls)
|
| 20 |
+
cls._instance._init_singleton(url)
|
| 21 |
+
return cls._instance
|
| 22 |
+
|
| 23 |
+
def _init_singleton(self, url: str):
|
| 24 |
+
"""Initialize the singleton instance"""
|
| 25 |
+
if hasattr(self, 'initialized'):
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
self.url = url
|
| 29 |
+
self.websocket = None
|
| 30 |
+
self.connected = False
|
| 31 |
+
self.message_queue = Queue()
|
| 32 |
+
self.response_queues: Dict[str, Queue] = {}
|
| 33 |
+
self.lock = threading.Lock()
|
| 34 |
+
self._closing = False
|
| 35 |
+
self._loop = None
|
| 36 |
+
self.error_count = 0
|
| 37 |
+
self.last_error_time = 0
|
| 38 |
+
self.max_retries = 5
|
| 39 |
+
self.tensor_registry: Dict[str, Dict[str, Any]] = {} # Track tensor metadata
|
| 40 |
+
self.model_registry: Dict[str, Dict[str, Any]] = {} # Track loaded models
|
| 41 |
+
self.resource_monitor = {
|
| 42 |
+
'vram_used': 0,
|
| 43 |
+
'active_tensors': 0,
|
| 44 |
+
'loaded_models': set()
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Start WebSocket connection in a separate thread
|
| 48 |
+
self.ws_thread = threading.Thread(target=self._run_websocket_loop, daemon=True)
|
| 49 |
+
self.ws_thread.start()
|
| 50 |
+
self.initialized = True
|
| 51 |
+
|
| 52 |
+
def __init__(self, url: str = "wss://factorst-wbs1.hf.space/ws"):
|
| 53 |
+
"""This will actually just return the singleton instance"""
|
| 54 |
+
pass
|
| 55 |
+
|
| 56 |
+
def _run_websocket_loop(self):
|
| 57 |
+
self._loop = asyncio.new_event_loop()
|
| 58 |
+
asyncio.set_event_loop(self._loop)
|
| 59 |
+
self._loop.run_until_complete(self._websocket_handler())
|
| 60 |
+
|
| 61 |
+
async def _websocket_handler(self):
|
| 62 |
+
while not self._closing:
|
| 63 |
+
try:
|
| 64 |
+
async with websockets.connect(self.url) as websocket:
|
| 65 |
+
self.websocket = websocket
|
| 66 |
+
self.connected = True
|
| 67 |
+
self.error_count = 0 # Reset error count on successful connection
|
| 68 |
+
print("Connected to GPU storage server")
|
| 69 |
+
|
| 70 |
+
while True:
|
| 71 |
+
# Handle outgoing messages
|
| 72 |
+
try:
|
| 73 |
+
while not self.message_queue.empty():
|
| 74 |
+
msg_id, operation = self.message_queue.get()
|
| 75 |
+
await websocket.send(json.dumps(operation))
|
| 76 |
+
|
| 77 |
+
# Wait for response with timeout
|
| 78 |
+
try:
|
| 79 |
+
response = await asyncio.wait_for(websocket.recv(), timeout=30)
|
| 80 |
+
response_data = json.loads(response)
|
| 81 |
+
|
| 82 |
+
# Put response in corresponding queue
|
| 83 |
+
if msg_id in self.response_queues:
|
| 84 |
+
self.response_queues[msg_id].put(response_data)
|
| 85 |
+
except asyncio.TimeoutError:
|
| 86 |
+
if msg_id in self.response_queues:
|
| 87 |
+
self.response_queues[msg_id].put({
|
| 88 |
+
"status": "error",
|
| 89 |
+
"message": "Operation timed out"
|
| 90 |
+
})
|
| 91 |
+
except Exception as e:
|
| 92 |
+
if msg_id in self.response_queues:
|
| 93 |
+
self.response_queues[msg_id].put({
|
| 94 |
+
"status": "error",
|
| 95 |
+
"message": f"Error processing response: {str(e)}"
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Error processing message: {str(e)}")
|
| 100 |
+
|
| 101 |
+
# Keep connection alive with heartbeat
|
| 102 |
+
try:
|
| 103 |
+
await websocket.ping()
|
| 104 |
+
except:
|
| 105 |
+
break # Break inner loop on ping failure
|
| 106 |
+
|
| 107 |
+
await asyncio.sleep(0.001) # 1ms sleep for electron-speed response
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"WebSocket connection error: {e}")
|
| 111 |
+
self.connected = False
|
| 112 |
+
await asyncio.sleep(1) # Wait before reconnecting
|
| 113 |
+
|
| 114 |
+
def _send_operation(self, operation: Dict[str, Any]) -> Dict[str, Any]:
|
| 115 |
+
if self._closing:
|
| 116 |
+
return {"status": "error", "message": "WebSocket is closing"}
|
| 117 |
+
|
| 118 |
+
if not self.wait_for_connection(timeout=10):
|
| 119 |
+
return {"status": "error", "message": "Not connected to GPU storage server"}
|
| 120 |
+
|
| 121 |
+
msg_id = str(time.time())
|
| 122 |
+
response_queue = Queue()
|
| 123 |
+
|
| 124 |
+
with self.lock:
|
| 125 |
+
self.response_queues[msg_id] = response_queue
|
| 126 |
+
self.message_queue.put((msg_id, operation))
|
| 127 |
+
|
| 128 |
+
try:
|
| 129 |
+
# Wait for response with configurable timeout
|
| 130 |
+
response = response_queue.get(timeout=30) # Extended timeout for large models
|
| 131 |
+
if response.get("status") == "error" and "model_size" in operation:
|
| 132 |
+
# Retry once for model loading operations
|
| 133 |
+
self.message_queue.put((msg_id, operation))
|
| 134 |
+
response = response_queue.get(timeout=30)
|
| 135 |
+
except Exception as e:
|
| 136 |
+
response = {"status": "error", "message": f"Operation failed: {str(e)}"}
|
| 137 |
+
finally:
|
| 138 |
+
with self.lock:
|
| 139 |
+
if msg_id in self.response_queues:
|
| 140 |
+
del self.response_queues[msg_id]
|
| 141 |
+
|
| 142 |
+
return response
|
| 143 |
+
|
| 144 |
+
def store_tensor(self, tensor_id: str, data: np.ndarray, model_size: Optional[int] = None) -> bool:
|
| 145 |
+
try:
|
| 146 |
+
if data is None:
|
| 147 |
+
raise ValueError("Cannot store None tensor")
|
| 148 |
+
|
| 149 |
+
# Calculate tensor metadata
|
| 150 |
+
tensor_shape = data.shape
|
| 151 |
+
tensor_dtype = str(data.dtype)
|
| 152 |
+
tensor_size = data.nbytes
|
| 153 |
+
|
| 154 |
+
operation = {
|
| 155 |
+
'operation': 'vram',
|
| 156 |
+
'type': 'write',
|
| 157 |
+
'block_id': tensor_id,
|
| 158 |
+
'data': data.tolist(),
|
| 159 |
+
'model_size': model_size if model_size is not None else -1, # -1 indicates unlimited
|
| 160 |
+
'metadata': {
|
| 161 |
+
'shape': tensor_shape,
|
| 162 |
+
'dtype': tensor_dtype,
|
| 163 |
+
'size': tensor_size,
|
| 164 |
+
'timestamp': time.time()
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
response = self._send_operation(operation)
|
| 169 |
+
if response.get('status') == 'success':
|
| 170 |
+
# Update tensor registry
|
| 171 |
+
with self.lock:
|
| 172 |
+
self.tensor_registry[tensor_id] = {
|
| 173 |
+
'shape': tensor_shape,
|
| 174 |
+
'dtype': tensor_dtype,
|
| 175 |
+
'size': tensor_size,
|
| 176 |
+
'timestamp': time.time()
|
| 177 |
+
}
|
| 178 |
+
self.resource_monitor['vram_used'] += tensor_size
|
| 179 |
+
self.resource_monitor['active_tensors'] += 1
|
| 180 |
+
return True
|
| 181 |
+
else:
|
| 182 |
+
print(f"Failed to store tensor {tensor_id}: {response.get('message', 'Unknown error')}")
|
| 183 |
+
return False
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"Error storing tensor {tensor_id}: {str(e)}")
|
| 186 |
+
return False
|
| 187 |
+
|
| 188 |
+
def load_tensor(self, tensor_id: str) -> Optional[np.ndarray]:
|
| 189 |
+
try:
|
| 190 |
+
# Check tensor registry first
|
| 191 |
+
if tensor_id not in self.tensor_registry:
|
| 192 |
+
print(f"Tensor {tensor_id} not registered in VRAM")
|
| 193 |
+
return None
|
| 194 |
+
|
| 195 |
+
operation = {
|
| 196 |
+
'operation': 'vram',
|
| 197 |
+
'type': 'read',
|
| 198 |
+
'block_id': tensor_id,
|
| 199 |
+
'expected_metadata': self.tensor_registry.get(tensor_id, {})
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
response = self._send_operation(operation)
|
| 203 |
+
if response.get('status') == 'success':
|
| 204 |
+
data = response.get('data')
|
| 205 |
+
if data is None:
|
| 206 |
+
print(f"No data found for tensor {tensor_id}")
|
| 207 |
+
return None
|
| 208 |
+
|
| 209 |
+
# Verify tensor metadata
|
| 210 |
+
metadata = response.get('metadata', {})
|
| 211 |
+
expected_metadata = self.tensor_registry.get(tensor_id, {})
|
| 212 |
+
if metadata.get('shape') != expected_metadata.get('shape'):
|
| 213 |
+
print(f"Warning: Tensor {tensor_id} shape mismatch")
|
| 214 |
+
|
| 215 |
+
try:
|
| 216 |
+
# Convert to numpy array with correct dtype
|
| 217 |
+
arr = np.array(data, dtype=np.dtype(expected_metadata.get('dtype', 'float32')))
|
| 218 |
+
if arr.shape != expected_metadata.get('shape'):
|
| 219 |
+
arr = arr.reshape(expected_metadata.get('shape'))
|
| 220 |
+
return arr
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"Error converting tensor data: {str(e)}")
|
| 223 |
+
return None
|
| 224 |
+
else:
|
| 225 |
+
print(f"Failed to load tensor {tensor_id}: {response.get('message', 'Unknown error')}")
|
| 226 |
+
return None
|
| 227 |
+
except Exception as e:
|
| 228 |
+
print(f"Error loading tensor {tensor_id}: {str(e)}")
|
| 229 |
+
return None
|
| 230 |
+
|
| 231 |
+
def store_state(self, component: str, state_id: str, state_data: Dict[str, Any]) -> bool:
|
| 232 |
+
try:
|
| 233 |
+
operation = {
|
| 234 |
+
'operation': 'state',
|
| 235 |
+
'type': 'save',
|
| 236 |
+
'component': component,
|
| 237 |
+
'state_id': state_id,
|
| 238 |
+
'data': state_data,
|
| 239 |
+
'timestamp': time.time()
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
response = self._send_operation(operation)
|
| 243 |
+
if response.get('status') != 'success':
|
| 244 |
+
print(f"Failed to store state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
|
| 245 |
+
return False
|
| 246 |
+
return True
|
| 247 |
+
except Exception as e:
|
| 248 |
+
print(f"Error storing state for {component}/{state_id}: {str(e)}")
|
| 249 |
+
return False
|
| 250 |
+
|
| 251 |
+
def load_state(self, component: str, state_id: str) -> Optional[Dict[str, Any]]:
|
| 252 |
+
try:
|
| 253 |
+
operation = {
|
| 254 |
+
'operation': 'state',
|
| 255 |
+
'type': 'load',
|
| 256 |
+
'component': component,
|
| 257 |
+
'state_id': state_id
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
response = self._send_operation(operation)
|
| 261 |
+
if response.get('status') == 'success':
|
| 262 |
+
data = response.get('data')
|
| 263 |
+
if data is None:
|
| 264 |
+
print(f"No state found for {component}/{state_id}")
|
| 265 |
+
return None
|
| 266 |
+
return data
|
| 267 |
+
else:
|
| 268 |
+
print(f"Failed to load state for {component}/{state_id}: {response.get('message', 'Unknown error')}")
|
| 269 |
+
return None
|
| 270 |
+
except Exception as e:
|
| 271 |
+
print(f"Error loading state for {component}/{state_id}: {str(e)}")
|
| 272 |
+
return None
|
| 273 |
+
|
| 274 |
+
def is_model_loaded(self, model_name: str) -> bool:
|
| 275 |
+
"""Check if a model is already loaded in VRAM"""
|
| 276 |
+
return model_name in self.resource_monitor['loaded_models']
|
| 277 |
+
|
| 278 |
+
def load_model(self, model_name: str, model_path: Optional[str] = None, model_data: Optional[Dict] = None) -> bool:
|
| 279 |
+
"""Load a model into VRAM if not already loaded"""
|
| 280 |
+
try:
|
| 281 |
+
# Check if model is already loaded
|
| 282 |
+
if self.is_model_loaded(model_name):
|
| 283 |
+
print(f"Model {model_name} already loaded in VRAM")
|
| 284 |
+
return True
|
| 285 |
+
|
| 286 |
+
# Calculate model hash if path provided
|
| 287 |
+
model_hash = None
|
| 288 |
+
if model_path:
|
| 289 |
+
model_hash = self._calculate_model_hash(model_path)
|
| 290 |
+
|
| 291 |
+
operation = {
|
| 292 |
+
'operation': 'model',
|
| 293 |
+
'type': 'load',
|
| 294 |
+
'model_name': model_name,
|
| 295 |
+
'model_hash': model_hash,
|
| 296 |
+
'model_data': model_data
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
response = self._send_operation(operation)
|
| 300 |
+
if response.get('status') == 'success':
|
| 301 |
+
with self.lock:
|
| 302 |
+
self.model_registry[model_name] = {
|
| 303 |
+
'hash': model_hash,
|
| 304 |
+
'timestamp': time.time(),
|
| 305 |
+
'tensors': response.get('tensor_ids', [])
|
| 306 |
+
}
|
| 307 |
+
self.resource_monitor['loaded_models'].add(model_name)
|
| 308 |
+
print(f"Successfully loaded model {model_name}")
|
| 309 |
+
return True
|
| 310 |
+
else:
|
| 311 |
+
print(f"Failed to load model {model_name}: {response.get('message', 'Unknown error')}")
|
| 312 |
+
return False
|
| 313 |
+
except Exception as e:
|
| 314 |
+
print(f"Error loading model {model_name}: {str(e)}")
|
| 315 |
+
return False
|
| 316 |
+
|
| 317 |
+
def _calculate_model_hash(self, model_path: str) -> str:
|
| 318 |
+
"""Calculate SHA256 hash of model file"""
|
| 319 |
+
try:
|
| 320 |
+
sha256_hash = hashlib.sha256()
|
| 321 |
+
with open(model_path, "rb") as f:
|
| 322 |
+
for byte_block in iter(lambda: f.read(4096), b""):
|
| 323 |
+
sha256_hash.update(byte_block)
|
| 324 |
+
return sha256_hash.hexdigest()
|
| 325 |
+
except Exception as e:
|
| 326 |
+
print(f"Error calculating model hash: {str(e)}")
|
| 327 |
+
return ""
|
| 328 |
+
|
| 329 |
+
def cache_data(self, key: str, data: Any) -> bool:
|
| 330 |
+
operation = {
|
| 331 |
+
'operation': 'cache',
|
| 332 |
+
'type': 'set',
|
| 333 |
+
'key': key,
|
| 334 |
+
'data': data
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
response = self._send_operation(operation)
|
| 338 |
+
return response.get('status') == 'success'
|
| 339 |
+
|
| 340 |
+
def get_cached_data(self, key: str) -> Optional[Any]:
|
| 341 |
+
operation = {
|
| 342 |
+
'operation': 'cache',
|
| 343 |
+
'type': 'get',
|
| 344 |
+
'key': key
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
response = self._send_operation(operation)
|
| 348 |
+
if response.get('status') == 'success':
|
| 349 |
+
return response['data']
|
| 350 |
+
return None
|
| 351 |
+
|
| 352 |
+
def wait_for_connection(self, timeout: float = 30.0) -> bool:
|
| 353 |
+
"""Wait for WebSocket connection to be established"""
|
| 354 |
+
start_time = time.time()
|
| 355 |
+
while not self._closing and not self.connected:
|
| 356 |
+
if time.time() - start_time > timeout:
|
| 357 |
+
print("Connection timeout exceeded")
|
| 358 |
+
return False
|
| 359 |
+
time.sleep(0.1)
|
| 360 |
+
return self.connected
|
| 361 |
+
|
| 362 |
+
def is_connected(self) -> bool:
|
| 363 |
+
"""Check if WebSocket connection is active"""
|
| 364 |
+
return self.connected and not self._closing
|
| 365 |
+
|
| 366 |
+
def get_connection_status(self) -> Dict[str, Any]:
|
| 367 |
+
"""Get detailed connection status"""
|
| 368 |
+
return {
|
| 369 |
+
"connected": self.connected,
|
| 370 |
+
"closing": self._closing,
|
| 371 |
+
"error_count": self.error_count,
|
| 372 |
+
"url": self.url,
|
| 373 |
+
"last_error_time": self.last_error_time,
|
| 374 |
+
"loaded_models": list(self.resource_monitor['loaded_models'])
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
def start_inference(self, model_name: str, input_data: np.ndarray) -> Optional[Dict[str, Any]]:
|
| 378 |
+
"""Start inference with a loaded model"""
|
| 379 |
+
try:
|
| 380 |
+
if not self.is_model_loaded(model_name):
|
| 381 |
+
print(f"Model {model_name} not loaded. Please load the model first.")
|
| 382 |
+
return None
|
| 383 |
+
|
| 384 |
+
operation = {
|
| 385 |
+
'operation': 'inference',
|
| 386 |
+
'type': 'run',
|
| 387 |
+
'model_name': model_name,
|
| 388 |
+
'input_data': input_data.tolist() if isinstance(input_data, np.ndarray) else input_data
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
response = self._send_operation(operation)
|
| 392 |
+
if response.get('status') == 'success':
|
| 393 |
+
return {
|
| 394 |
+
'output': np.array(response['output']) if 'output' in response else None,
|
| 395 |
+
'metrics': response.get('metrics', {}),
|
| 396 |
+
'model_info': self.model_registry.get(model_name, {})
|
| 397 |
+
}
|
| 398 |
+
else:
|
| 399 |
+
print(f"Inference failed: {response.get('message', 'Unknown error')}")
|
| 400 |
+
return None
|
| 401 |
+
except Exception as e:
|
| 402 |
+
print(f"Error during inference: {str(e)}")
|
| 403 |
+
return None
|
| 404 |
+
|
| 405 |
+
def close(self):
|
| 406 |
+
"""Close WebSocket connection and cleanup resources."""
|
| 407 |
+
if not self._closing:
|
| 408 |
+
self._closing = True
|
| 409 |
+
if self.websocket and self._loop:
|
| 410 |
+
async def cleanup():
|
| 411 |
+
try:
|
| 412 |
+
# Clean up registries
|
| 413 |
+
with self.lock:
|
| 414 |
+
self.tensor_registry.clear()
|
| 415 |
+
self.model_registry.clear()
|
| 416 |
+
self.resource_monitor['vram_used'] = 0
|
| 417 |
+
self.resource_monitor['active_tensors'] = 0
|
| 418 |
+
self.resource_monitor['loaded_models'].clear()
|
| 419 |
+
|
| 420 |
+
# Notify server about cleanup
|
| 421 |
+
if self.connected:
|
| 422 |
+
try:
|
| 423 |
+
await self.websocket.send(json.dumps({
|
| 424 |
+
'operation': 'cleanup',
|
| 425 |
+
'type': 'full'
|
| 426 |
+
}))
|
| 427 |
+
except:
|
| 428 |
+
pass
|
| 429 |
+
|
| 430 |
+
await self.websocket.close()
|
| 431 |
+
except Exception as e:
|
| 432 |
+
print(f"Error during cleanup: {str(e)}")
|
| 433 |
+
finally:
|
| 434 |
+
self.connected = False
|
| 435 |
+
|
| 436 |
+
if self._loop.is_running():
|
| 437 |
+
self._loop.create_task(cleanup())
|
| 438 |
+
else:
|
| 439 |
+
asyncio.run(cleanup())
|
| 440 |
+
|
| 441 |
+
async def aclose(self):
|
| 442 |
+
"""Asynchronously close WebSocket connection."""
|
| 443 |
+
if not self._closing:
|
| 444 |
+
self._closing = True
|
| 445 |
+
if self.websocket:
|
| 446 |
+
try:
|
| 447 |
+
await self.websocket.close()
|
| 448 |
+
except:
|
| 449 |
+
pass
|
| 450 |
+
finally:
|
| 451 |
+
self.connected = False
|
| 452 |
+
|
| 453 |
+
def __del__(self):
|
| 454 |
+
"""Ensure cleanup on deletion."""
|
| 455 |
+
self.close()
|