Spaces:
Runtime error
Runtime error
Factor Studios commited on
Update test_ai_integration_http.py
Browse files- test_ai_integration_http.py +231 -231
test_ai_integration_http.py
CHANGED
|
@@ -1,231 +1,231 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test Florence-2-Large model integration with vGPU.
|
| 3 |
-
Configure PyTorch to use vGPU as device and run image inference.
|
| 4 |
-
"""
|
| 5 |
-
import logging
|
| 6 |
-
import os
|
| 7 |
-
import time
|
| 8 |
-
from contextlib import contextmanager
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
|
| 11 |
-
import torch
|
| 12 |
-
from torch import nn
|
| 13 |
-
import torch.nn.functional as F
|
| 14 |
-
from PIL import Image
|
| 15 |
-
from transformers import (
|
| 16 |
-
AutoTokenizer,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
from virtual_vram import VirtualVRAM
|
| 21 |
-
from http_storage import HTTPGPUStorage
|
| 22 |
-
from torch_vgpu import VGPUDevice, to_vgpu
|
| 23 |
-
|
| 24 |
-
# Register vGPU device type
|
| 25 |
-
def register_vgpu_device():
|
| 26 |
-
"""Register vGPU as a custom device type"""
|
| 27 |
-
try:
|
| 28 |
-
if hasattr(torch.backends, 'register_custom_device'):
|
| 29 |
-
torch.backends.register_custom_device("vgpu", VGPUDevice)
|
| 30 |
-
else:
|
| 31 |
-
# Fallback: Add device type to torch._C
|
| 32 |
-
if not hasattr(torch._C, "_vgpu_device"):
|
| 33 |
-
torch._C._vgpu_device = VGPUDevice
|
| 34 |
-
logger.info("Using fallback vGPU device registration")
|
| 35 |
-
except Exception as e:
|
| 36 |
-
logger.error(f"vGPU device registration failed: {str(e)}")
|
| 37 |
-
raise
|
| 38 |
-
|
| 39 |
-
# Register vGPU device
|
| 40 |
-
register_vgpu_device()
|
| 41 |
-
|
| 42 |
-
# Configure logging
|
| 43 |
-
logging.basicConfig(
|
| 44 |
-
level=logging.INFO,
|
| 45 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 46 |
-
)
|
| 47 |
-
logger = logging.getLogger(__name__)
|
| 48 |
-
|
| 49 |
-
@contextmanager
|
| 50 |
-
def gpu_context():
|
| 51 |
-
"""Context manager for vGPU resources"""
|
| 52 |
-
storage = None
|
| 53 |
-
try:
|
| 54 |
-
storage = HTTPGPUStorage()
|
| 55 |
-
yield storage
|
| 56 |
-
finally:
|
| 57 |
-
if storage:
|
| 58 |
-
storage.close()
|
| 59 |
-
logger.info("vGPU resources cleaned up")
|
| 60 |
-
|
| 61 |
-
def get_model_size(model):
|
| 62 |
-
"""Calculate model size in parameters and memory footprint"""
|
| 63 |
-
param_size = 0
|
| 64 |
-
for param in model.parameters():
|
| 65 |
-
param_size += param.nelement() * param.element_size()
|
| 66 |
-
buffer_size = 0
|
| 67 |
-
for buffer in model.buffers():
|
| 68 |
-
buffer_size += buffer.nelement() * buffer.element_size()
|
| 69 |
-
return param_size + buffer_size
|
| 70 |
-
|
| 71 |
-
def load_image(image_name):
|
| 72 |
-
"""Load and preprocess image from sample_task folder"""
|
| 73 |
-
try:
|
| 74 |
-
image_path = os.path.join("sample_task", image_name)
|
| 75 |
-
if not os.path.exists(image_path):
|
| 76 |
-
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 77 |
-
|
| 78 |
-
image = Image.open(image_path)
|
| 79 |
-
# Convert to RGB if needed
|
| 80 |
-
if image.mode != 'RGB':
|
| 81 |
-
image = image.convert('RGB')
|
| 82 |
-
logger.info(f"Loaded image from {image_path}: size={image.size}")
|
| 83 |
-
return image
|
| 84 |
-
except Exception as e:
|
| 85 |
-
logger.error(f"Image loading failed: {str(e)}")
|
| 86 |
-
raise
|
| 87 |
-
|
| 88 |
-
def test_ai_integration_http():
|
| 89 |
-
"""Test Florence-2-Large model on vGPU with image inference"""
|
| 90 |
-
logger.info("Starting vGPU image inference test")
|
| 91 |
-
|
| 92 |
-
status = {
|
| 93 |
-
'model_loaded': False,
|
| 94 |
-
'processor_loaded': False,
|
| 95 |
-
'model_on_vgpu': False,
|
| 96 |
-
'image_processed': False,
|
| 97 |
-
'inference_complete': False,
|
| 98 |
-
'cleanup_success': False
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
with gpu_context() as storage:
|
| 102 |
-
try:
|
| 103 |
-
# Initialize vRAM with monitoring
|
| 104 |
-
initial_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 105 |
-
vram = VirtualVRAM(size_gb=None, storage=storage)
|
| 106 |
-
device = VGPUDevice(vram=vram)
|
| 107 |
-
logger.info("vGPU device initialized with HTTP storage backend")
|
| 108 |
-
|
| 109 |
-
# Load Florence model and processor
|
| 110 |
-
model_name = "microsoft/florence-2-large"
|
| 111 |
-
logger.info(f"Loading {model_name}")
|
| 112 |
-
|
| 113 |
-
try:
|
| 114 |
-
processor =
|
| 115 |
-
model_name,
|
| 116 |
-
trust_remote_code=True
|
| 117 |
-
)
|
| 118 |
-
model =
|
| 119 |
-
model_name,
|
| 120 |
-
trust_remote_code=True
|
| 121 |
-
)
|
| 122 |
-
status['processor_loaded'] = True
|
| 123 |
-
status['model_loaded'] = True
|
| 124 |
-
|
| 125 |
-
# Log model architecture
|
| 126 |
-
model_size = get_model_size(model)
|
| 127 |
-
logger.info(f"Model loaded: {model_size/1e9:.2f} GB in parameters")
|
| 128 |
-
logger.info(f"Model architecture: {model.__class__.__name__}")
|
| 129 |
-
except Exception as e:
|
| 130 |
-
logger.error(f"Model loading failed: {str(e)}")
|
| 131 |
-
raise
|
| 132 |
-
|
| 133 |
-
# Move model to vGPU with verification
|
| 134 |
-
try:
|
| 135 |
-
model = to_vgpu(model, vram=vram)
|
| 136 |
-
model.eval()
|
| 137 |
-
status['model_on_vgpu'] = True
|
| 138 |
-
|
| 139 |
-
# Verify model location
|
| 140 |
-
for param in model.parameters():
|
| 141 |
-
if not hasattr(param, 'device') or param.device != device:
|
| 142 |
-
raise RuntimeError("Model not properly moved to vGPU")
|
| 143 |
-
|
| 144 |
-
current_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 145 |
-
logger.info(f"Model memory usage: {(current_mem - initial_mem)/1e9:.2f} GB")
|
| 146 |
-
except Exception as e:
|
| 147 |
-
logger.error(f"Model transfer to vGPU failed: {str(e)}")
|
| 148 |
-
raise
|
| 149 |
-
|
| 150 |
-
# Prepare image input from sample_task folder
|
| 151 |
-
try:
|
| 152 |
-
# Load image from sample_task directory
|
| 153 |
-
image_name = "sample1.jpg" # Replace with your image name
|
| 154 |
-
image = load_image(image_name)
|
| 155 |
-
|
| 156 |
-
# Process image with Florence processor
|
| 157 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 158 |
-
if not inputs or 'pixel_values' not in inputs:
|
| 159 |
-
raise ValueError("Invalid processor output")
|
| 160 |
-
|
| 161 |
-
# Move inputs to vGPU
|
| 162 |
-
inputs = {k: to_vgpu(v, vram=vram) for k, v in inputs.items()}
|
| 163 |
-
status['image_processed'] = True
|
| 164 |
-
logger.info(f"Image processed: shape={inputs['pixel_values'].shape}")
|
| 165 |
-
except Exception as e:
|
| 166 |
-
logger.error(f"Image preparation failed: {str(e)}")
|
| 167 |
-
raise
|
| 168 |
-
|
| 169 |
-
# Run image inference with monitoring
|
| 170 |
-
logger.info("Running image inference...")
|
| 171 |
-
start = time.time()
|
| 172 |
-
peak_mem = initial_mem
|
| 173 |
-
|
| 174 |
-
try:
|
| 175 |
-
with torch.no_grad():
|
| 176 |
-
# Get image embeddings
|
| 177 |
-
outputs = model(**inputs)
|
| 178 |
-
image_features = outputs.last_hidden_state[:, 0] # Take [CLS] token features
|
| 179 |
-
|
| 180 |
-
# Normalize features
|
| 181 |
-
image_features = F.normalize(image_features, dim=-1)
|
| 182 |
-
|
| 183 |
-
if hasattr(storage, 'get_used_memory'):
|
| 184 |
-
peak_mem = max(peak_mem, storage.get_used_memory())
|
| 185 |
-
|
| 186 |
-
inference_time = time.time() - start
|
| 187 |
-
status['inference_complete'] = True
|
| 188 |
-
|
| 189 |
-
# Log performance metrics
|
| 190 |
-
logger.info(f"Inference stats:")
|
| 191 |
-
logger.info(f"- Time: {inference_time:.4f}s")
|
| 192 |
-
logger.info(f"- Memory peak: {(peak_mem - initial_mem)/1e9:.2f} GB")
|
| 193 |
-
logger.info(f"- Image features shape: {image_features.shape}")
|
| 194 |
-
logger.info(f"- Feature norm: {torch.norm(image_features).item():.4f}")
|
| 195 |
-
logger.info(f"- Output device: {image_features.device}")
|
| 196 |
-
|
| 197 |
-
# Optionally compute confidence scores
|
| 198 |
-
if hasattr(outputs, 'logits'):
|
| 199 |
-
logits = outputs.logits
|
| 200 |
-
probs = F.softmax(logits, dim=-1)
|
| 201 |
-
confidence = torch.max(probs).item()
|
| 202 |
-
logger.info(f"- Confidence: {confidence:.4f}")
|
| 203 |
-
|
| 204 |
-
except Exception as e:
|
| 205 |
-
logger.error(f"Image inference failed: {str(e)}")
|
| 206 |
-
raise
|
| 207 |
-
|
| 208 |
-
except Exception as e:
|
| 209 |
-
logger.error(f"Test failed: {str(e)}")
|
| 210 |
-
raise
|
| 211 |
-
finally:
|
| 212 |
-
# Cleanup and status report
|
| 213 |
-
try:
|
| 214 |
-
del model
|
| 215 |
-
del outputs
|
| 216 |
-
torch.cuda.empty_cache() if hasattr(torch, 'cuda') else None
|
| 217 |
-
status['cleanup_success'] = True
|
| 218 |
-
except Exception as e:
|
| 219 |
-
logger.error(f"Cleanup error: {str(e)}")
|
| 220 |
-
|
| 221 |
-
logger.info("\nTest Summary:")
|
| 222 |
-
for key, value in status.items():
|
| 223 |
-
logger.info(f"- {key}: {'✓' if value else '✗'}")
|
| 224 |
-
|
| 225 |
-
final_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 226 |
-
if final_mem > initial_mem:
|
| 227 |
-
logger.warning(f"Memory leak detected: {(final_mem - initial_mem)/1e6:.2f} MB")
|
| 228 |
-
|
| 229 |
-
if __name__ == "__main__":
|
| 230 |
-
test_ai_integration_http()
|
| 231 |
-
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test Florence-2-Large model integration with vGPU.
|
| 3 |
+
Configure PyTorch to use vGPU as device and run image inference.
|
| 4 |
+
"""
|
| 5 |
+
import logging
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
from contextlib import contextmanager
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
from torch import nn
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
from PIL import Image
|
| 15 |
+
from transformers import (
|
| 16 |
+
AutoTokenizer,
|
| 17 |
+
AutoModelForCausalLM,
|
| 18 |
+
AutoProcessor
|
| 19 |
+
)
|
| 20 |
+
from virtual_vram import VirtualVRAM
|
| 21 |
+
from http_storage import HTTPGPUStorage
|
| 22 |
+
from torch_vgpu import VGPUDevice, to_vgpu
|
| 23 |
+
|
| 24 |
+
# Register vGPU device type
|
| 25 |
+
def register_vgpu_device():
|
| 26 |
+
"""Register vGPU as a custom device type"""
|
| 27 |
+
try:
|
| 28 |
+
if hasattr(torch.backends, 'register_custom_device'):
|
| 29 |
+
torch.backends.register_custom_device("vgpu", VGPUDevice)
|
| 30 |
+
else:
|
| 31 |
+
# Fallback: Add device type to torch._C
|
| 32 |
+
if not hasattr(torch._C, "_vgpu_device"):
|
| 33 |
+
torch._C._vgpu_device = VGPUDevice
|
| 34 |
+
logger.info("Using fallback vGPU device registration")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.error(f"vGPU device registration failed: {str(e)}")
|
| 37 |
+
raise
|
| 38 |
+
|
| 39 |
+
# Register vGPU device
|
| 40 |
+
register_vgpu_device()
|
| 41 |
+
|
| 42 |
+
# Configure logging
|
| 43 |
+
logging.basicConfig(
|
| 44 |
+
level=logging.INFO,
|
| 45 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 46 |
+
)
|
| 47 |
+
logger = logging.getLogger(__name__)
|
| 48 |
+
|
| 49 |
+
@contextmanager
|
| 50 |
+
def gpu_context():
|
| 51 |
+
"""Context manager for vGPU resources"""
|
| 52 |
+
storage = None
|
| 53 |
+
try:
|
| 54 |
+
storage = HTTPGPUStorage()
|
| 55 |
+
yield storage
|
| 56 |
+
finally:
|
| 57 |
+
if storage:
|
| 58 |
+
storage.close()
|
| 59 |
+
logger.info("vGPU resources cleaned up")
|
| 60 |
+
|
| 61 |
+
def get_model_size(model):
|
| 62 |
+
"""Calculate model size in parameters and memory footprint"""
|
| 63 |
+
param_size = 0
|
| 64 |
+
for param in model.parameters():
|
| 65 |
+
param_size += param.nelement() * param.element_size()
|
| 66 |
+
buffer_size = 0
|
| 67 |
+
for buffer in model.buffers():
|
| 68 |
+
buffer_size += buffer.nelement() * buffer.element_size()
|
| 69 |
+
return param_size + buffer_size
|
| 70 |
+
|
| 71 |
+
def load_image(image_name):
|
| 72 |
+
"""Load and preprocess image from sample_task folder"""
|
| 73 |
+
try:
|
| 74 |
+
image_path = os.path.join("sample_task", image_name)
|
| 75 |
+
if not os.path.exists(image_path):
|
| 76 |
+
raise FileNotFoundError(f"Image not found: {image_path}")
|
| 77 |
+
|
| 78 |
+
image = Image.open(image_path)
|
| 79 |
+
# Convert to RGB if needed
|
| 80 |
+
if image.mode != 'RGB':
|
| 81 |
+
image = image.convert('RGB')
|
| 82 |
+
logger.info(f"Loaded image from {image_path}: size={image.size}")
|
| 83 |
+
return image
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logger.error(f"Image loading failed: {str(e)}")
|
| 86 |
+
raise
|
| 87 |
+
|
| 88 |
+
def test_ai_integration_http():
|
| 89 |
+
"""Test Florence-2-Large model on vGPU with image inference"""
|
| 90 |
+
logger.info("Starting vGPU image inference test")
|
| 91 |
+
|
| 92 |
+
status = {
|
| 93 |
+
'model_loaded': False,
|
| 94 |
+
'processor_loaded': False,
|
| 95 |
+
'model_on_vgpu': False,
|
| 96 |
+
'image_processed': False,
|
| 97 |
+
'inference_complete': False,
|
| 98 |
+
'cleanup_success': False
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
with gpu_context() as storage:
|
| 102 |
+
try:
|
| 103 |
+
# Initialize vRAM with monitoring
|
| 104 |
+
initial_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 105 |
+
vram = VirtualVRAM(size_gb=None, storage=storage)
|
| 106 |
+
device = VGPUDevice(vram=vram)
|
| 107 |
+
logger.info("vGPU device initialized with HTTP storage backend")
|
| 108 |
+
|
| 109 |
+
# Load Florence model and processor
|
| 110 |
+
model_name = "microsoft/florence-2-large"
|
| 111 |
+
logger.info(f"Loading {model_name}")
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
processor = AutoProcessor.from_pretrained(
|
| 115 |
+
model_name,
|
| 116 |
+
trust_remote_code=True
|
| 117 |
+
)
|
| 118 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 119 |
+
model_name,
|
| 120 |
+
trust_remote_code=True
|
| 121 |
+
)
|
| 122 |
+
status['processor_loaded'] = True
|
| 123 |
+
status['model_loaded'] = True
|
| 124 |
+
|
| 125 |
+
# Log model architecture
|
| 126 |
+
model_size = get_model_size(model)
|
| 127 |
+
logger.info(f"Model loaded: {model_size/1e9:.2f} GB in parameters")
|
| 128 |
+
logger.info(f"Model architecture: {model.__class__.__name__}")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
logger.error(f"Model loading failed: {str(e)}")
|
| 131 |
+
raise
|
| 132 |
+
|
| 133 |
+
# Move model to vGPU with verification
|
| 134 |
+
try:
|
| 135 |
+
model = to_vgpu(model, vram=vram)
|
| 136 |
+
model.eval()
|
| 137 |
+
status['model_on_vgpu'] = True
|
| 138 |
+
|
| 139 |
+
# Verify model location
|
| 140 |
+
for param in model.parameters():
|
| 141 |
+
if not hasattr(param, 'device') or param.device != device:
|
| 142 |
+
raise RuntimeError("Model not properly moved to vGPU")
|
| 143 |
+
|
| 144 |
+
current_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 145 |
+
logger.info(f"Model memory usage: {(current_mem - initial_mem)/1e9:.2f} GB")
|
| 146 |
+
except Exception as e:
|
| 147 |
+
logger.error(f"Model transfer to vGPU failed: {str(e)}")
|
| 148 |
+
raise
|
| 149 |
+
|
| 150 |
+
# Prepare image input from sample_task folder
|
| 151 |
+
try:
|
| 152 |
+
# Load image from sample_task directory
|
| 153 |
+
image_name = "sample1.jpg" # Replace with your image name
|
| 154 |
+
image = load_image(image_name)
|
| 155 |
+
|
| 156 |
+
# Process image with Florence processor
|
| 157 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 158 |
+
if not inputs or 'pixel_values' not in inputs:
|
| 159 |
+
raise ValueError("Invalid processor output")
|
| 160 |
+
|
| 161 |
+
# Move inputs to vGPU
|
| 162 |
+
inputs = {k: to_vgpu(v, vram=vram) for k, v in inputs.items()}
|
| 163 |
+
status['image_processed'] = True
|
| 164 |
+
logger.info(f"Image processed: shape={inputs['pixel_values'].shape}")
|
| 165 |
+
except Exception as e:
|
| 166 |
+
logger.error(f"Image preparation failed: {str(e)}")
|
| 167 |
+
raise
|
| 168 |
+
|
| 169 |
+
# Run image inference with monitoring
|
| 170 |
+
logger.info("Running image inference...")
|
| 171 |
+
start = time.time()
|
| 172 |
+
peak_mem = initial_mem
|
| 173 |
+
|
| 174 |
+
try:
|
| 175 |
+
with torch.no_grad():
|
| 176 |
+
# Get image embeddings
|
| 177 |
+
outputs = model(**inputs)
|
| 178 |
+
image_features = outputs.last_hidden_state[:, 0] # Take [CLS] token features
|
| 179 |
+
|
| 180 |
+
# Normalize features
|
| 181 |
+
image_features = F.normalize(image_features, dim=-1)
|
| 182 |
+
|
| 183 |
+
if hasattr(storage, 'get_used_memory'):
|
| 184 |
+
peak_mem = max(peak_mem, storage.get_used_memory())
|
| 185 |
+
|
| 186 |
+
inference_time = time.time() - start
|
| 187 |
+
status['inference_complete'] = True
|
| 188 |
+
|
| 189 |
+
# Log performance metrics
|
| 190 |
+
logger.info(f"Inference stats:")
|
| 191 |
+
logger.info(f"- Time: {inference_time:.4f}s")
|
| 192 |
+
logger.info(f"- Memory peak: {(peak_mem - initial_mem)/1e9:.2f} GB")
|
| 193 |
+
logger.info(f"- Image features shape: {image_features.shape}")
|
| 194 |
+
logger.info(f"- Feature norm: {torch.norm(image_features).item():.4f}")
|
| 195 |
+
logger.info(f"- Output device: {image_features.device}")
|
| 196 |
+
|
| 197 |
+
# Optionally compute confidence scores
|
| 198 |
+
if hasattr(outputs, 'logits'):
|
| 199 |
+
logits = outputs.logits
|
| 200 |
+
probs = F.softmax(logits, dim=-1)
|
| 201 |
+
confidence = torch.max(probs).item()
|
| 202 |
+
logger.info(f"- Confidence: {confidence:.4f}")
|
| 203 |
+
|
| 204 |
+
except Exception as e:
|
| 205 |
+
logger.error(f"Image inference failed: {str(e)}")
|
| 206 |
+
raise
|
| 207 |
+
|
| 208 |
+
except Exception as e:
|
| 209 |
+
logger.error(f"Test failed: {str(e)}")
|
| 210 |
+
raise
|
| 211 |
+
finally:
|
| 212 |
+
# Cleanup and status report
|
| 213 |
+
try:
|
| 214 |
+
del model
|
| 215 |
+
del outputs
|
| 216 |
+
torch.cuda.empty_cache() if hasattr(torch, 'cuda') else None
|
| 217 |
+
status['cleanup_success'] = True
|
| 218 |
+
except Exception as e:
|
| 219 |
+
logger.error(f"Cleanup error: {str(e)}")
|
| 220 |
+
|
| 221 |
+
logger.info("\nTest Summary:")
|
| 222 |
+
for key, value in status.items():
|
| 223 |
+
logger.info(f"- {key}: {'✓' if value else '✗'}")
|
| 224 |
+
|
| 225 |
+
final_mem = storage.get_used_memory() if hasattr(storage, 'get_used_memory') else 0
|
| 226 |
+
if final_mem > initial_mem:
|
| 227 |
+
logger.warning(f"Memory leak detected: {(final_mem - initial_mem)/1e6:.2f} MB")
|
| 228 |
+
|
| 229 |
+
if __name__ == "__main__":
|
| 230 |
+
test_ai_integration_http()
|
| 231 |
+
|