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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
AI Accelerator Module
|
| 3 |
+
|
| 4 |
+
This module implements AI-specific operations, treating the vGPU as a tensor engine
|
| 5 |
+
and leveraging the simulated parallelism of 50,000 cores and 800 SMs.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import time
|
| 10 |
+
from typing import Dict, Any, Optional, Tuple, Union, List
|
| 11 |
+
from enum import Enum
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class VectorOperation(Enum):
|
| 15 |
+
"""Enumeration of supported vector operations."""
|
| 16 |
+
ADD = "add"
|
| 17 |
+
SUBTRACT = "subtract"
|
| 18 |
+
MULTIPLY = "multiply"
|
| 19 |
+
DIVIDE = "divide"
|
| 20 |
+
DOT_PRODUCT = "dot_product"
|
| 21 |
+
CROSS_PRODUCT = "cross_product"
|
| 22 |
+
NORMALIZE = "normalize"
|
| 23 |
+
MAGNITUDE = "magnitude"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class AIAccelerator:
|
| 27 |
+
"""
|
| 28 |
+
AI Accelerator that simulates GPU-based AI computations.
|
| 29 |
+
|
| 30 |
+
This class leverages NumPy's optimized operations to simulate the parallel
|
| 31 |
+
processing capabilities of the vGPU for AI workloads.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def __init__(self, vram=None, num_sms: int = 800, cores_per_sm: int = 62):
|
| 35 |
+
self.vram = vram
|
| 36 |
+
self.num_sms = num_sms
|
| 37 |
+
self.cores_per_sm = cores_per_sm
|
| 38 |
+
self.total_cores = num_sms * cores_per_sm
|
| 39 |
+
|
| 40 |
+
# AI operation statistics
|
| 41 |
+
self.operations_performed = 0
|
| 42 |
+
self.total_compute_time = 0.0
|
| 43 |
+
self.flops_performed = 0 # Floating point operations
|
| 44 |
+
|
| 45 |
+
# Matrix registry for storing matrices in VRAM
|
| 46 |
+
self.matrix_registry: Dict[str, str] = {} # matrix_id -> vram_address
|
| 47 |
+
self.matrix_counter = 0
|
| 48 |
+
|
| 49 |
+
def set_vram(self, vram):
|
| 50 |
+
"""Set the VRAM reference."""
|
| 51 |
+
self.vram = vram
|
| 52 |
+
|
| 53 |
+
def allocate_matrix(self, shape: Tuple[int, ...], dtype=np.float32,
|
| 54 |
+
name: Optional[str] = None) -> str:
|
| 55 |
+
"""Allocate a matrix in VRAM and return its ID."""
|
| 56 |
+
if not self.vram:
|
| 57 |
+
raise RuntimeError("VRAM not available")
|
| 58 |
+
|
| 59 |
+
if name is None:
|
| 60 |
+
name = f"matrix_{self.matrix_counter}"
|
| 61 |
+
self.matrix_counter += 1
|
| 62 |
+
|
| 63 |
+
# Create matrix data
|
| 64 |
+
matrix_data = np.zeros(shape, dtype=dtype)
|
| 65 |
+
|
| 66 |
+
# Store in VRAM as a texture (reusing texture storage mechanism)
|
| 67 |
+
matrix_id = self.vram.load_texture(matrix_data, name)
|
| 68 |
+
self.matrix_registry[name] = matrix_id
|
| 69 |
+
|
| 70 |
+
return name
|
| 71 |
+
|
| 72 |
+
def load_matrix(self, matrix_data: np.ndarray, name: Optional[str] = None) -> str:
|
| 73 |
+
"""Load matrix data into VRAM and return its ID."""
|
| 74 |
+
if not self.vram:
|
| 75 |
+
raise RuntimeError("VRAM not available")
|
| 76 |
+
|
| 77 |
+
if name is None:
|
| 78 |
+
name = f"matrix_{self.matrix_counter}"
|
| 79 |
+
self.matrix_counter += 1
|
| 80 |
+
|
| 81 |
+
# Store in VRAM
|
| 82 |
+
matrix_id = self.vram.load_texture(matrix_data, name)
|
| 83 |
+
self.matrix_registry[name] = matrix_id
|
| 84 |
+
|
| 85 |
+
return name
|
| 86 |
+
|
| 87 |
+
def get_matrix(self, matrix_id: str) -> Optional[np.ndarray]:
|
| 88 |
+
"""Retrieve matrix data from VRAM."""
|
| 89 |
+
if not self.vram or matrix_id not in self.matrix_registry:
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
vram_id = self.matrix_registry[matrix_id]
|
| 93 |
+
return self.vram.get_texture(vram_id)
|
| 94 |
+
|
| 95 |
+
def matrix_multiply(self, matrix_a_id: str, matrix_b_id: str,
|
| 96 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 97 |
+
"""Perform matrix multiplication using simulated GPU parallelism."""
|
| 98 |
+
start_time = time.time()
|
| 99 |
+
|
| 100 |
+
# Retrieve matrices from VRAM
|
| 101 |
+
matrix_a = self.get_matrix(matrix_a_id)
|
| 102 |
+
matrix_b = self.get_matrix(matrix_b_id)
|
| 103 |
+
|
| 104 |
+
if matrix_a is None or matrix_b is None:
|
| 105 |
+
print(f"Error: Could not retrieve matrices {matrix_a_id} or {matrix_b_id}")
|
| 106 |
+
return None
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
# Check if matrices can be multiplied
|
| 110 |
+
if matrix_a.shape[-1] != matrix_b.shape[0]:
|
| 111 |
+
print(f"Error: Matrix dimensions incompatible for multiplication: "
|
| 112 |
+
f"{matrix_a.shape} x {matrix_b.shape}")
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
# Simulate parallel processing by breaking down the operation
|
| 116 |
+
# In a real GPU, this would be distributed across SMs and cores
|
| 117 |
+
result = self._simulate_parallel_matmul(matrix_a, matrix_b)
|
| 118 |
+
|
| 119 |
+
# Store result in VRAM
|
| 120 |
+
if result_id is None:
|
| 121 |
+
result_id = f"result_{self.matrix_counter}"
|
| 122 |
+
self.matrix_counter += 1
|
| 123 |
+
|
| 124 |
+
result_matrix_id = self.load_matrix(result, result_id)
|
| 125 |
+
|
| 126 |
+
# Update statistics
|
| 127 |
+
compute_time = time.time() - start_time
|
| 128 |
+
self.total_compute_time += compute_time
|
| 129 |
+
self.operations_performed += 1
|
| 130 |
+
|
| 131 |
+
# Calculate FLOPs (2 * M * N * K for matrix multiplication)
|
| 132 |
+
m, k = matrix_a.shape
|
| 133 |
+
k2, n = matrix_b.shape
|
| 134 |
+
flops = 2 * m * n * k
|
| 135 |
+
self.flops_performed += flops
|
| 136 |
+
|
| 137 |
+
print(f"Matrix multiplication completed: {matrix_a.shape} x {matrix_b.shape} "
|
| 138 |
+
f"= {result.shape} in {compute_time:.4f}s")
|
| 139 |
+
print(f"Simulated {flops:,} FLOPs across {self.total_cores} cores")
|
| 140 |
+
|
| 141 |
+
return result_matrix_id
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
print(f"Error in matrix multiplication: {e}")
|
| 145 |
+
return None
|
| 146 |
+
|
| 147 |
+
def _simulate_parallel_matmul(self, matrix_a: np.ndarray, matrix_b: np.ndarray) -> np.ndarray:
|
| 148 |
+
"""Simulate parallel matrix multiplication across SMs."""
|
| 149 |
+
# Use NumPy's optimized matrix multiplication
|
| 150 |
+
# In a real implementation, this would be broken down into blocks
|
| 151 |
+
# and distributed across the simulated SMs
|
| 152 |
+
|
| 153 |
+
# For demonstration, we can show how the work would be distributed
|
| 154 |
+
m, k = matrix_a.shape
|
| 155 |
+
k2, n = matrix_b.shape
|
| 156 |
+
|
| 157 |
+
# Calculate work distribution
|
| 158 |
+
total_output_elements = m * n
|
| 159 |
+
elements_per_sm = max(1, total_output_elements // self.num_sms)
|
| 160 |
+
|
| 161 |
+
print(f"Distributing {total_output_elements:,} output elements across "
|
| 162 |
+
f"{self.num_sms} SMs ({elements_per_sm} elements per SM)")
|
| 163 |
+
|
| 164 |
+
# Perform the actual computation using NumPy
|
| 165 |
+
result = np.dot(matrix_a, matrix_b)
|
| 166 |
+
|
| 167 |
+
return result
|
| 168 |
+
|
| 169 |
+
def vector_operation(self, operation: VectorOperation, vector_a_id: str,
|
| 170 |
+
vector_b_id: Optional[str] = None,
|
| 171 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 172 |
+
"""Perform vector operations using simulated GPU parallelism."""
|
| 173 |
+
start_time = time.time()
|
| 174 |
+
|
| 175 |
+
# Retrieve vectors from VRAM
|
| 176 |
+
vector_a = self.get_matrix(vector_a_id)
|
| 177 |
+
if vector_a is None:
|
| 178 |
+
print(f"Error: Could not retrieve vector {vector_a_id}")
|
| 179 |
+
return None
|
| 180 |
+
|
| 181 |
+
vector_b = None
|
| 182 |
+
if vector_b_id:
|
| 183 |
+
vector_b = self.get_matrix(vector_b_id)
|
| 184 |
+
if vector_b is None:
|
| 185 |
+
print(f"Error: Could not retrieve vector {vector_b_id}")
|
| 186 |
+
return None
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
result = None
|
| 190 |
+
flops = 0
|
| 191 |
+
|
| 192 |
+
if operation == VectorOperation.ADD:
|
| 193 |
+
if vector_b is None:
|
| 194 |
+
raise ValueError("Vector B required for addition")
|
| 195 |
+
result = vector_a + vector_b
|
| 196 |
+
flops = vector_a.size
|
| 197 |
+
|
| 198 |
+
elif operation == VectorOperation.SUBTRACT:
|
| 199 |
+
if vector_b is None:
|
| 200 |
+
raise ValueError("Vector B required for subtraction")
|
| 201 |
+
result = vector_a - vector_b
|
| 202 |
+
flops = vector_a.size
|
| 203 |
+
|
| 204 |
+
elif operation == VectorOperation.MULTIPLY:
|
| 205 |
+
if vector_b is None:
|
| 206 |
+
raise ValueError("Vector B required for multiplication")
|
| 207 |
+
result = vector_a * vector_b
|
| 208 |
+
flops = vector_a.size
|
| 209 |
+
|
| 210 |
+
elif operation == VectorOperation.DIVIDE:
|
| 211 |
+
if vector_b is None:
|
| 212 |
+
raise ValueError("Vector B required for division")
|
| 213 |
+
result = vector_a / vector_b
|
| 214 |
+
flops = vector_a.size
|
| 215 |
+
|
| 216 |
+
elif operation == VectorOperation.DOT_PRODUCT:
|
| 217 |
+
if vector_b is None:
|
| 218 |
+
raise ValueError("Vector B required for dot product")
|
| 219 |
+
result = np.dot(vector_a.flatten(), vector_b.flatten())
|
| 220 |
+
flops = 2 * vector_a.size
|
| 221 |
+
|
| 222 |
+
elif operation == VectorOperation.CROSS_PRODUCT:
|
| 223 |
+
if vector_b is None:
|
| 224 |
+
raise ValueError("Vector B required for cross product")
|
| 225 |
+
result = np.cross(vector_a, vector_b)
|
| 226 |
+
flops = 6 # Approximate for 3D cross product
|
| 227 |
+
|
| 228 |
+
elif operation == VectorOperation.NORMALIZE:
|
| 229 |
+
magnitude = np.linalg.norm(vector_a)
|
| 230 |
+
result = vector_a / magnitude if magnitude > 0 else vector_a
|
| 231 |
+
flops = vector_a.size * 2 # Division + magnitude calculation
|
| 232 |
+
|
| 233 |
+
elif operation == VectorOperation.MAGNITUDE:
|
| 234 |
+
result = np.array([np.linalg.norm(vector_a)])
|
| 235 |
+
flops = vector_a.size * 2 # Squares and sum
|
| 236 |
+
|
| 237 |
+
else:
|
| 238 |
+
raise ValueError(f"Unsupported vector operation: {operation}")
|
| 239 |
+
|
| 240 |
+
# Store result in VRAM
|
| 241 |
+
if result_id is None:
|
| 242 |
+
result_id = f"vector_result_{self.matrix_counter}"
|
| 243 |
+
self.matrix_counter += 1
|
| 244 |
+
|
| 245 |
+
result_vector_id = self.load_matrix(result, result_id)
|
| 246 |
+
|
| 247 |
+
# Update statistics
|
| 248 |
+
compute_time = time.time() - start_time
|
| 249 |
+
self.total_compute_time += compute_time
|
| 250 |
+
self.operations_performed += 1
|
| 251 |
+
self.flops_performed += flops
|
| 252 |
+
|
| 253 |
+
print(f"Vector operation {operation.value} completed in {compute_time:.4f}s")
|
| 254 |
+
|
| 255 |
+
return result_vector_id
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
print(f"Error in vector operation {operation.value}: {e}")
|
| 259 |
+
return None
|
| 260 |
+
|
| 261 |
+
def convolution_2d(self, input_id: str, kernel_id: str,
|
| 262 |
+
stride: int = 1, padding: int = 0,
|
| 263 |
+
result_id: Optional[str] = None) -> Optional[str]:
|
| 264 |
+
"""Perform 2D convolution operation."""
|
| 265 |
+
start_time = time.time()
|
| 266 |
+
|
| 267 |
+
# Retrieve input and kernel from VRAM
|
| 268 |
+
input_data = self.get_matrix(input_id)
|
| 269 |
+
kernel = self.get_matrix(kernel_id)
|
| 270 |
+
|
| 271 |
+
if input_data is None or kernel is None:
|
| 272 |
+
print(f"Error: Could not retrieve input or kernel")
|
| 273 |
+
return None
|
| 274 |
+
|
| 275 |
+
try:
|
| 276 |
+
# Simple 2D convolution implementation
|
| 277 |
+
# In a real GPU implementation, this would be highly optimized
|
| 278 |
+
# and distributed across many cores
|
| 279 |
+
|
| 280 |
+
if len(input_data.shape) == 2:
|
| 281 |
+
input_h, input_w = input_data.shape
|
| 282 |
+
channels = 1
|
| 283 |
+
else:
|
| 284 |
+
input_h, input_w, channels = input_data.shape
|
| 285 |
+
|
| 286 |
+
kernel_h, kernel_w = kernel.shape[:2]
|
| 287 |
+
|
| 288 |
+
# Calculate output dimensions
|
| 289 |
+
output_h = (input_h + 2 * padding - kernel_h) // stride + 1
|
| 290 |
+
output_w = (input_w + 2 * padding - kernel_w) // stride + 1
|
| 291 |
+
|
| 292 |
+
# Initialize output
|
| 293 |
+
if channels == 1:
|
| 294 |
+
output = np.zeros((output_h, output_w))
|
| 295 |
+
else:
|
| 296 |
+
output = np.zeros((output_h, output_w, channels))
|
| 297 |
+
|
| 298 |
+
# Pad input if necessary
|
| 299 |
+
if padding > 0:
|
| 300 |
+
if channels == 1:
|
| 301 |
+
padded_input = np.pad(input_data, padding, mode='constant')
|
| 302 |
+
else:
|
| 303 |
+
padded_input = np.pad(input_data,
|
| 304 |
+
((padding, padding), (padding, padding), (0, 0)),
|
| 305 |
+
mode='constant')
|
| 306 |
+
else:
|
| 307 |
+
padded_input = input_data
|
| 308 |
+
|
| 309 |
+
# Perform convolution
|
| 310 |
+
flops = 0
|
| 311 |
+
for y in range(0, output_h):
|
| 312 |
+
for x in range(0, output_w):
|
| 313 |
+
y_start = y * stride
|
| 314 |
+
x_start = x * stride
|
| 315 |
+
|
| 316 |
+
if channels == 1:
|
| 317 |
+
patch = padded_input[y_start:y_start+kernel_h, x_start:x_start+kernel_w]
|
| 318 |
+
output[y, x] = np.sum(patch * kernel)
|
| 319 |
+
flops += kernel_h * kernel_w * 2 # Multiply and add
|
| 320 |
+
else:
|
| 321 |
+
for c in range(channels):
|
| 322 |
+
patch = padded_input[y_start:y_start+kernel_h,
|
| 323 |
+
x_start:x_start+kernel_w, c]
|
| 324 |
+
output[y, x, c] = np.sum(patch * kernel)
|
| 325 |
+
flops += kernel_h * kernel_w * 2
|
| 326 |
+
|
| 327 |
+
# Store result in VRAM
|
| 328 |
+
if result_id is None:
|
| 329 |
+
result_id = f"conv_result_{self.matrix_counter}"
|
| 330 |
+
self.matrix_counter += 1
|
| 331 |
+
|
| 332 |
+
result_conv_id = self.load_matrix(output, result_id)
|
| 333 |
+
|
| 334 |
+
# Update statistics
|
| 335 |
+
compute_time = time.time() - start_time
|
| 336 |
+
self.total_compute_time += compute_time
|
| 337 |
+
self.operations_performed += 1
|
| 338 |
+
self.flops_performed += flops
|
| 339 |
+
|
| 340 |
+
print(f"2D Convolution completed: {input_data.shape} * {kernel.shape} "
|
| 341 |
+
f"= {output.shape} in {compute_time:.4f}s")
|
| 342 |
+
print(f"Simulated {flops:,} FLOPs")
|
| 343 |
+
|
| 344 |
+
return result_conv_id
|
| 345 |
+
|
| 346 |
+
except Exception as e:
|
| 347 |
+
print(f"Error in 2D convolution: {e}")
|
| 348 |
+
return None
|
| 349 |
+
|
| 350 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 351 |
+
"""Get AI accelerator statistics."""
|
| 352 |
+
avg_compute_time = self.total_compute_time / max(1, self.operations_performed)
|
| 353 |
+
flops_per_second = self.flops_performed / max(0.001, self.total_compute_time)
|
| 354 |
+
|
| 355 |
+
return {
|
| 356 |
+
"operations_performed": self.operations_performed,
|
| 357 |
+
"total_compute_time": self.total_compute_time,
|
| 358 |
+
"avg_compute_time": avg_compute_time,
|
| 359 |
+
"flops_performed": self.flops_performed,
|
| 360 |
+
"flops_per_second": flops_per_second,
|
| 361 |
+
"matrices_in_memory": len(self.matrix_registry),
|
| 362 |
+
"simulated_cores": self.total_cores,
|
| 363 |
+
"simulated_sms": self.num_sms
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
def reset_stats(self) -> None:
|
| 367 |
+
"""Reset AI accelerator statistics."""
|
| 368 |
+
self.operations_performed = 0
|
| 369 |
+
self.total_compute_time = 0.0
|
| 370 |
+
self.flops_performed = 0
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
if __name__ == "__main__":
|
| 374 |
+
# Test the AI accelerator
|
| 375 |
+
from vram import VRAM
|
| 376 |
+
|
| 377 |
+
# Create VRAM and AI accelerator
|
| 378 |
+
vram = VRAM(memory_size_gb=1)
|
| 379 |
+
ai = AIAccelerator(vram)
|
| 380 |
+
|
| 381 |
+
print("Testing AI Accelerator...")
|
| 382 |
+
|
| 383 |
+
# Test matrix operations
|
| 384 |
+
# Create test matrices
|
| 385 |
+
matrix_a = np.random.rand(100, 50).astype(np.float32)
|
| 386 |
+
matrix_b = np.random.rand(50, 75).astype(np.float32)
|
| 387 |
+
|
| 388 |
+
# Load matrices into VRAM
|
| 389 |
+
a_id = ai.load_matrix(matrix_a, "test_matrix_a")
|
| 390 |
+
b_id = ai.load_matrix(matrix_b, "test_matrix_b")
|
| 391 |
+
|
| 392 |
+
# Perform matrix multiplication
|
| 393 |
+
result_id = ai.matrix_multiply(a_id, b_id, "multiplication_result")
|
| 394 |
+
|
| 395 |
+
if result_id:
|
| 396 |
+
result = ai.get_matrix(result_id)
|
| 397 |
+
print(f"Matrix multiplication result shape: {result.shape}")
|
| 398 |
+
|
| 399 |
+
# Verify result
|
| 400 |
+
expected = np.dot(matrix_a, matrix_b)
|
| 401 |
+
if np.allclose(result, expected):
|
| 402 |
+
print("Matrix multiplication result is correct!")
|
| 403 |
+
else:
|
| 404 |
+
print("Matrix multiplication result is incorrect!")
|
| 405 |
+
|
| 406 |
+
# Test vector operations
|
| 407 |
+
vector_a = np.random.rand(1000).astype(np.float32)
|
| 408 |
+
vector_b = np.random.rand(1000).astype(np.float32)
|
| 409 |
+
|
| 410 |
+
va_id = ai.load_matrix(vector_a, "vector_a")
|
| 411 |
+
vb_id = ai.load_matrix(vector_b, "vector_b")
|
| 412 |
+
|
| 413 |
+
# Test vector addition
|
| 414 |
+
add_result_id = ai.vector_operation(VectorOperation.ADD, va_id, vb_id)
|
| 415 |
+
if add_result_id:
|
| 416 |
+
add_result = ai.get_matrix(add_result_id)
|
| 417 |
+
expected_add = vector_a + vector_b
|
| 418 |
+
if np.allclose(add_result, expected_add):
|
| 419 |
+
print("Vector addition result is correct!")
|
| 420 |
+
|
| 421 |
+
# Test dot product
|
| 422 |
+
dot_result_id = ai.vector_operation(VectorOperation.DOT_PRODUCT, va_id, vb_id)
|
| 423 |
+
if dot_result_id:
|
| 424 |
+
dot_result = ai.get_matrix(dot_result_id)
|
| 425 |
+
expected_dot = np.dot(vector_a, vector_b)
|
| 426 |
+
if np.allclose(dot_result[0], expected_dot):
|
| 427 |
+
print("Dot product result is correct!")
|
| 428 |
+
|
| 429 |
+
# Test 2D convolution
|
| 430 |
+
input_image = np.random.rand(32, 32).astype(np.float32)
|
| 431 |
+
kernel = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=np.float32) # Sobel edge detector
|
| 432 |
+
|
| 433 |
+
img_id = ai.load_matrix(input_image, "test_image")
|
| 434 |
+
kernel_id = ai.load_matrix(kernel, "sobel_kernel")
|
| 435 |
+
|
| 436 |
+
conv_result_id = ai.convolution_2d(img_id, kernel_id)
|
| 437 |
+
if conv_result_id:
|
| 438 |
+
conv_result = ai.get_matrix(conv_result_id)
|
| 439 |
+
print(f"Convolution result shape: {conv_result.shape}")
|
| 440 |
+
|
| 441 |
+
# Print final statistics
|
| 442 |
+
stats = ai.get_stats()
|
| 443 |
+
print(f"AI Accelerator stats: {stats}")
|
| 444 |
+
|
| 445 |
+
print("AI Accelerator test completed!")
|
| 446 |
+
|
src/driver.py
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
GPU Driver Module
|
| 3 |
+
|
| 4 |
+
This module acts as the interface between a virtual CPU (or external command source)
|
| 5 |
+
and the vGPU, handling command queuing and interpretation.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import asyncio
|
| 9 |
+
from collections import deque
|
| 10 |
+
from enum import Enum
|
| 11 |
+
from typing import Dict, Any, Optional, List
|
| 12 |
+
from dataclasses import dataclass
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class CommandType(Enum):
|
| 16 |
+
"""Enumeration of supported GPU commands."""
|
| 17 |
+
CLEAR = "clear"
|
| 18 |
+
DRAW_RECT = "draw_rect"
|
| 19 |
+
DRAW_PIXEL = "draw_pixel"
|
| 20 |
+
DRAW_IMAGE = "draw_image"
|
| 21 |
+
SET_SHADER = "set_shader"
|
| 22 |
+
MATRIX_MULTIPLY = "matrix_multiply"
|
| 23 |
+
VECTOR_OP = "vector_op"
|
| 24 |
+
CREATE_FRAMEBUFFER = "create_framebuffer"
|
| 25 |
+
SET_FRAMEBUFFER = "set_framebuffer"
|
| 26 |
+
LOAD_TEXTURE = "load_texture"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass
|
| 30 |
+
class Command:
|
| 31 |
+
"""Represents a single command to be executed by the vGPU."""
|
| 32 |
+
command_id: str
|
| 33 |
+
command_type: CommandType
|
| 34 |
+
parameters: Dict[str, Any]
|
| 35 |
+
priority: int = 0
|
| 36 |
+
timestamp: float = 0.0
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class GPUDriver:
|
| 40 |
+
"""
|
| 41 |
+
GPU Driver that manages command queues and interfaces with the vGPU.
|
| 42 |
+
|
| 43 |
+
This class receives commands from external sources (virtual CPU, applications)
|
| 44 |
+
and translates them into tasks that can be processed by the vGPU.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
def __init__(self, vgpu=None):
|
| 48 |
+
self.vgpu = vgpu
|
| 49 |
+
|
| 50 |
+
# Command queue management
|
| 51 |
+
self.command_queue = deque()
|
| 52 |
+
self.command_counter = 0
|
| 53 |
+
|
| 54 |
+
# Current state
|
| 55 |
+
self.current_framebuffer = None
|
| 56 |
+
self.current_shader = None
|
| 57 |
+
|
| 58 |
+
# Command processing statistics
|
| 59 |
+
self.commands_processed = 0
|
| 60 |
+
self.commands_failed = 0
|
| 61 |
+
|
| 62 |
+
def set_vgpu(self, vgpu):
|
| 63 |
+
"""Set the vGPU reference."""
|
| 64 |
+
self.vgpu = vgpu
|
| 65 |
+
|
| 66 |
+
def submit_command(self, command_type: CommandType, parameters: Dict[str, Any],
|
| 67 |
+
priority: int = 0) -> str:
|
| 68 |
+
"""Submit a command to the GPU driver."""
|
| 69 |
+
command_id = f"cmd_{self.command_counter}"
|
| 70 |
+
self.command_counter += 1
|
| 71 |
+
|
| 72 |
+
command = Command(
|
| 73 |
+
command_id=command_id,
|
| 74 |
+
command_type=command_type,
|
| 75 |
+
parameters=parameters,
|
| 76 |
+
priority=priority,
|
| 77 |
+
timestamp=asyncio.get_event_loop().time()
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Insert command based on priority (higher priority first)
|
| 81 |
+
if priority > 0:
|
| 82 |
+
# Find insertion point for priority queue
|
| 83 |
+
inserted = False
|
| 84 |
+
for i, existing_cmd in enumerate(self.command_queue):
|
| 85 |
+
if existing_cmd.priority < priority:
|
| 86 |
+
self.command_queue.insert(i, command)
|
| 87 |
+
inserted = True
|
| 88 |
+
break
|
| 89 |
+
if not inserted:
|
| 90 |
+
self.command_queue.append(command)
|
| 91 |
+
else:
|
| 92 |
+
self.command_queue.append(command)
|
| 93 |
+
|
| 94 |
+
return command_id
|
| 95 |
+
|
| 96 |
+
async def process_commands(self) -> None:
|
| 97 |
+
"""Process all pending commands in the queue."""
|
| 98 |
+
while self.command_queue:
|
| 99 |
+
command = self.command_queue.popleft()
|
| 100 |
+
await self._execute_command(command)
|
| 101 |
+
|
| 102 |
+
async def _execute_command(self, command: Command) -> None:
|
| 103 |
+
"""Execute a single command."""
|
| 104 |
+
try:
|
| 105 |
+
if command.command_type == CommandType.CLEAR:
|
| 106 |
+
await self._handle_clear(command)
|
| 107 |
+
elif command.command_type == CommandType.DRAW_RECT:
|
| 108 |
+
await self._handle_draw_rect(command)
|
| 109 |
+
elif command.command_type == CommandType.DRAW_PIXEL:
|
| 110 |
+
await self._handle_draw_pixel(command)
|
| 111 |
+
elif command.command_type == CommandType.DRAW_IMAGE:
|
| 112 |
+
await self._handle_draw_image(command)
|
| 113 |
+
elif command.command_type == CommandType.SET_SHADER:
|
| 114 |
+
await self._handle_set_shader(command)
|
| 115 |
+
elif command.command_type == CommandType.MATRIX_MULTIPLY:
|
| 116 |
+
await self._handle_matrix_multiply(command)
|
| 117 |
+
elif command.command_type == CommandType.VECTOR_OP:
|
| 118 |
+
await self._handle_vector_op(command)
|
| 119 |
+
elif command.command_type == CommandType.CREATE_FRAMEBUFFER:
|
| 120 |
+
await self._handle_create_framebuffer(command)
|
| 121 |
+
elif command.command_type == CommandType.SET_FRAMEBUFFER:
|
| 122 |
+
await self._handle_set_framebuffer(command)
|
| 123 |
+
elif command.command_type == CommandType.LOAD_TEXTURE:
|
| 124 |
+
await self._handle_load_texture(command)
|
| 125 |
+
else:
|
| 126 |
+
print(f"Unknown command type: {command.command_type}")
|
| 127 |
+
self.commands_failed += 1
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
self.commands_processed += 1
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"Error executing command {command.command_id}: {e}")
|
| 134 |
+
self.commands_failed += 1
|
| 135 |
+
|
| 136 |
+
async def _handle_clear(self, command: Command) -> None:
|
| 137 |
+
"""Handle CLEAR command."""
|
| 138 |
+
if self.vgpu and self.current_framebuffer:
|
| 139 |
+
from vgpu import TaskType
|
| 140 |
+
task_id = self.vgpu.submit_task(
|
| 141 |
+
TaskType.RENDER_CLEAR,
|
| 142 |
+
{
|
| 143 |
+
"framebuffer_id": self.current_framebuffer,
|
| 144 |
+
**command.parameters
|
| 145 |
+
}
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
async def _handle_draw_rect(self, command: Command) -> None:
|
| 149 |
+
"""Handle DRAW_RECT command."""
|
| 150 |
+
if self.vgpu and self.current_framebuffer:
|
| 151 |
+
from vgpu import TaskType
|
| 152 |
+
task_id = self.vgpu.submit_task(
|
| 153 |
+
TaskType.RENDER_RECT,
|
| 154 |
+
{
|
| 155 |
+
"framebuffer_id": self.current_framebuffer,
|
| 156 |
+
**command.parameters
|
| 157 |
+
}
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
async def _handle_draw_pixel(self, command: Command) -> None:
|
| 161 |
+
"""Handle DRAW_PIXEL command."""
|
| 162 |
+
if self.vgpu and self.current_framebuffer:
|
| 163 |
+
from vgpu import TaskType
|
| 164 |
+
# Convert single pixel to a 1x1 rectangle
|
| 165 |
+
params = command.parameters.copy()
|
| 166 |
+
params.update({
|
| 167 |
+
"framebuffer_id": self.current_framebuffer,
|
| 168 |
+
"width": 1,
|
| 169 |
+
"height": 1
|
| 170 |
+
})
|
| 171 |
+
task_id = self.vgpu.submit_task(TaskType.RENDER_RECT, params)
|
| 172 |
+
|
| 173 |
+
async def _handle_draw_image(self, command: Command) -> None:
|
| 174 |
+
"""Handle DRAW_IMAGE command."""
|
| 175 |
+
if self.vgpu and self.current_framebuffer:
|
| 176 |
+
from vgpu import TaskType
|
| 177 |
+
task_id = self.vgpu.submit_task(
|
| 178 |
+
TaskType.RENDER_IMAGE,
|
| 179 |
+
{
|
| 180 |
+
"framebuffer_id": self.current_framebuffer,
|
| 181 |
+
**command.parameters
|
| 182 |
+
}
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
async def _handle_set_shader(self, command: Command) -> None:
|
| 186 |
+
"""Handle SET_SHADER command."""
|
| 187 |
+
shader_id = command.parameters.get("shader_id")
|
| 188 |
+
if shader_id:
|
| 189 |
+
self.current_shader = shader_id
|
| 190 |
+
|
| 191 |
+
async def _handle_matrix_multiply(self, command: Command) -> None:
|
| 192 |
+
"""Handle MATRIX_MULTIPLY command."""
|
| 193 |
+
if self.vgpu:
|
| 194 |
+
from vgpu import TaskType
|
| 195 |
+
task_id = self.vgpu.submit_task(
|
| 196 |
+
TaskType.AI_MATRIX_MULTIPLY,
|
| 197 |
+
command.parameters
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
async def _handle_vector_op(self, command: Command) -> None:
|
| 201 |
+
"""Handle VECTOR_OP command."""
|
| 202 |
+
if self.vgpu:
|
| 203 |
+
from vgpu import TaskType
|
| 204 |
+
task_id = self.vgpu.submit_task(
|
| 205 |
+
TaskType.AI_VECTOR_OP,
|
| 206 |
+
command.parameters
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
async def _handle_create_framebuffer(self, command: Command) -> None:
|
| 210 |
+
"""Handle CREATE_FRAMEBUFFER command."""
|
| 211 |
+
if self.vgpu and self.vgpu.vram:
|
| 212 |
+
width = command.parameters.get("width", 800)
|
| 213 |
+
height = command.parameters.get("height", 600)
|
| 214 |
+
channels = command.parameters.get("channels", 3)
|
| 215 |
+
name = command.parameters.get("name")
|
| 216 |
+
|
| 217 |
+
framebuffer_id = self.vgpu.vram.create_framebuffer(width, height, channels, name)
|
| 218 |
+
|
| 219 |
+
# Set as current framebuffer if none is set
|
| 220 |
+
if self.current_framebuffer is None:
|
| 221 |
+
self.current_framebuffer = framebuffer_id
|
| 222 |
+
|
| 223 |
+
async def _handle_set_framebuffer(self, command: Command) -> None:
|
| 224 |
+
"""Handle SET_FRAMEBUFFER command."""
|
| 225 |
+
framebuffer_id = command.parameters.get("framebuffer_id")
|
| 226 |
+
if framebuffer_id and self.vgpu and self.vgpu.vram:
|
| 227 |
+
if self.vgpu.vram.get_framebuffer(framebuffer_id):
|
| 228 |
+
self.current_framebuffer = framebuffer_id
|
| 229 |
+
|
| 230 |
+
async def _handle_load_texture(self, command: Command) -> None:
|
| 231 |
+
"""Handle LOAD_TEXTURE command."""
|
| 232 |
+
if self.vgpu and self.vgpu.vram:
|
| 233 |
+
texture_data = command.parameters.get("texture_data")
|
| 234 |
+
name = command.parameters.get("name")
|
| 235 |
+
|
| 236 |
+
if texture_data is not None:
|
| 237 |
+
texture_id = self.vgpu.vram.load_texture(texture_data, name)
|
| 238 |
+
|
| 239 |
+
def get_current_framebuffer(self) -> Optional[str]:
|
| 240 |
+
"""Get the current active framebuffer ID."""
|
| 241 |
+
return self.current_framebuffer
|
| 242 |
+
|
| 243 |
+
def get_current_shader(self) -> Optional[str]:
|
| 244 |
+
"""Get the current active shader ID."""
|
| 245 |
+
return self.current_shader
|
| 246 |
+
|
| 247 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 248 |
+
"""Get driver statistics."""
|
| 249 |
+
return {
|
| 250 |
+
"commands_in_queue": len(self.command_queue),
|
| 251 |
+
"commands_processed": self.commands_processed,
|
| 252 |
+
"commands_failed": self.commands_failed,
|
| 253 |
+
"current_framebuffer": self.current_framebuffer,
|
| 254 |
+
"current_shader": self.current_shader
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
# Convenience methods for common operations
|
| 258 |
+
def clear_screen(self, color: tuple = (0, 0, 0)) -> str:
|
| 259 |
+
"""Clear the current framebuffer with the specified color."""
|
| 260 |
+
return self.submit_command(CommandType.CLEAR, {"color": color})
|
| 261 |
+
|
| 262 |
+
def draw_rectangle(self, x: int, y: int, width: int, height: int,
|
| 263 |
+
color: tuple = (255, 255, 255)) -> str:
|
| 264 |
+
"""Draw a rectangle on the current framebuffer."""
|
| 265 |
+
return self.submit_command(
|
| 266 |
+
CommandType.DRAW_RECT,
|
| 267 |
+
{"x": x, "y": y, "width": width, "height": height, "color": color}
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
def draw_pixel(self, x: int, y: int, color: tuple = (255, 255, 255)) -> str:
|
| 271 |
+
"""Draw a single pixel on the current framebuffer."""
|
| 272 |
+
return self.submit_command(
|
| 273 |
+
CommandType.DRAW_PIXEL,
|
| 274 |
+
{"x": x, "y": y, "color": color}
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
def create_framebuffer(self, width: int, height: int, channels: int = 3,
|
| 278 |
+
name: Optional[str] = None) -> str:
|
| 279 |
+
"""Create a new framebuffer."""
|
| 280 |
+
return self.submit_command(
|
| 281 |
+
CommandType.CREATE_FRAMEBUFFER,
|
| 282 |
+
{"width": width, "height": height, "channels": channels, "name": name}
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
def set_framebuffer(self, framebuffer_id: str) -> str:
|
| 286 |
+
"""Set the active framebuffer."""
|
| 287 |
+
return self.submit_command(
|
| 288 |
+
CommandType.SET_FRAMEBUFFER,
|
| 289 |
+
{"framebuffer_id": framebuffer_id}
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
if __name__ == "__main__":
|
| 294 |
+
# Test the driver
|
| 295 |
+
async def test_driver():
|
| 296 |
+
driver = GPUDriver()
|
| 297 |
+
|
| 298 |
+
# Submit some test commands
|
| 299 |
+
driver.create_framebuffer(800, 600)
|
| 300 |
+
driver.clear_screen((255, 0, 0))
|
| 301 |
+
driver.draw_rectangle(100, 100, 200, 150, (0, 255, 0))
|
| 302 |
+
driver.draw_pixel(400, 300, (0, 0, 255))
|
| 303 |
+
|
| 304 |
+
print(f"Driver stats: {driver.get_stats()}")
|
| 305 |
+
|
| 306 |
+
# Process commands (without vGPU, they won't actually execute)
|
| 307 |
+
await driver.process_commands()
|
| 308 |
+
|
| 309 |
+
print(f"Driver stats after processing: {driver.get_stats()}")
|
| 310 |
+
|
| 311 |
+
asyncio.run(test_driver())
|
| 312 |
+
|
src/render.py
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Render Module - Software Raster Pipeline
|
| 3 |
+
|
| 4 |
+
This module implements the software raster pipeline for drawing primitives
|
| 5 |
+
and images onto framebuffers stored in VRAM.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
from typing import Tuple, Optional, Any, Dict
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Renderer:
|
| 14 |
+
"""
|
| 15 |
+
Software-based renderer that implements basic drawing operations.
|
| 16 |
+
|
| 17 |
+
This renderer operates on framebuffers stored in VRAM and provides
|
| 18 |
+
functions for drawing primitives like rectangles, lines, and pixels.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def __init__(self, vram=None):
|
| 22 |
+
self.vram = vram
|
| 23 |
+
self.current_shader = None
|
| 24 |
+
|
| 25 |
+
# Rendering statistics
|
| 26 |
+
self.pixels_drawn = 0
|
| 27 |
+
self.draw_calls = 0
|
| 28 |
+
self.render_time = 0.0
|
| 29 |
+
|
| 30 |
+
def set_vram(self, vram):
|
| 31 |
+
"""Set the VRAM reference."""
|
| 32 |
+
self.vram = vram
|
| 33 |
+
|
| 34 |
+
def set_shader(self, shader):
|
| 35 |
+
"""Set the current shader for rendering operations."""
|
| 36 |
+
self.current_shader = shader
|
| 37 |
+
|
| 38 |
+
def clear(self, framebuffer_id: str, color: Tuple[int, int, int] = (0, 0, 0)) -> bool:
|
| 39 |
+
"""Clear a framebuffer with the specified color."""
|
| 40 |
+
if not self.vram:
|
| 41 |
+
return False
|
| 42 |
+
|
| 43 |
+
start_time = time.time()
|
| 44 |
+
|
| 45 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 46 |
+
if not framebuffer:
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
framebuffer.clear(color)
|
| 51 |
+
self.pixels_drawn += framebuffer.width * framebuffer.height
|
| 52 |
+
self.draw_calls += 1
|
| 53 |
+
self.render_time += time.time() - start_time
|
| 54 |
+
return True
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Error clearing framebuffer {framebuffer_id}: {e}")
|
| 57 |
+
return False
|
| 58 |
+
|
| 59 |
+
def draw_pixel(self, framebuffer_id: str, x: int, y: int,
|
| 60 |
+
color: Tuple[int, int, int] = (255, 255, 255)) -> bool:
|
| 61 |
+
"""Draw a single pixel on the framebuffer."""
|
| 62 |
+
if not self.vram:
|
| 63 |
+
return False
|
| 64 |
+
|
| 65 |
+
start_time = time.time()
|
| 66 |
+
|
| 67 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 68 |
+
if not framebuffer:
|
| 69 |
+
return False
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Apply shader if available
|
| 73 |
+
final_color = color
|
| 74 |
+
if self.current_shader:
|
| 75 |
+
final_color = self.current_shader.process_pixel(x, y, color)
|
| 76 |
+
|
| 77 |
+
framebuffer.set_pixel(x, y, final_color)
|
| 78 |
+
self.pixels_drawn += 1
|
| 79 |
+
self.draw_calls += 1
|
| 80 |
+
self.render_time += time.time() - start_time
|
| 81 |
+
return True
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error drawing pixel at ({x}, {y}): {e}")
|
| 84 |
+
return False
|
| 85 |
+
|
| 86 |
+
def draw_rect(self, framebuffer_id: str, x: int, y: int, width: int, height: int,
|
| 87 |
+
color: Tuple[int, int, int] = (255, 255, 255)) -> bool:
|
| 88 |
+
"""Draw a filled rectangle on the framebuffer."""
|
| 89 |
+
if not self.vram:
|
| 90 |
+
return False
|
| 91 |
+
|
| 92 |
+
start_time = time.time()
|
| 93 |
+
|
| 94 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 95 |
+
if not framebuffer:
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
# Clamp rectangle to framebuffer bounds
|
| 100 |
+
x1 = max(0, x)
|
| 101 |
+
y1 = max(0, y)
|
| 102 |
+
x2 = min(framebuffer.width, x + width)
|
| 103 |
+
y2 = min(framebuffer.height, y + height)
|
| 104 |
+
|
| 105 |
+
if x2 <= x1 or y2 <= y1:
|
| 106 |
+
return True # Nothing to draw
|
| 107 |
+
|
| 108 |
+
# Use NumPy for efficient rectangle filling
|
| 109 |
+
if self.current_shader:
|
| 110 |
+
# Apply shader to each pixel (slower but more flexible)
|
| 111 |
+
for py in range(y1, y2):
|
| 112 |
+
for px in range(x1, x2):
|
| 113 |
+
final_color = self.current_shader.process_pixel(px, py, color)
|
| 114 |
+
framebuffer.pixel_buffer[py, px] = final_color[:framebuffer.channels]
|
| 115 |
+
else:
|
| 116 |
+
# Direct fill (faster)
|
| 117 |
+
framebuffer.pixel_buffer[y1:y2, x1:x2] = color[:framebuffer.channels]
|
| 118 |
+
|
| 119 |
+
pixels_affected = (x2 - x1) * (y2 - y1)
|
| 120 |
+
self.pixels_drawn += pixels_affected
|
| 121 |
+
self.draw_calls += 1
|
| 122 |
+
self.render_time += time.time() - start_time
|
| 123 |
+
return True
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"Error drawing rectangle at ({x}, {y}, {width}, {height}): {e}")
|
| 127 |
+
return False
|
| 128 |
+
|
| 129 |
+
def draw_line(self, framebuffer_id: str, x1: int, y1: int, x2: int, y2: int,
|
| 130 |
+
color: Tuple[int, int, int] = (255, 255, 255)) -> bool:
|
| 131 |
+
"""Draw a line using Bresenham's algorithm."""
|
| 132 |
+
if not self.vram:
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
start_time = time.time()
|
| 136 |
+
|
| 137 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 138 |
+
if not framebuffer:
|
| 139 |
+
return False
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
# Bresenham's line algorithm
|
| 143 |
+
dx = abs(x2 - x1)
|
| 144 |
+
dy = abs(y2 - y1)
|
| 145 |
+
sx = 1 if x1 < x2 else -1
|
| 146 |
+
sy = 1 if y1 < y2 else -1
|
| 147 |
+
err = dx - dy
|
| 148 |
+
|
| 149 |
+
x, y = x1, y1
|
| 150 |
+
pixels_drawn = 0
|
| 151 |
+
|
| 152 |
+
while True:
|
| 153 |
+
# Draw pixel if within bounds
|
| 154 |
+
if 0 <= x < framebuffer.width and 0 <= y < framebuffer.height:
|
| 155 |
+
final_color = color
|
| 156 |
+
if self.current_shader:
|
| 157 |
+
final_color = self.current_shader.process_pixel(x, y, color)
|
| 158 |
+
framebuffer.set_pixel(x, y, final_color)
|
| 159 |
+
pixels_drawn += 1
|
| 160 |
+
|
| 161 |
+
if x == x2 and y == y2:
|
| 162 |
+
break
|
| 163 |
+
|
| 164 |
+
e2 = 2 * err
|
| 165 |
+
if e2 > -dy:
|
| 166 |
+
err -= dy
|
| 167 |
+
x += sx
|
| 168 |
+
if e2 < dx:
|
| 169 |
+
err += dx
|
| 170 |
+
y += sy
|
| 171 |
+
|
| 172 |
+
self.pixels_drawn += pixels_drawn
|
| 173 |
+
self.draw_calls += 1
|
| 174 |
+
self.render_time += time.time() - start_time
|
| 175 |
+
return True
|
| 176 |
+
|
| 177 |
+
except Exception as e:
|
| 178 |
+
print(f"Error drawing line from ({x1}, {y1}) to ({x2}, {y2}): {e}")
|
| 179 |
+
return False
|
| 180 |
+
|
| 181 |
+
def draw_circle(self, framebuffer_id: str, center_x: int, center_y: int, radius: int,
|
| 182 |
+
color: Tuple[int, int, int] = (255, 255, 255), filled: bool = False) -> bool:
|
| 183 |
+
"""Draw a circle using the midpoint circle algorithm."""
|
| 184 |
+
if not self.vram:
|
| 185 |
+
return False
|
| 186 |
+
|
| 187 |
+
start_time = time.time()
|
| 188 |
+
|
| 189 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 190 |
+
if not framebuffer:
|
| 191 |
+
return False
|
| 192 |
+
|
| 193 |
+
try:
|
| 194 |
+
pixels_drawn = 0
|
| 195 |
+
|
| 196 |
+
if filled:
|
| 197 |
+
# Draw filled circle
|
| 198 |
+
for y in range(center_y - radius, center_y + radius + 1):
|
| 199 |
+
for x in range(center_x - radius, center_x + radius + 1):
|
| 200 |
+
if (x - center_x) ** 2 + (y - center_y) ** 2 <= radius ** 2:
|
| 201 |
+
if 0 <= x < framebuffer.width and 0 <= y < framebuffer.height:
|
| 202 |
+
final_color = color
|
| 203 |
+
if self.current_shader:
|
| 204 |
+
final_color = self.current_shader.process_pixel(x, y, color)
|
| 205 |
+
framebuffer.set_pixel(x, y, final_color)
|
| 206 |
+
pixels_drawn += 1
|
| 207 |
+
else:
|
| 208 |
+
# Draw circle outline using midpoint algorithm
|
| 209 |
+
x = 0
|
| 210 |
+
y = radius
|
| 211 |
+
d = 1 - radius
|
| 212 |
+
|
| 213 |
+
def draw_circle_points(cx, cy, x, y):
|
| 214 |
+
points = [
|
| 215 |
+
(cx + x, cy + y), (cx - x, cy + y),
|
| 216 |
+
(cx + x, cy - y), (cx - x, cy - y),
|
| 217 |
+
(cx + y, cy + x), (cx - y, cy + x),
|
| 218 |
+
(cx + y, cy - x), (cx - y, cy - x)
|
| 219 |
+
]
|
| 220 |
+
drawn = 0
|
| 221 |
+
for px, py in points:
|
| 222 |
+
if 0 <= px < framebuffer.width and 0 <= py < framebuffer.height:
|
| 223 |
+
final_color = color
|
| 224 |
+
if self.current_shader:
|
| 225 |
+
final_color = self.current_shader.process_pixel(px, py, color)
|
| 226 |
+
framebuffer.set_pixel(px, py, final_color)
|
| 227 |
+
drawn += 1
|
| 228 |
+
return drawn
|
| 229 |
+
|
| 230 |
+
pixels_drawn += draw_circle_points(center_x, center_y, x, y)
|
| 231 |
+
|
| 232 |
+
while x < y:
|
| 233 |
+
if d < 0:
|
| 234 |
+
d += 2 * x + 3
|
| 235 |
+
else:
|
| 236 |
+
d += 2 * (x - y) + 5
|
| 237 |
+
y -= 1
|
| 238 |
+
x += 1
|
| 239 |
+
pixels_drawn += draw_circle_points(center_x, center_y, x, y)
|
| 240 |
+
|
| 241 |
+
self.pixels_drawn += pixels_drawn
|
| 242 |
+
self.draw_calls += 1
|
| 243 |
+
self.render_time += time.time() - start_time
|
| 244 |
+
return True
|
| 245 |
+
|
| 246 |
+
except Exception as e:
|
| 247 |
+
print(f"Error drawing circle at ({center_x}, {center_y}) with radius {radius}: {e}")
|
| 248 |
+
return False
|
| 249 |
+
|
| 250 |
+
def draw_image(self, framebuffer_id: str, x: int, y: int, texture_id: str,
|
| 251 |
+
scale_x: float = 1.0, scale_y: float = 1.0) -> bool:
|
| 252 |
+
"""Draw an image/texture onto the framebuffer."""
|
| 253 |
+
if not self.vram:
|
| 254 |
+
return False
|
| 255 |
+
|
| 256 |
+
start_time = time.time()
|
| 257 |
+
|
| 258 |
+
framebuffer = self.vram.get_framebuffer(framebuffer_id)
|
| 259 |
+
texture = self.vram.get_texture(texture_id)
|
| 260 |
+
|
| 261 |
+
if not framebuffer or texture is None:
|
| 262 |
+
return False
|
| 263 |
+
|
| 264 |
+
try:
|
| 265 |
+
# Get texture dimensions
|
| 266 |
+
if len(texture.shape) == 3:
|
| 267 |
+
tex_height, tex_width, tex_channels = texture.shape
|
| 268 |
+
else:
|
| 269 |
+
tex_height, tex_width = texture.shape
|
| 270 |
+
tex_channels = 1
|
| 271 |
+
|
| 272 |
+
# Calculate scaled dimensions
|
| 273 |
+
scaled_width = int(tex_width * scale_x)
|
| 274 |
+
scaled_height = int(tex_height * scale_y)
|
| 275 |
+
|
| 276 |
+
pixels_drawn = 0
|
| 277 |
+
|
| 278 |
+
# Simple nearest-neighbor scaling and blitting
|
| 279 |
+
for dy in range(scaled_height):
|
| 280 |
+
for dx in range(scaled_width):
|
| 281 |
+
# Calculate destination pixel
|
| 282 |
+
dest_x = x + dx
|
| 283 |
+
dest_y = y + dy
|
| 284 |
+
|
| 285 |
+
# Check bounds
|
| 286 |
+
if (dest_x < 0 or dest_x >= framebuffer.width or
|
| 287 |
+
dest_y < 0 or dest_y >= framebuffer.height):
|
| 288 |
+
continue
|
| 289 |
+
|
| 290 |
+
# Calculate source pixel (nearest neighbor)
|
| 291 |
+
src_x = int(dx / scale_x)
|
| 292 |
+
src_y = int(dy / scale_y)
|
| 293 |
+
|
| 294 |
+
# Clamp source coordinates
|
| 295 |
+
src_x = min(src_x, tex_width - 1)
|
| 296 |
+
src_y = min(src_y, tex_height - 1)
|
| 297 |
+
|
| 298 |
+
# Get source pixel color
|
| 299 |
+
if tex_channels == 1:
|
| 300 |
+
color = (texture[src_y, src_x], texture[src_y, src_x], texture[src_y, src_x])
|
| 301 |
+
else:
|
| 302 |
+
color = tuple(texture[src_y, src_x, :min(3, tex_channels)])
|
| 303 |
+
|
| 304 |
+
# Apply shader if available
|
| 305 |
+
final_color = color
|
| 306 |
+
if self.current_shader:
|
| 307 |
+
final_color = self.current_shader.process_pixel(dest_x, dest_y, color)
|
| 308 |
+
|
| 309 |
+
# Set pixel
|
| 310 |
+
framebuffer.set_pixel(dest_x, dest_y, final_color)
|
| 311 |
+
pixels_drawn += 1
|
| 312 |
+
|
| 313 |
+
self.pixels_drawn += pixels_drawn
|
| 314 |
+
self.draw_calls += 1
|
| 315 |
+
self.render_time += time.time() - start_time
|
| 316 |
+
return True
|
| 317 |
+
|
| 318 |
+
except Exception as e:
|
| 319 |
+
print(f"Error drawing image {texture_id} at ({x}, {y}): {e}")
|
| 320 |
+
return False
|
| 321 |
+
|
| 322 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 323 |
+
"""Get rendering statistics."""
|
| 324 |
+
return {
|
| 325 |
+
"pixels_drawn": self.pixels_drawn,
|
| 326 |
+
"draw_calls": self.draw_calls,
|
| 327 |
+
"total_render_time": self.render_time,
|
| 328 |
+
"avg_render_time": self.render_time / max(1, self.draw_calls),
|
| 329 |
+
"pixels_per_second": self.pixels_drawn / max(0.001, self.render_time)
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
def reset_stats(self) -> None:
|
| 333 |
+
"""Reset rendering statistics."""
|
| 334 |
+
self.pixels_drawn = 0
|
| 335 |
+
self.draw_calls = 0
|
| 336 |
+
self.render_time = 0.0
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
if __name__ == "__main__":
|
| 340 |
+
# Test the renderer
|
| 341 |
+
from vram import VRAM
|
| 342 |
+
|
| 343 |
+
# Create VRAM and renderer
|
| 344 |
+
vram = VRAM(memory_size_gb=1)
|
| 345 |
+
renderer = Renderer(vram)
|
| 346 |
+
|
| 347 |
+
# Create a test framebuffer
|
| 348 |
+
fb_id = vram.create_framebuffer(800, 600, 3)
|
| 349 |
+
|
| 350 |
+
# Test rendering operations
|
| 351 |
+
print("Testing renderer...")
|
| 352 |
+
|
| 353 |
+
# Clear screen
|
| 354 |
+
renderer.clear(fb_id, (64, 128, 255))
|
| 355 |
+
|
| 356 |
+
# Draw some rectangles
|
| 357 |
+
renderer.draw_rect(fb_id, 100, 100, 200, 150, (255, 0, 0))
|
| 358 |
+
renderer.draw_rect(fb_id, 200, 200, 100, 100, (0, 255, 0))
|
| 359 |
+
|
| 360 |
+
# Draw some lines
|
| 361 |
+
renderer.draw_line(fb_id, 0, 0, 799, 599, (255, 255, 255))
|
| 362 |
+
renderer.draw_line(fb_id, 799, 0, 0, 599, (255, 255, 255))
|
| 363 |
+
|
| 364 |
+
# Draw a circle
|
| 365 |
+
renderer.draw_circle(fb_id, 400, 300, 50, (255, 255, 0), filled=True)
|
| 366 |
+
|
| 367 |
+
# Draw some pixels
|
| 368 |
+
for i in range(100):
|
| 369 |
+
renderer.draw_pixel(fb_id, 50 + i, 50, (255, 0, 255))
|
| 370 |
+
|
| 371 |
+
# Print statistics
|
| 372 |
+
stats = renderer.get_stats()
|
| 373 |
+
print(f"Renderer stats: {stats}")
|
| 374 |
+
|
| 375 |
+
# Get framebuffer and check a pixel
|
| 376 |
+
fb = vram.get_framebuffer(fb_id)
|
| 377 |
+
if fb:
|
| 378 |
+
pixel = fb.get_pixel(100, 100)
|
| 379 |
+
print(f"Pixel at (100, 100): {pixel}")
|
| 380 |
+
|
| 381 |
+
print("Renderer test completed!")
|
| 382 |
+
|
src/vgpu.py
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
vGPU Core Processor Module
|
| 3 |
+
|
| 4 |
+
This module implements the central orchestrator of the virtual GPU, managing
|
| 5 |
+
workload distribution across 800 SMs and 50,000 cores, and coordinating
|
| 6 |
+
operations between all other modules.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import asyncio
|
| 10 |
+
import time
|
| 11 |
+
from collections import deque
|
| 12 |
+
from enum import Enum
|
| 13 |
+
from typing import Dict, List, Optional, Any
|
| 14 |
+
from dataclasses import dataclass
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TaskType(Enum):
|
| 18 |
+
"""Enumeration of task types that can be processed by the vGPU."""
|
| 19 |
+
RENDER_PIXEL_BLOCK = "render_pixel_block"
|
| 20 |
+
RENDER_CLEAR = "render_clear"
|
| 21 |
+
RENDER_RECT = "render_rect"
|
| 22 |
+
RENDER_IMAGE = "render_image"
|
| 23 |
+
AI_MATRIX_MULTIPLY = "ai_matrix_multiply"
|
| 24 |
+
AI_VECTOR_OP = "ai_vector_op"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TaskStatus(Enum):
|
| 28 |
+
"""Enumeration of task statuses."""
|
| 29 |
+
PENDING = "pending"
|
| 30 |
+
IN_PROGRESS = "in_progress"
|
| 31 |
+
COMPLETED = "completed"
|
| 32 |
+
FAILED = "failed"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dataclass
|
| 36 |
+
class Task:
|
| 37 |
+
"""Represents a single task to be processed by the vGPU."""
|
| 38 |
+
task_id: str
|
| 39 |
+
task_type: TaskType
|
| 40 |
+
payload: Dict[str, Any]
|
| 41 |
+
sm_id: Optional[int] = None
|
| 42 |
+
status: TaskStatus = TaskStatus.PENDING
|
| 43 |
+
created_time: float = 0.0
|
| 44 |
+
start_time: float = 0.0
|
| 45 |
+
end_time: float = 0.0
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class StreamingMultiprocessor:
|
| 49 |
+
"""Represents a single Streaming Multiprocessor (SM) in the vGPU."""
|
| 50 |
+
|
| 51 |
+
def __init__(self, sm_id: int, cores_per_sm: int = 62):
|
| 52 |
+
self.sm_id = sm_id
|
| 53 |
+
self.cores_per_sm = cores_per_sm
|
| 54 |
+
self.task_queue = deque()
|
| 55 |
+
self.current_task: Optional[Task] = None
|
| 56 |
+
self.is_busy = False
|
| 57 |
+
self.total_tasks_processed = 0
|
| 58 |
+
|
| 59 |
+
def add_task(self, task: Task) -> None:
|
| 60 |
+
"""Add a task to this SM's queue."""
|
| 61 |
+
task.sm_id = self.sm_id
|
| 62 |
+
self.task_queue.append(task)
|
| 63 |
+
|
| 64 |
+
def get_next_task(self) -> Optional[Task]:
|
| 65 |
+
"""Get the next task from the queue."""
|
| 66 |
+
if self.task_queue and not self.is_busy:
|
| 67 |
+
task = self.task_queue.popleft()
|
| 68 |
+
self.current_task = task
|
| 69 |
+
self.is_busy = True
|
| 70 |
+
task.status = TaskStatus.IN_PROGRESS
|
| 71 |
+
task.start_time = time.time()
|
| 72 |
+
return task
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
def complete_task(self) -> Optional[Task]:
|
| 76 |
+
"""Mark the current task as completed."""
|
| 77 |
+
if self.current_task:
|
| 78 |
+
self.current_task.status = TaskStatus.COMPLETED
|
| 79 |
+
self.current_task.end_time = time.time()
|
| 80 |
+
completed_task = self.current_task
|
| 81 |
+
self.current_task = None
|
| 82 |
+
self.is_busy = False
|
| 83 |
+
self.total_tasks_processed += 1
|
| 84 |
+
return completed_task
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
def get_queue_length(self) -> int:
|
| 88 |
+
"""Get the current queue length."""
|
| 89 |
+
return len(self.task_queue)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class VirtualGPU:
|
| 93 |
+
"""
|
| 94 |
+
The main Virtual GPU class that orchestrates all operations.
|
| 95 |
+
|
| 96 |
+
This class manages 800 SMs with a total of 50,000 cores, handles task
|
| 97 |
+
distribution, and coordinates with other modules like VRAM, renderer, and AI.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
def __init__(self, num_sms: int = 800, total_cores: int = 50000):
|
| 101 |
+
self.num_sms = num_sms
|
| 102 |
+
self.total_cores = total_cores
|
| 103 |
+
self.cores_per_sm = total_cores // num_sms
|
| 104 |
+
|
| 105 |
+
# Initialize Streaming Multiprocessors
|
| 106 |
+
self.sms: List[StreamingMultiprocessor] = []
|
| 107 |
+
for i in range(num_sms):
|
| 108 |
+
# Distribute cores evenly, with some SMs getting an extra core if needed
|
| 109 |
+
cores_for_this_sm = self.cores_per_sm
|
| 110 |
+
if i < (total_cores % num_sms):
|
| 111 |
+
cores_for_this_sm += 1
|
| 112 |
+
self.sms.append(StreamingMultiprocessor(i, cores_for_this_sm))
|
| 113 |
+
|
| 114 |
+
# Global task management
|
| 115 |
+
self.pending_tasks = deque()
|
| 116 |
+
self.completed_tasks = deque()
|
| 117 |
+
self.task_counter = 0
|
| 118 |
+
|
| 119 |
+
# GPU state
|
| 120 |
+
self.is_running = False
|
| 121 |
+
self.clock_cycle = 0
|
| 122 |
+
self.tick_rate = 60 # Hz
|
| 123 |
+
|
| 124 |
+
# Module references (to be set by external initialization)
|
| 125 |
+
self.vram = None
|
| 126 |
+
self.renderer = None
|
| 127 |
+
self.ai_accelerator = None
|
| 128 |
+
self.driver = None
|
| 129 |
+
|
| 130 |
+
def set_modules(self, vram, renderer, ai_accelerator, driver):
|
| 131 |
+
"""Set references to other vGPU modules."""
|
| 132 |
+
self.vram = vram
|
| 133 |
+
self.renderer = renderer
|
| 134 |
+
self.ai_accelerator = ai_accelerator
|
| 135 |
+
self.driver = driver
|
| 136 |
+
|
| 137 |
+
def submit_task(self, task_type: TaskType, payload: Dict[str, Any]) -> str:
|
| 138 |
+
"""Submit a new task to the vGPU."""
|
| 139 |
+
task_id = f"task_{self.task_counter}"
|
| 140 |
+
self.task_counter += 1
|
| 141 |
+
|
| 142 |
+
task = Task(
|
| 143 |
+
task_id=task_id,
|
| 144 |
+
task_type=task_type,
|
| 145 |
+
payload=payload,
|
| 146 |
+
created_time=time.time()
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
self.pending_tasks.append(task)
|
| 150 |
+
return task_id
|
| 151 |
+
|
| 152 |
+
def distribute_tasks(self) -> None:
|
| 153 |
+
"""Distribute pending tasks to available SMs using round-robin."""
|
| 154 |
+
sm_index = 0
|
| 155 |
+
max_queue_length = 10 # Prevent any SM from being overloaded
|
| 156 |
+
|
| 157 |
+
while self.pending_tasks:
|
| 158 |
+
# Find an SM that's not overloaded
|
| 159 |
+
attempts = 0
|
| 160 |
+
while attempts < self.num_sms:
|
| 161 |
+
current_sm = self.sms[sm_index]
|
| 162 |
+
if current_sm.get_queue_length() < max_queue_length:
|
| 163 |
+
task = self.pending_tasks.popleft()
|
| 164 |
+
current_sm.add_task(task)
|
| 165 |
+
break
|
| 166 |
+
sm_index = (sm_index + 1) % self.num_sms
|
| 167 |
+
attempts += 1
|
| 168 |
+
|
| 169 |
+
if attempts >= self.num_sms:
|
| 170 |
+
# All SMs are overloaded, break to avoid infinite loop
|
| 171 |
+
break
|
| 172 |
+
|
| 173 |
+
sm_index = (sm_index + 1) % self.num_sms
|
| 174 |
+
|
| 175 |
+
def process_sm_tasks(self) -> None:
|
| 176 |
+
"""Process tasks on all SMs."""
|
| 177 |
+
for sm in self.sms:
|
| 178 |
+
# Start a new task if the SM is idle
|
| 179 |
+
if not sm.is_busy:
|
| 180 |
+
task = sm.get_next_task()
|
| 181 |
+
if task:
|
| 182 |
+
# Task will be processed in the next step
|
| 183 |
+
pass
|
| 184 |
+
|
| 185 |
+
# Process the current task (simulate work completion)
|
| 186 |
+
if sm.current_task:
|
| 187 |
+
# Simulate task processing by calling appropriate module
|
| 188 |
+
self._execute_task(sm.current_task)
|
| 189 |
+
completed_task = sm.complete_task()
|
| 190 |
+
if completed_task:
|
| 191 |
+
self.completed_tasks.append(completed_task)
|
| 192 |
+
|
| 193 |
+
def _execute_task(self, task: Task) -> None:
|
| 194 |
+
"""Execute a specific task by calling the appropriate module."""
|
| 195 |
+
try:
|
| 196 |
+
if task.task_type == TaskType.RENDER_CLEAR and self.renderer:
|
| 197 |
+
self.renderer.clear(**task.payload)
|
| 198 |
+
elif task.task_type == TaskType.RENDER_RECT and self.renderer:
|
| 199 |
+
self.renderer.draw_rect(**task.payload)
|
| 200 |
+
elif task.task_type == TaskType.RENDER_IMAGE and self.renderer:
|
| 201 |
+
self.renderer.draw_image(**task.payload)
|
| 202 |
+
elif task.task_type == TaskType.AI_MATRIX_MULTIPLY and self.ai_accelerator:
|
| 203 |
+
self.ai_accelerator.matrix_multiply(**task.payload)
|
| 204 |
+
elif task.task_type == TaskType.AI_VECTOR_OP and self.ai_accelerator:
|
| 205 |
+
self.ai_accelerator.vector_operation(**task.payload)
|
| 206 |
+
else:
|
| 207 |
+
print(f"Unknown task type: {task.task_type}")
|
| 208 |
+
task.status = TaskStatus.FAILED
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"Error executing task {task.task_id}: {e}")
|
| 211 |
+
task.status = TaskStatus.FAILED
|
| 212 |
+
|
| 213 |
+
async def tick(self) -> None:
|
| 214 |
+
"""Main GPU tick cycle."""
|
| 215 |
+
self.clock_cycle += 1
|
| 216 |
+
|
| 217 |
+
# 1. Distribute pending tasks to SMs
|
| 218 |
+
self.distribute_tasks()
|
| 219 |
+
|
| 220 |
+
# 2. Process tasks on all SMs
|
| 221 |
+
self.process_sm_tasks()
|
| 222 |
+
|
| 223 |
+
# 3. Handle any driver commands
|
| 224 |
+
if self.driver:
|
| 225 |
+
await self.driver.process_commands()
|
| 226 |
+
|
| 227 |
+
async def run(self) -> None:
|
| 228 |
+
"""Main GPU execution loop."""
|
| 229 |
+
self.is_running = True
|
| 230 |
+
tick_interval = 1.0 / self.tick_rate
|
| 231 |
+
|
| 232 |
+
print(f"Starting vGPU with {self.num_sms} SMs and {self.total_cores} cores")
|
| 233 |
+
print(f"Tick rate: {self.tick_rate} Hz")
|
| 234 |
+
|
| 235 |
+
while self.is_running:
|
| 236 |
+
start_time = time.time()
|
| 237 |
+
|
| 238 |
+
await self.tick()
|
| 239 |
+
|
| 240 |
+
# Maintain consistent tick rate
|
| 241 |
+
elapsed = time.time() - start_time
|
| 242 |
+
if elapsed < tick_interval:
|
| 243 |
+
await asyncio.sleep(tick_interval - elapsed)
|
| 244 |
+
|
| 245 |
+
def stop(self) -> None:
|
| 246 |
+
"""Stop the GPU execution."""
|
| 247 |
+
self.is_running = False
|
| 248 |
+
|
| 249 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 250 |
+
"""Get current GPU statistics."""
|
| 251 |
+
total_tasks_processed = sum(sm.total_tasks_processed for sm in self.sms)
|
| 252 |
+
total_queue_length = sum(sm.get_queue_length() for sm in self.sms)
|
| 253 |
+
busy_sms = sum(1 for sm in self.sms if sm.is_busy)
|
| 254 |
+
|
| 255 |
+
return {
|
| 256 |
+
"clock_cycle": self.clock_cycle,
|
| 257 |
+
"total_sms": self.num_sms,
|
| 258 |
+
"total_cores": self.total_cores,
|
| 259 |
+
"busy_sms": busy_sms,
|
| 260 |
+
"total_tasks_processed": total_tasks_processed,
|
| 261 |
+
"pending_tasks": len(self.pending_tasks),
|
| 262 |
+
"total_queue_length": total_queue_length,
|
| 263 |
+
"completed_tasks": len(self.completed_tasks)
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
if __name__ == "__main__":
|
| 268 |
+
# Basic test of the vGPU
|
| 269 |
+
async def test_vgpu():
|
| 270 |
+
vgpu = VirtualGPU()
|
| 271 |
+
|
| 272 |
+
# Submit some test tasks
|
| 273 |
+
vgpu.submit_task(TaskType.RENDER_CLEAR, {"color": (255, 0, 0)})
|
| 274 |
+
vgpu.submit_task(TaskType.RENDER_RECT, {"x": 10, "y": 10, "width": 100, "height": 50, "color": (0, 255, 0)})
|
| 275 |
+
|
| 276 |
+
# Run a few ticks
|
| 277 |
+
for _ in range(5):
|
| 278 |
+
await vgpu.tick()
|
| 279 |
+
print(f"Stats: {vgpu.get_stats()}")
|
| 280 |
+
await asyncio.sleep(0.1)
|
| 281 |
+
|
| 282 |
+
asyncio.run(test_vgpu())
|
| 283 |
+
|
src/vram.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
from typing import Dict, Any, Optional, Tuple, Union
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@dataclass
|
| 9 |
+
class MemoryBlock:
|
| 10 |
+
"""Represents a block of memory in the symbolic VRAM."""
|
| 11 |
+
address: int
|
| 12 |
+
size: int
|
| 13 |
+
data: Optional[Any]
|
| 14 |
+
allocated_time: float
|
| 15 |
+
last_accessed: float
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Framebuffer:
|
| 19 |
+
"""Represents a 2D drawing surface in VRAM."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, width: int, height: int, channels: int = 3, dtype=np.uint8):
|
| 22 |
+
self.width = width
|
| 23 |
+
self.height = height
|
| 24 |
+
self.channels = channels
|
| 25 |
+
self.dtype = dtype
|
| 26 |
+
|
| 27 |
+
# Create the pixel buffer symbolically to avoid large allocations
|
| 28 |
+
# The actual pixel data will be managed by the MemoryManager
|
| 29 |
+
self.pixel_buffer_address: Optional[int] = None
|
| 30 |
+
self.pixel_buffer_size: int = width * height * channels * np.dtype(dtype).itemsize
|
| 31 |
+
self.pixel_buffer = np.zeros((height, width, channels), dtype=dtype)
|
| 32 |
+
self.vram_address: Optional[int] = None # This is the address in the MemoryManager
|
| 33 |
+
|
| 34 |
+
def resize(self, new_width: int, new_height: int) -> None:
|
| 35 |
+
# No actual data to resize, just update symbolic size
|
| 36 |
+
self.width = new_width
|
| 37 |
+
self.height = new_height
|
| 38 |
+
self.pixel_buffer_size = new_width * new_height * self.channels * np.dtype(self.dtype).itemsize
|
| 39 |
+
|
| 40 |
+
def clear(self, color: Tuple[int, int, int]) -> None:
|
| 41 |
+
self.pixel_buffer[:, :] = color
|
| 42 |
+
|
| 43 |
+
def get_pixel(self, x: int, y: int) -> np.ndarray:
|
| 44 |
+
if 0 <= x < self.width and 0 <= y < self.height:
|
| 45 |
+
return self.pixel_buffer[y, x]
|
| 46 |
+
return np.zeros(self.channels, dtype=self.dtype)
|
| 47 |
+
def set_pixel(self, x: int, y: int, color: Tuple[int, int, int]) -> None:
|
| 48 |
+
if 0 <= x < self.width and 0 <= y < self.height:
|
| 49 |
+
self.pixel_buffer[y, x] = color[:self.channels]
|
| 50 |
+
|
| 51 |
+
def get_memory_usage(self) -> int:
|
| 52 |
+
"""Get the memory usage of this framebuffer in bytes."""
|
| 53 |
+
return self.pixel_buffer_size
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class MemoryManager:
|
| 57 |
+
"""Manages the symbolic 500GB GDDR7 memory space."""
|
| 58 |
+
|
| 59 |
+
def __init__(self, total_memory_gb: int = 500, block_size_kb: int = 4):
|
| 60 |
+
self.total_memory_bytes = total_memory_gb * 1024 * 1024 * 1024 # 500GB
|
| 61 |
+
self.block_size_bytes = block_size_kb * 1024 # 4KB blocks
|
| 62 |
+
self.total_blocks = self.total_memory_bytes // self.block_size_bytes
|
| 63 |
+
|
| 64 |
+
# Symbolic memory space - only allocated blocks are stored
|
| 65 |
+
self.memory_blocks: Dict[int, MemoryBlock] = {}
|
| 66 |
+
|
| 67 |
+
# Free block tracking - use a list of free block ranges instead of a set of all blocks
|
| 68 |
+
self.free_block_ranges = [(0, self.total_blocks - 1)] # (start_block_id, end_block_id)
|
| 69 |
+
self.allocated_blocks = set() # Still track allocated blocks for quick lookup
|
| 70 |
+
|
| 71 |
+
# Address allocation counter
|
| 72 |
+
self.next_address = 0
|
| 73 |
+
|
| 74 |
+
def allocate_block(self, size_bytes: int) -> Optional[int]:
|
| 75 |
+
"""Allocate a block of memory and return its address."""
|
| 76 |
+
blocks_needed = (size_bytes + self.block_size_bytes - 1) // self.block_size_bytes
|
| 77 |
+
|
| 78 |
+
# Find a suitable contiguous block range
|
| 79 |
+
for i, (start, end) in enumerate(self.free_block_ranges):
|
| 80 |
+
available_blocks = end - start + 1
|
| 81 |
+
if available_blocks >= blocks_needed:
|
| 82 |
+
# Found a suitable range
|
| 83 |
+
base_block_id = start
|
| 84 |
+
|
| 85 |
+
# Update free_block_ranges
|
| 86 |
+
new_start = start + blocks_needed
|
| 87 |
+
if new_start <= end:
|
| 88 |
+
self.free_block_ranges[i] = (new_start, end)
|
| 89 |
+
else:
|
| 90 |
+
self.free_block_ranges.pop(i)
|
| 91 |
+
|
| 92 |
+
# Add to allocated_blocks
|
| 93 |
+
for j in range(blocks_needed):
|
| 94 |
+
self.allocated_blocks.add(base_block_id + j)
|
| 95 |
+
|
| 96 |
+
# Create memory block
|
| 97 |
+
base_address = base_block_id * self.block_size_bytes
|
| 98 |
+
|
| 99 |
+
memory_block = MemoryBlock(
|
| 100 |
+
address=base_address,
|
| 101 |
+
size=size_bytes,
|
| 102 |
+
data=bytearray(size_bytes), # Allocate actual bytearray for data
|
| 103 |
+
allocated_time=time.time(),
|
| 104 |
+
last_accessed=time.time()
|
| 105 |
+
)
|
| 106 |
+
self.memory_blocks[base_address] = memory_block
|
| 107 |
+
return base_address
|
| 108 |
+
|
| 109 |
+
return None # Out of memory
|
| 110 |
+
|
| 111 |
+
def deallocate_block(self, address: int) -> bool:
|
| 112 |
+
"""Deallocate a block of memory."""
|
| 113 |
+
if address in self.memory_blocks:
|
| 114 |
+
memory_block = self.memory_blocks[address]
|
| 115 |
+
blocks_to_free = (memory_block.size + self.block_size_bytes - 1) // self.block_size_bytes
|
| 116 |
+
|
| 117 |
+
base_block_id = address // self.block_size_bytes
|
| 118 |
+
for i in range(blocks_to_free):
|
| 119 |
+
block_id = base_block_id + i
|
| 120 |
+
if block_id in self.allocated_blocks:
|
| 121 |
+
self.allocated_blocks.remove(block_id)
|
| 122 |
+
# Add back to free_block_ranges (simple merge for now)
|
| 123 |
+
self.free_block_ranges.append((block_id, block_id))
|
| 124 |
+
self.free_block_ranges.sort() # Keep sorted for efficient merging
|
| 125 |
+
|
| 126 |
+
del self.memory_blocks[address]
|
| 127 |
+
return True
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
def read_data(self, address: int, size: int) -> Optional[np.ndarray]:
|
| 131 |
+
"""Read data from memory."""
|
| 132 |
+
if address in self.memory_blocks:
|
| 133 |
+
memory_block = self.memory_blocks[address]
|
| 134 |
+
if memory_block.data is not None and size <= memory_block.size:
|
| 135 |
+
return np.frombuffer(memory_block.data[:size], dtype=np.uint8) # Return as numpy array
|
| 136 |
+
return None
|
| 137 |
+
|
| 138 |
+
def write_data(self, address: int, data: Union[np.ndarray, bytes]) -> bool:
|
| 139 |
+
"""Write data to memory."""
|
| 140 |
+
if address in self.memory_blocks:
|
| 141 |
+
memory_block = self.memory_blocks[address]
|
| 142 |
+
if memory_block.data is not None:
|
| 143 |
+
if isinstance(data, np.ndarray):
|
| 144 |
+
data_bytes = data.tobytes()
|
| 145 |
+
elif isinstance(data, bytes):
|
| 146 |
+
data_bytes = data
|
| 147 |
+
else:
|
| 148 |
+
raise TypeError("Data must be a NumPy array or bytes.")
|
| 149 |
+
|
| 150 |
+
if len(data_bytes) <= memory_block.size:
|
| 151 |
+
memory_block.data[:len(data_bytes)] = data_bytes
|
| 152 |
+
return True
|
| 153 |
+
return False
|
| 154 |
+
|
| 155 |
+
def get_memory_stats(self) -> Dict[str, Any]:
|
| 156 |
+
"""Get memory usage statistics."""
|
| 157 |
+
allocated_bytes = sum(block.size for block in self.memory_blocks.values())
|
| 158 |
+
free_bytes = self.total_memory_bytes - allocated_bytes
|
| 159 |
+
|
| 160 |
+
return {
|
| 161 |
+
"total_memory_gb": self.total_memory_bytes / (1024**3),
|
| 162 |
+
"allocated_bytes": allocated_bytes,
|
| 163 |
+
"free_bytes": free_bytes,
|
| 164 |
+
"allocated_blocks_count": len(self.allocated_blocks),
|
| 165 |
+
"free_block_ranges_count": len(self.free_block_ranges),
|
| 166 |
+
"utilization_percent": (allocated_bytes / self.total_memory_bytes) * 100 if self.total_memory_bytes > 0 else 0
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
class VRAM:
|
| 171 |
+
"""
|
| 172 |
+
Main VRAM class that provides the interface for the 500GB GDDR7 memory.
|
| 173 |
+
|
| 174 |
+
This class combines the MemoryManager for low-level memory operations
|
| 175 |
+
with higher-level abstractions like Framebuffers.
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
def __init__(self, memory_size_gb: int = 500):
|
| 179 |
+
self.memory_manager = MemoryManager(memory_size_gb)
|
| 180 |
+
|
| 181 |
+
# Cache for frequently accessed data (simulates L1/L2 cache)
|
| 182 |
+
self.cache_size = 1000 # Number of cache entries
|
| 183 |
+
self.cache = OrderedDict()
|
| 184 |
+
|
| 185 |
+
# Framebuffer registry
|
| 186 |
+
self.framebuffers: Dict[str, Framebuffer] = {}
|
| 187 |
+
self.framebuffer_counter = 0
|
| 188 |
+
|
| 189 |
+
# Texture registry
|
| 190 |
+
self.textures: Dict[str, np.ndarray] = {}
|
| 191 |
+
self.texture_counter = 0
|
| 192 |
+
|
| 193 |
+
def create_framebuffer(self, width: int, height: int, channels: int = 3,
|
| 194 |
+
name: Optional[str] = None) -> str:
|
| 195 |
+
"""Create a new framebuffer and return its ID."""
|
| 196 |
+
if name is None:
|
| 197 |
+
name = f"framebuffer_{self.framebuffer_counter}"
|
| 198 |
+
self.framebuffer_counter += 1
|
| 199 |
+
|
| 200 |
+
framebuffer = Framebuffer(width, height, channels)
|
| 201 |
+
|
| 202 |
+
# Allocate memory for the framebuffer
|
| 203 |
+
memory_size = framebuffer.get_memory_usage()
|
| 204 |
+
address = self.memory_manager.allocate_block(memory_size)
|
| 205 |
+
|
| 206 |
+
if address is not None:
|
| 207 |
+
framebuffer.vram_address = address
|
| 208 |
+
self.framebuffers[name] = framebuffer
|
| 209 |
+
return name
|
| 210 |
+
else:
|
| 211 |
+
raise MemoryError("Failed to allocate memory for framebuffer")
|
| 212 |
+
|
| 213 |
+
def get_framebuffer(self, name: str) -> Optional[Framebuffer]:
|
| 214 |
+
"""Get a framebuffer by name."""
|
| 215 |
+
return self.framebuffers.get(name)
|
| 216 |
+
|
| 217 |
+
def delete_framebuffer(self, name: str) -> bool:
|
| 218 |
+
"""Delete a framebuffer and free its memory."""
|
| 219 |
+
if name in self.framebuffers:
|
| 220 |
+
framebuffer = self.framebuffers[name]
|
| 221 |
+
if framebuffer.vram_address is not None:
|
| 222 |
+
self.memory_manager.deallocate_block(framebuffer.vram_address)
|
| 223 |
+
del self.framebuffers[name]
|
| 224 |
+
return True
|
| 225 |
+
return False
|
| 226 |
+
|
| 227 |
+
def load_texture(self, texture_data: Union[np.ndarray, bytes], name: Optional[str] = None) -> str:
|
| 228 |
+
"""Load texture data into VRAM and return its ID."""
|
| 229 |
+
if name is None:
|
| 230 |
+
name = f"texture_{self.texture_counter}"
|
| 231 |
+
self.texture_counter += 1
|
| 232 |
+
|
| 233 |
+
size_bytes = 0
|
| 234 |
+
if isinstance(texture_data, np.ndarray):
|
| 235 |
+
size_bytes = texture_data.nbytes
|
| 236 |
+
elif isinstance(texture_data, bytes):
|
| 237 |
+
size_bytes = len(texture_data)
|
| 238 |
+
else:
|
| 239 |
+
raise TypeError("Texture data must be a NumPy array or bytes.")
|
| 240 |
+
|
| 241 |
+
# Allocate memory for the texture
|
| 242 |
+
address = self.memory_manager.allocate_block(size_bytes)
|
| 243 |
+
|
| 244 |
+
if address is not None:
|
| 245 |
+
self.memory_manager.write_data(address, texture_data) # Write actual data
|
| 246 |
+
self.textures[name] = texture_data # Store actual data for reference
|
| 247 |
+
return name
|
| 248 |
+
else:
|
| 249 |
+
raise MemoryError("Failed to allocate memory for texture")
|
| 250 |
+
|
| 251 |
+
def get_texture(self, name: str) -> Optional[np.ndarray]:
|
| 252 |
+
"""Get texture data by name."""
|
| 253 |
+
return self.textures.get(name)
|
| 254 |
+
|
| 255 |
+
def cache_read(self, address: int, size: int) -> Optional[np.ndarray]:
|
| 256 |
+
"""Read data with caching support."""
|
| 257 |
+
cache_key = (address, size)
|
| 258 |
+
|
| 259 |
+
# Check cache first
|
| 260 |
+
if cache_key in self.cache:
|
| 261 |
+
# Move to end (most recently used)
|
| 262 |
+
data = self.cache.pop(cache_key)
|
| 263 |
+
self.cache[cache_key] = data
|
| 264 |
+
return data.copy()
|
| 265 |
+
|
| 266 |
+
# Read from memory
|
| 267 |
+
data = self.memory_manager.read_data(address, size)
|
| 268 |
+
if data is not None:
|
| 269 |
+
# Add to cache
|
| 270 |
+
if len(self.cache) >= self.cache_size:
|
| 271 |
+
# Remove least recently used item
|
| 272 |
+
self.cache.popitem(last=False)
|
| 273 |
+
self.cache[cache_key] = data.copy()
|
| 274 |
+
|
| 275 |
+
return data
|
| 276 |
+
|
| 277 |
+
def transfer_from_ram(self, name: str, data: Union[np.ndarray, bytes],
|
| 278 |
+
delay_ms: float = 0.0) -> Optional[str]:
|
| 279 |
+
"""Transfer a block of data from RAM to VRAM."""
|
| 280 |
+
if isinstance(data, np.ndarray):
|
| 281 |
+
size_bytes = data.nbytes
|
| 282 |
+
data_to_store = data.flatten()
|
| 283 |
+
elif isinstance(data, bytes):
|
| 284 |
+
size_bytes = len(data)
|
| 285 |
+
data_to_store = np.frombuffer(data, dtype=np.uint8)
|
| 286 |
+
else:
|
| 287 |
+
raise TypeError("Data must be a NumPy array or bytes.")
|
| 288 |
+
|
| 289 |
+
# Simulate delay
|
| 290 |
+
if delay_ms > 0:
|
| 291 |
+
time.sleep(delay_ms / 1000.0)
|
| 292 |
+
|
| 293 |
+
# Allocate memory in VRAM
|
| 294 |
+
address = self.memory_manager.allocate_block(size_bytes)
|
| 295 |
+
|
| 296 |
+
if address is not None:
|
| 297 |
+
# Store data in VRAM
|
| 298 |
+
self.memory_manager.write_data(address, data_to_store)
|
| 299 |
+
|
| 300 |
+
# Register the transferred data as a texture/buffer in VRAM
|
| 301 |
+
# For simplicity, we\"ll register it as a texture for now
|
| 302 |
+
texture_id = f"ram_transfer_{self.texture_counter}"
|
| 303 |
+
self.texture_counter += 1
|
| 304 |
+
self.textures[texture_id] = data # Store actual data for reference
|
| 305 |
+
print(f"Transferred {size_bytes} bytes from RAM to VRAM at address {address} as {texture_id}")
|
| 306 |
+
return texture_id
|
| 307 |
+
else:
|
| 308 |
+
print(f"Failed to transfer {size_bytes} bytes from RAM to VRAM: Out of VRAM memory.")
|
| 309 |
+
return None
|
| 310 |
+
|
| 311 |
+
def get_stats(self) -> Dict[str, Any]:
|
| 312 |
+
"""Get comprehensive VRAM statistics."""
|
| 313 |
+
memory_stats = self.memory_manager.get_memory_stats()
|
| 314 |
+
|
| 315 |
+
framebuffer_memory = sum(fb.get_memory_usage() for fb in self.framebuffers.values())
|
| 316 |
+
texture_memory = sum(tex.nbytes for tex in self.textures.values())
|
| 317 |
+
|
| 318 |
+
return {
|
| 319 |
+
**memory_stats,
|
| 320 |
+
"framebuffers_count": len(self.framebuffers),
|
| 321 |
+
"textures_count": len(self.textures),
|
| 322 |
+
"framebuffer_memory_bytes": framebuffer_memory,
|
| 323 |
+
"texture_memory_bytes": texture_memory,
|
| 324 |
+
"cache_entries": len(self.cache),
|
| 325 |
+
"cache_hit_ratio": 0.0 # TODO: Implement cache hit tracking
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
if __name__ == "__main__":
|
| 330 |
+
# Test the VRAM module
|
| 331 |
+
vram = VRAM(memory_size_gb=1) # Use 1GB for testing
|
| 332 |
+
|
| 333 |
+
# Create a framebuffer
|
| 334 |
+
fb_id = vram.create_framebuffer(1920, 1080, 3)
|
| 335 |
+
print(f"Created framebuffer: {fb_id}")
|
| 336 |
+
|
| 337 |
+
# Get the framebuffer and modify it
|
| 338 |
+
fb = vram.get_framebuffer(fb_id)
|
| 339 |
+
if fb:
|
| 340 |
+
fb.clear((255, 0, 0)) # Clear to red
|
| 341 |
+
fb.set_pixel(100, 100, (0, 255, 0)) # Set a green pixel
|
| 342 |
+
print(f"Framebuffer size: {fb.width}x{fb.height}")
|
| 343 |
+
print(f"Pixel at (100, 100): {fb.get_pixel(100, 100)}")
|
| 344 |
+
|
| 345 |
+
# Load a test texture
|
| 346 |
+
test_texture = np.random.randint(0, 256, (256, 256, 3), dtype=np.uint8)
|
| 347 |
+
tex_id = vram.load_texture(test_texture)
|
| 348 |
+
print(f"Loaded texture: {tex_id}")
|
| 349 |
+
|
| 350 |
+
# Test transfer_from_ram
|
| 351 |
+
ram_data = b"\x01\x02\x03\x04\x05\x06\x07\x08"
|
| 352 |
+
transferred_id = vram.transfer_from_ram("test_ram_data", ram_data, delay_ms=10)
|
| 353 |
+
print(f"Transferred RAM data ID: {transferred_id}")
|
| 354 |
+
|
| 355 |
+
# Print statistics
|
| 356 |
+
stats = vram.get_stats()
|
| 357 |
+
print(f"VRAM Stats: {stats}")
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
|