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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Comprehensive test script for Wildnerve TLM that tests both model functionality and weight loading.
|
| 3 |
+
|
| 4 |
+
Usage:
|
| 5 |
+
# Test model inference with custom prompt
|
| 6 |
+
python test_model.py --prompt "Your test prompt here"
|
| 7 |
+
|
| 8 |
+
# Test the weights and maths
|
| 9 |
+
python test_model.py --check-weights --check-math --diagnostics
|
| 10 |
+
|
| 11 |
+
# Test to verify repos and list weights
|
| 12 |
+
python test_model.py --verify-repos --list-weights
|
| 13 |
+
|
| 14 |
+
# Test everything
|
| 15 |
+
python test_model.py --all
|
| 16 |
+
|
| 17 |
+
# Test just the weight loading
|
| 18 |
+
python test_model.py --check-weights
|
| 19 |
+
|
| 20 |
+
# Check repository access and list available weights
|
| 21 |
+
python test_model.py --verify-repos --list-weights
|
| 22 |
+
|
| 23 |
+
# Test model inference with custom prompt
|
| 24 |
+
python test_model.py --prompt "What is quantum computing?"
|
| 25 |
+
"""
|
| 26 |
+
import os
|
| 27 |
+
import sys
|
| 28 |
+
import time
|
| 29 |
+
import logging
|
| 30 |
+
import argparse
|
| 31 |
+
import importlib.util
|
| 32 |
+
from typing import Dict, Any, Optional, List, Tuple
|
| 33 |
+
from pathlib import Path
|
| 34 |
+
|
| 35 |
+
# Configure logging
|
| 36 |
+
logging.basicConfig(
|
| 37 |
+
level=logging.INFO,
|
| 38 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 39 |
+
)
|
| 40 |
+
logger = logging.getLogger(__name__)
|
| 41 |
+
|
| 42 |
+
def test_model_loading(prompt: str, verbose: bool = False) -> Dict[str, Any]:
|
| 43 |
+
"""
|
| 44 |
+
Test if the model loads correctly and can generate responses.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
prompt: Text prompt to test with
|
| 48 |
+
verbose: Whether to print detailed diagnostics
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
Dictionary with test results
|
| 52 |
+
"""
|
| 53 |
+
results = {
|
| 54 |
+
"success": False,
|
| 55 |
+
"model_loaded": False,
|
| 56 |
+
"response": None,
|
| 57 |
+
"response_type": None,
|
| 58 |
+
"elapsed_time": 0,
|
| 59 |
+
"error": None
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Import adapter layer
|
| 64 |
+
from adapter_layer import WildnerveModelAdapter
|
| 65 |
+
adapter = WildnerveModelAdapter("")
|
| 66 |
+
logger.info("Model adapter initialized")
|
| 67 |
+
|
| 68 |
+
# Record start time for performance measurement
|
| 69 |
+
start_time = time.time()
|
| 70 |
+
|
| 71 |
+
# Try to generate a response
|
| 72 |
+
logger.info(f"Generating response for: {prompt}")
|
| 73 |
+
response = adapter.generate(prompt)
|
| 74 |
+
|
| 75 |
+
# Record elapsed time
|
| 76 |
+
elapsed_time = time.time() - start_time
|
| 77 |
+
results["elapsed_time"] = elapsed_time
|
| 78 |
+
results["response"] = response
|
| 79 |
+
|
| 80 |
+
# Check if we got a non-fallback response
|
| 81 |
+
fallback_phrases = [
|
| 82 |
+
"I've received your input about",
|
| 83 |
+
"Processing:",
|
| 84 |
+
"The model couldn't be properly initialized",
|
| 85 |
+
"No language model available"
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
is_fallback = any(phrase in response for phrase in fallback_phrases)
|
| 89 |
+
results["response_type"] = "fallback" if is_fallback else "model"
|
| 90 |
+
results["model_loaded"] = not is_fallback
|
| 91 |
+
results["success"] = True
|
| 92 |
+
|
| 93 |
+
if verbose:
|
| 94 |
+
logger.info(f"Response ({len(response)} chars): {response[:100]}...")
|
| 95 |
+
logger.info(f"Response appears to be from: {'fallback' if is_fallback else 'neural model'}")
|
| 96 |
+
logger.info(f"Generation took: {elapsed_time:.2f} seconds")
|
| 97 |
+
|
| 98 |
+
return results
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
logger.error(f"Error testing model: {e}", exc_info=True)
|
| 102 |
+
results["error"] = str(e)
|
| 103 |
+
return results
|
| 104 |
+
|
| 105 |
+
def test_math_capability() -> Dict[str, Any]:
|
| 106 |
+
"""
|
| 107 |
+
Test the model's math capabilities with various arithmetic expressions.
|
| 108 |
+
|
| 109 |
+
Returns:
|
| 110 |
+
Dictionary with test results
|
| 111 |
+
"""
|
| 112 |
+
results = {
|
| 113 |
+
"success": False,
|
| 114 |
+
"tests_passed": 0,
|
| 115 |
+
"tests_total": 0,
|
| 116 |
+
"details": []
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
# Test cases: (prompt, expected_contains)
|
| 120 |
+
math_tests = [
|
| 121 |
+
("What is 3 + 4?", "7"),
|
| 122 |
+
("What is 12 * 5?", "60"),
|
| 123 |
+
("Calculate 18 / 6", "3"),
|
| 124 |
+
("What is four multiplied by three?", "12"),
|
| 125 |
+
("What is seven plus nine?", "16"),
|
| 126 |
+
("Compute 25 - 13", "12")
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
from adapter_layer import WildnerveModelAdapter
|
| 131 |
+
adapter = WildnerveModelAdapter("")
|
| 132 |
+
logger.info("Testing math capabilities...")
|
| 133 |
+
|
| 134 |
+
results["tests_total"] = len(math_tests)
|
| 135 |
+
|
| 136 |
+
for i, (prompt, expected) in enumerate(math_tests):
|
| 137 |
+
logger.info(f"Math test {i+1}/{len(math_tests)}: {prompt}")
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
response = adapter.generate(prompt)
|
| 141 |
+
passes = expected in response
|
| 142 |
+
|
| 143 |
+
results["details"].append({
|
| 144 |
+
"prompt": prompt,
|
| 145 |
+
"response": response,
|
| 146 |
+
"expected": expected,
|
| 147 |
+
"passed": passes
|
| 148 |
+
})
|
| 149 |
+
|
| 150 |
+
if passes:
|
| 151 |
+
results["tests_passed"] += 1
|
| 152 |
+
logger.info(f"✓ Test passed: found '{expected}' in response")
|
| 153 |
+
else:
|
| 154 |
+
logger.info(f"✗ Test failed: '{expected}' not found in response")
|
| 155 |
+
logger.info(f"Response: {response[:100]}...")
|
| 156 |
+
except Exception as e:
|
| 157 |
+
logger.error(f"Error in math test: {e}")
|
| 158 |
+
results["details"].append({
|
| 159 |
+
"prompt": prompt,
|
| 160 |
+
"error": str(e),
|
| 161 |
+
"passed": False
|
| 162 |
+
})
|
| 163 |
+
|
| 164 |
+
results["success"] = True
|
| 165 |
+
return results
|
| 166 |
+
except Exception as e:
|
| 167 |
+
logger.error(f"Failed to run math tests: {e}")
|
| 168 |
+
results["error"] = str(e)
|
| 169 |
+
return results
|
| 170 |
+
|
| 171 |
+
def test_weight_loading() -> Dict[str, Any]:
|
| 172 |
+
"""Test loading model weights from local files or HF repository.
|
| 173 |
+
|
| 174 |
+
Returns:
|
| 175 |
+
Dictionary with test results
|
| 176 |
+
"""
|
| 177 |
+
results = {
|
| 178 |
+
"success": False,
|
| 179 |
+
"local_weights_found": False,
|
| 180 |
+
"downloaded_weights": False,
|
| 181 |
+
"weight_files": {},
|
| 182 |
+
"errors": [],
|
| 183 |
+
"elapsed_time": 0
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
start_time = time.time()
|
| 188 |
+
|
| 189 |
+
# Try to import load_model_weights
|
| 190 |
+
try:
|
| 191 |
+
from load_model_weights import load_model_weights, check_for_local_weights, verify_token
|
| 192 |
+
|
| 193 |
+
# First check token
|
| 194 |
+
token_verified = verify_token()
|
| 195 |
+
results["token_verified"] = token_verified
|
| 196 |
+
|
| 197 |
+
# Check for local weights
|
| 198 |
+
local_weights = check_for_local_weights()
|
| 199 |
+
results["local_weights_found"] = local_weights
|
| 200 |
+
|
| 201 |
+
if local_weights:
|
| 202 |
+
results["weight_files"] = {
|
| 203 |
+
"transformer": os.environ.get("TLM_TRANSFORMER_WEIGHTS"),
|
| 204 |
+
"snn": os.environ.get("TLM_SNN_WEIGHTS")
|
| 205 |
+
}
|
| 206 |
+
logger.info("Found local weights")
|
| 207 |
+
else:
|
| 208 |
+
# Try downloading weights
|
| 209 |
+
logger.info("No local weights found, downloading from HF Hub...")
|
| 210 |
+
weight_files = load_model_weights()
|
| 211 |
+
|
| 212 |
+
if weight_files:
|
| 213 |
+
results["downloaded_weights"] = True
|
| 214 |
+
results["weight_files"] = weight_files
|
| 215 |
+
logger.info(f"Downloaded weights: {list(weight_files.keys())}")
|
| 216 |
+
else:
|
| 217 |
+
logger.warning("Failed to download weights")
|
| 218 |
+
results["errors"].append("Failed to download weights")
|
| 219 |
+
|
| 220 |
+
except ImportError as e:
|
| 221 |
+
logger.error(f"Could not import load_model_weights: {e}")
|
| 222 |
+
results["errors"].append(f"ImportError: {str(e)}")
|
| 223 |
+
|
| 224 |
+
# Check if we got any weights
|
| 225 |
+
if results["local_weights_found"] or results["downloaded_weights"]:
|
| 226 |
+
results["success"] = True
|
| 227 |
+
|
| 228 |
+
# Record elapsed time
|
| 229 |
+
results["elapsed_time"] = time.time() - start_time
|
| 230 |
+
|
| 231 |
+
return results
|
| 232 |
+
except Exception as e:
|
| 233 |
+
logger.error(f"Error testing weight loading: {e}", exc_info=True)
|
| 234 |
+
results["errors"].append(str(e))
|
| 235 |
+
results["elapsed_time"] = time.time() - start_time
|
| 236 |
+
return results
|
| 237 |
+
|
| 238 |
+
def verify_repositories() -> Dict[str, Any]:
|
| 239 |
+
"""Verify access to model repositories.
|
| 240 |
+
|
| 241 |
+
Returns:
|
| 242 |
+
Dictionary with verification results
|
| 243 |
+
"""
|
| 244 |
+
results = {
|
| 245 |
+
"repositories_checked": 0,
|
| 246 |
+
"repositories_accessible": 0,
|
| 247 |
+
"details": {}
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
try:
|
| 251 |
+
# Try to import verification function
|
| 252 |
+
from load_model_weights import verify_repository, verify_token
|
| 253 |
+
|
| 254 |
+
# Get token
|
| 255 |
+
token = os.environ.get("HF_TOKEN", os.environ.get("HF_API_TOKEN"))
|
| 256 |
+
token_verified = verify_token()
|
| 257 |
+
results["token_verified"] = token_verified
|
| 258 |
+
|
| 259 |
+
# First try to get repositories from model_repo_config
|
| 260 |
+
try:
|
| 261 |
+
from model_repo_config import get_repo_config
|
| 262 |
+
config = get_repo_config()
|
| 263 |
+
repos_to_check = [config.repo_id] + config.alternative_paths
|
| 264 |
+
except ImportError:
|
| 265 |
+
# Fallback repositories
|
| 266 |
+
repos_to_check = [
|
| 267 |
+
"EvolphTech/Weights",
|
| 268 |
+
"Wildnerve/tlm-0.05Bx12",
|
| 269 |
+
"Wildnerve/tlm",
|
| 270 |
+
"EvolphTech/Checkpoints",
|
| 271 |
+
"bert-base-uncased" # Fallback public model
|
| 272 |
+
]
|
| 273 |
+
|
| 274 |
+
# Check each repository
|
| 275 |
+
for repo in repos_to_check:
|
| 276 |
+
logger.info(f"Verifying repository: {repo}")
|
| 277 |
+
success, files = verify_repository(repo, token)
|
| 278 |
+
|
| 279 |
+
results["repositories_checked"] += 1
|
| 280 |
+
if success:
|
| 281 |
+
results["repositories_accessible"] += 1
|
| 282 |
+
|
| 283 |
+
results["details"][repo] = {
|
| 284 |
+
"accessible": success,
|
| 285 |
+
"num_files": len(files) if success else 0,
|
| 286 |
+
"model_files": [f for f in files if f.endswith('.bin') or f.endswith('.pt')] if success else []
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
return results
|
| 290 |
+
except Exception as e:
|
| 291 |
+
logger.error(f"Error verifying repositories: {e}", exc_info=True)
|
| 292 |
+
results["error"] = str(e)
|
| 293 |
+
return results
|
| 294 |
+
|
| 295 |
+
def list_weight_files() -> Dict[str, Any]:
|
| 296 |
+
"""List available weight files in repositories and locally.
|
| 297 |
+
|
| 298 |
+
Returns:
|
| 299 |
+
Dictionary with weight file lists
|
| 300 |
+
"""
|
| 301 |
+
results = {
|
| 302 |
+
"local_weights": [],
|
| 303 |
+
"repository_weights": {},
|
| 304 |
+
"error": None
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
try:
|
| 308 |
+
# Check local weight files
|
| 309 |
+
local_paths = [
|
| 310 |
+
"/app/Weights/",
|
| 311 |
+
"./Weights/",
|
| 312 |
+
"/tmp/hf_cache/",
|
| 313 |
+
"/tmp/tlm_cache/"
|
| 314 |
+
]
|
| 315 |
+
|
| 316 |
+
for base_path in local_paths:
|
| 317 |
+
if os.path.exists(base_path):
|
| 318 |
+
for root, _, files in os.walk(base_path):
|
| 319 |
+
for file in files:
|
| 320 |
+
if file.endswith(('.bin', '.pt', '.pth')):
|
| 321 |
+
full_path = os.path.join(root, file)
|
| 322 |
+
relative_path = os.path.relpath(full_path, base_path)
|
| 323 |
+
results["local_weights"].append({
|
| 324 |
+
"path": full_path,
|
| 325 |
+
"relative_path": relative_path,
|
| 326 |
+
"size_mb": os.path.getsize(full_path) / (1024 * 1024)
|
| 327 |
+
})
|
| 328 |
+
|
| 329 |
+
# List repository weight files
|
| 330 |
+
try:
|
| 331 |
+
from load_model_weights import list_model_files, verify_token
|
| 332 |
+
|
| 333 |
+
# Get token
|
| 334 |
+
token = os.environ.get("HF_TOKEN", os.environ.get("HF_API_TOKEN"))
|
| 335 |
+
token_verified = verify_token()
|
| 336 |
+
|
| 337 |
+
# First try to get repositories from model_repo_config
|
| 338 |
+
try:
|
| 339 |
+
from model_repo_config import get_repo_config
|
| 340 |
+
config = get_repo_config()
|
| 341 |
+
repos_to_check = [config.repo_id] + config.alternative_paths[:2] # Only check first few
|
| 342 |
+
except ImportError:
|
| 343 |
+
# Fallback repositories
|
| 344 |
+
repos_to_check = ["EvolphTech/Weights", "Wildnerve/tlm-0.05Bx12"]
|
| 345 |
+
|
| 346 |
+
# Check each repository
|
| 347 |
+
for repo in repos_to_check:
|
| 348 |
+
try:
|
| 349 |
+
logger.info(f"Listing files in repository: {repo}")
|
| 350 |
+
files = list_model_files(repo, token)
|
| 351 |
+
results["repository_weights"][repo] = files
|
| 352 |
+
except Exception as e:
|
| 353 |
+
logger.warning(f"Error listing files in {repo}: {e}")
|
| 354 |
+
results["repository_weights"][repo] = f"Error: {str(e)}"
|
| 355 |
+
except ImportError as e:
|
| 356 |
+
results["error"] = f"Could not import functions to list repository files: {e}"
|
| 357 |
+
|
| 358 |
+
return results
|
| 359 |
+
except Exception as e:
|
| 360 |
+
logger.error(f"Error listing weight files: {e}", exc_info=True)
|
| 361 |
+
results["error"] = str(e)
|
| 362 |
+
return results
|
| 363 |
+
|
| 364 |
+
def test_weight_loading_in_model() -> Dict[str, Any]:
|
| 365 |
+
"""Test loading weights into an actual model instance.
|
| 366 |
+
|
| 367 |
+
Returns:
|
| 368 |
+
Dictionary with test results
|
| 369 |
+
"""
|
| 370 |
+
results = {
|
| 371 |
+
"success": False,
|
| 372 |
+
"model_created": False,
|
| 373 |
+
"weights_loaded": False,
|
| 374 |
+
"weight_path": None,
|
| 375 |
+
"error": None
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
try:
|
| 379 |
+
# Try to find or download weights
|
| 380 |
+
weight_loading_results = test_weight_loading()
|
| 381 |
+
|
| 382 |
+
if not (weight_loading_results["local_weights_found"] or weight_loading_results["downloaded_weights"]):
|
| 383 |
+
results["error"] = "No weights available to test"
|
| 384 |
+
return results
|
| 385 |
+
|
| 386 |
+
# Get weight path
|
| 387 |
+
weight_path = None
|
| 388 |
+
if "transformer" in weight_loading_results.get("weight_files", {}):
|
| 389 |
+
weight_path = weight_loading_results["weight_files"]["transformer"]
|
| 390 |
+
|
| 391 |
+
if not weight_path or not os.path.exists(weight_path):
|
| 392 |
+
results["error"] = f"Weight file not found at {weight_path}"
|
| 393 |
+
return results
|
| 394 |
+
|
| 395 |
+
results["weight_path"] = weight_path
|
| 396 |
+
|
| 397 |
+
# Try to create a model
|
| 398 |
+
try:
|
| 399 |
+
# Try model_Custm first
|
| 400 |
+
try:
|
| 401 |
+
import model_Custm
|
| 402 |
+
if hasattr(model_Custm, "Wildnerve_tlm01"):
|
| 403 |
+
logger.info("Creating Wildnerve_tlm01 from model_Custm")
|
| 404 |
+
model_class = getattr(model_Custm, "Wildnerve_tlm01")
|
| 405 |
+
model = model_class(
|
| 406 |
+
vocab_size=50257, # GPT-2 vocab size
|
| 407 |
+
specialization="general",
|
| 408 |
+
embedding_dim=768,
|
| 409 |
+
num_heads=12,
|
| 410 |
+
hidden_dim=768,
|
| 411 |
+
num_layers=2,
|
| 412 |
+
output_size=50257,
|
| 413 |
+
dropout=0.1,
|
| 414 |
+
max_seq_length=128
|
| 415 |
+
)
|
| 416 |
+
results["model_created"] = True
|
| 417 |
+
except Exception as e:
|
| 418 |
+
logger.warning(f"Error creating model_Custm: {e}")
|
| 419 |
+
|
| 420 |
+
# Try model_PrTr as fallback
|
| 421 |
+
try:
|
| 422 |
+
import model_PrTr
|
| 423 |
+
if hasattr(model_PrTr, "Wildnerve_tlm01"):
|
| 424 |
+
logger.info("Creating Wildnerve_tlm01 from model_PrTr")
|
| 425 |
+
model_class = getattr(model_PrTr, "Wildnerve_tlm01")
|
| 426 |
+
model = model_class(
|
| 427 |
+
model_name="gpt2"
|
| 428 |
+
)
|
| 429 |
+
results["model_created"] = True
|
| 430 |
+
except Exception as e2:
|
| 431 |
+
logger.error(f"Error creating model_PrTr: {e2}")
|
| 432 |
+
results["error"] = f"Could not create any model: {e}, {e2}"
|
| 433 |
+
return results
|
| 434 |
+
|
| 435 |
+
# Load weights into model
|
| 436 |
+
if results["model_created"]:
|
| 437 |
+
from load_model_weights import load_weights_into_model
|
| 438 |
+
success = load_weights_into_model(model, weight_path, strict=False)
|
| 439 |
+
results["weights_loaded"] = success
|
| 440 |
+
|
| 441 |
+
if success:
|
| 442 |
+
# Try a quick test inference
|
| 443 |
+
try:
|
| 444 |
+
test_input = "This is a test."
|
| 445 |
+
if hasattr(model, "generate"):
|
| 446 |
+
output = model.generate(prompt=test_input, max_length=20)
|
| 447 |
+
logger.info(f"Test inference output: {output}")
|
| 448 |
+
results["test_inference"] = output
|
| 449 |
+
results["success"] = True
|
| 450 |
+
except Exception as inf_err:
|
| 451 |
+
logger.warning(f"Test inference failed: {inf_err}")
|
| 452 |
+
# Still mark success if weights loaded
|
| 453 |
+
results["success"] = True
|
| 454 |
+
else:
|
| 455 |
+
results["error"] = "Failed to load weights into model"
|
| 456 |
+
|
| 457 |
+
except ImportError as e:
|
| 458 |
+
results["error"] = f"ImportError: {str(e)}"
|
| 459 |
+
|
| 460 |
+
return results
|
| 461 |
+
except Exception as e:
|
| 462 |
+
logger.error(f"Error testing weight loading in model: {e}", exc_info=True)
|
| 463 |
+
results["error"] = str(e)
|
| 464 |
+
return results
|
| 465 |
+
|
| 466 |
+
def run_diagnostics() -> Dict[str, Any]:
|
| 467 |
+
"""
|
| 468 |
+
Run diagnostics on the model environment and dependencies.
|
| 469 |
+
|
| 470 |
+
Returns:
|
| 471 |
+
Dictionary with diagnostic results
|
| 472 |
+
"""
|
| 473 |
+
diagnostics = {
|
| 474 |
+
"python_version": sys.version,
|
| 475 |
+
"environment": {},
|
| 476 |
+
"modules": {},
|
| 477 |
+
"gpu_available": False,
|
| 478 |
+
"files_present": {},
|
| 479 |
+
"model_repo_config": None
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
# Check environment variables
|
| 483 |
+
for var in ["MODEL_REPO", "HF_TOKEN", "TLM_TRANSFORMER_WEIGHTS", "TLM_SNN_WEIGHTS",
|
| 484 |
+
"LOW_MEMORY_MODE", "CUDA_VISIBLE_DEVICES"]:
|
| 485 |
+
diagnostics["environment"][var] = os.environ.get(var, "Not set")
|
| 486 |
+
|
| 487 |
+
# Check critical modules
|
| 488 |
+
for module_name in ["torch", "transformers", "adapter_layer", "model_Custm", "model_PrTr",
|
| 489 |
+
"load_model_weights", "model_repo_config"]:
|
| 490 |
+
try:
|
| 491 |
+
module_spec = importlib.util.find_spec(module_name)
|
| 492 |
+
if module_spec is not None:
|
| 493 |
+
try:
|
| 494 |
+
module = importlib.import_module(module_name)
|
| 495 |
+
diagnostics["modules"][module_name] = getattr(module, "__version__", "Available (no version)")
|
| 496 |
+
except Exception as e:
|
| 497 |
+
diagnostics["modules"][module_name] = f"Import error: {e}"
|
| 498 |
+
else:
|
| 499 |
+
diagnostics["modules"][module_name] = "Not found"
|
| 500 |
+
except ImportError:
|
| 501 |
+
diagnostics["modules"][module_name] = "Not available"
|
| 502 |
+
|
| 503 |
+
# Check for GPU
|
| 504 |
+
try:
|
| 505 |
+
import torch
|
| 506 |
+
diagnostics["gpu_available"] = torch.cuda.is_available()
|
| 507 |
+
if diagnostics["gpu_available"]:
|
| 508 |
+
diagnostics["gpu_info"] = torch.cuda.get_device_name(0)
|
| 509 |
+
except:
|
| 510 |
+
pass
|
| 511 |
+
|
| 512 |
+
# Check critical files
|
| 513 |
+
required_files = [
|
| 514 |
+
"adapter_layer.py",
|
| 515 |
+
"model_Custm.py",
|
| 516 |
+
"model_PrTr.py",
|
| 517 |
+
"model_repo_config.py",
|
| 518 |
+
"load_model_weights.py",
|
| 519 |
+
"service_registry.py"
|
| 520 |
+
]
|
| 521 |
+
|
| 522 |
+
for filename in required_files:
|
| 523 |
+
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
|
| 524 |
+
diagnostics["files_present"][filename] = os.path.exists(file_path)
|
| 525 |
+
|
| 526 |
+
# Check model repo config
|
| 527 |
+
try:
|
| 528 |
+
from model_repo_config import get_repo_config
|
| 529 |
+
repo_config = get_repo_config()
|
| 530 |
+
diagnostics["model_repo_config"] = {
|
| 531 |
+
"repo_id": repo_config.repo_id,
|
| 532 |
+
"weight_locations": repo_config.weight_locations[:3] + ["..."], # First few for brevity
|
| 533 |
+
"has_auth_token": repo_config.has_auth_token(),
|
| 534 |
+
"cache_dir": repo_config.cache_dir
|
| 535 |
+
}
|
| 536 |
+
except Exception as e:
|
| 537 |
+
diagnostics["model_repo_config_error"] = str(e)
|
| 538 |
+
|
| 539 |
+
return diagnostics
|
| 540 |
+
|
| 541 |
+
def main():
|
| 542 |
+
"""Main function to parse arguments and run tests"""
|
| 543 |
+
parser = argparse.ArgumentParser(description="Comprehensive Wildnerve TLM Model Test Suite")
|
| 544 |
+
parser.add_argument("--prompt", type=str, default="Tell me about Malaysia's culture",
|
| 545 |
+
help="Prompt text to test (default is non-math to force model loading)")
|
| 546 |
+
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
|
| 547 |
+
parser.add_argument("--check-math", action="store_true", help="Run math capability tests")
|
| 548 |
+
parser.add_argument("--check-weights", action="store_true", help="Test model weight loading")
|
| 549 |
+
parser.add_argument("--verify-repos", action="store_true", help="Verify repository access")
|
| 550 |
+
parser.add_argument("--list-weights", action="store_true", help="List available weight files")
|
| 551 |
+
parser.add_argument("--test-load", action="store_true", help="Test loading weights into model")
|
| 552 |
+
parser.add_argument("--diagnostics", action="store_true", help="Run system diagnostics")
|
| 553 |
+
parser.add_argument("--all", action="store_true", help="Run all tests")
|
| 554 |
+
parser.add_argument("--output", type=str, help="Save results to JSON file")
|
| 555 |
+
args = parser.parse_args()
|
| 556 |
+
|
| 557 |
+
# If --all specified, enable all tests
|
| 558 |
+
if args.all:
|
| 559 |
+
args.check_math = True
|
| 560 |
+
args.check_weights = True
|
| 561 |
+
args.verify_repos = True
|
| 562 |
+
args.list_weights = True
|
| 563 |
+
args.test_load = True
|
| 564 |
+
args.diagnostics = True
|
| 565 |
+
|
| 566 |
+
# Track overall execution time
|
| 567 |
+
start_time = time.time()
|
| 568 |
+
results = {}
|
| 569 |
+
|
| 570 |
+
# Run diagnostics if requested
|
| 571 |
+
if args.diagnostics:
|
| 572 |
+
logger.info("Running system diagnostics...")
|
| 573 |
+
try:
|
| 574 |
+
diagnostics = run_diagnostics()
|
| 575 |
+
results["diagnostics"] = diagnostics
|
| 576 |
+
|
| 577 |
+
if args.verbose:
|
| 578 |
+
logger.info("Diagnostic results:")
|
| 579 |
+
for category, data in diagnostics.items():
|
| 580 |
+
logger.info(f" {category}:")
|
| 581 |
+
if isinstance(data, dict):
|
| 582 |
+
for key, value in data.items():
|
| 583 |
+
logger.info(f" - {key}: {value}")
|
| 584 |
+
else:
|
| 585 |
+
logger.info(f" - {data}")
|
| 586 |
+
except Exception as e:
|
| 587 |
+
logger.error(f"Error in diagnostics: {e}")
|
| 588 |
+
results["diagnostics_error"] = str(e)
|
| 589 |
+
|
| 590 |
+
# Verify repository access if requested
|
| 591 |
+
if args.verify_repos:
|
| 592 |
+
logger.info("Verifying model repository access...")
|
| 593 |
+
try:
|
| 594 |
+
repo_results = verify_repositories()
|
| 595 |
+
results["repository_verification"] = repo_results
|
| 596 |
+
|
| 597 |
+
# Log summary
|
| 598 |
+
logger.info(f"Repositories checked: {repo_results['repositories_checked']}")
|
| 599 |
+
logger.info(f"Repositories accessible: {repo_results['repositories_accessible']}")
|
| 600 |
+
|
| 601 |
+
if args.verbose:
|
| 602 |
+
for repo, details in repo_results["details"].items():
|
| 603 |
+
status = "✓" if details["accessible"] else "✗"
|
| 604 |
+
logger.info(f" {status} {repo}: {details['num_files']} files")
|
| 605 |
+
except Exception as e:
|
| 606 |
+
logger.error(f"Error verifying repositories: {e}")
|
| 607 |
+
results["repository_verification_error"] = str(e)
|
| 608 |
+
|
| 609 |
+
# List weight files if requested
|
| 610 |
+
if args.list_weights:
|
| 611 |
+
logger.info("Listing available weight files...")
|
| 612 |
+
try:
|
| 613 |
+
weight_files = list_weight_files()
|
| 614 |
+
results["weight_files"] = weight_files
|
| 615 |
+
|
| 616 |
+
# Log summary
|
| 617 |
+
logger.info(f"Local weight files found: {len(weight_files['local_weights'])}")
|
| 618 |
+
logger.info(f"Repositories with weights: {len(weight_files['repository_weights'])}")
|
| 619 |
+
|
| 620 |
+
if args.verbose:
|
| 621 |
+
# Show local weights
|
| 622 |
+
if weight_files["local_weights"]:
|
| 623 |
+
logger.info("Local weight files:")
|
| 624 |
+
for weight in weight_files["local_weights"]:
|
| 625 |
+
logger.info(f" - {weight['relative_path']} ({weight['size_mb']:.1f} MB)")
|
| 626 |
+
|
| 627 |
+
# Show repository weights
|
| 628 |
+
for repo, files in weight_files["repository_weights"].items():
|
| 629 |
+
if isinstance(files, list):
|
| 630 |
+
logger.info(f"Weights in {repo}: {len(files)} files")
|
| 631 |
+
for file in files[:5]: # Show first 5
|
| 632 |
+
logger.info(f" - {file}")
|
| 633 |
+
if len(files) > 5:
|
| 634 |
+
logger.info(f" - ... ({len(files)-5} more)")
|
| 635 |
+
except Exception as e:
|
| 636 |
+
logger.error(f"Error listing weight files: {e}")
|
| 637 |
+
results["weight_files_error"] = str(e)
|
| 638 |
+
|
| 639 |
+
# Test weight loading if requested
|
| 640 |
+
if args.check_weights:
|
| 641 |
+
logger.info("Testing model weight loading...")
|
| 642 |
+
try:
|
| 643 |
+
weight_loading = test_weight_loading()
|
| 644 |
+
results["weight_loading"] = weight_loading
|
| 645 |
+
|
| 646 |
+
# Log summary
|
| 647 |
+
if weight_loading["local_weights_found"]:
|
| 648 |
+
logger.info("✓ Local weights found")
|
| 649 |
+
for key, path in weight_loading["weight_files"].items():
|
| 650 |
+
if path:
|
| 651 |
+
logger.info(f" - {key}: {path}")
|
| 652 |
+
elif weight_loading["downloaded_weights"]:
|
| 653 |
+
logger.info("✓ Weights downloaded successfully")
|
| 654 |
+
for key, path in weight_loading["weight_files"].items():
|
| 655 |
+
if path:
|
| 656 |
+
logger.info(f" - {key}: {path}")
|
| 657 |
+
else:
|
| 658 |
+
logger.warning("✗ No weights found or downloaded")
|
| 659 |
+
if weight_loading["errors"]:
|
| 660 |
+
for error in weight_loading["errors"]:
|
| 661 |
+
logger.warning(f" - Error: {error}")
|
| 662 |
+
except Exception as e:
|
| 663 |
+
logger.error(f"Error testing weight loading: {e}")
|
| 664 |
+
results["weight_loading_error"] = str(e)
|
| 665 |
+
|
| 666 |
+
# Test loading weights into model if requested
|
| 667 |
+
if args.test_load:
|
| 668 |
+
logger.info("Testing loading weights into model...")
|
| 669 |
+
try:
|
| 670 |
+
weight_in_model = test_weight_loading_in_model()
|
| 671 |
+
results["weight_in_model"] = weight_in_model
|
| 672 |
+
|
| 673 |
+
# Log summary
|
| 674 |
+
if weight_in_model["success"]:
|
| 675 |
+
logger.info("✓ Successfully loaded weights into model")
|
| 676 |
+
logger.info(f" - Weight path: {weight_in_model['weight_path']}")
|
| 677 |
+
if "test_inference" in weight_in_model:
|
| 678 |
+
logger.info(f" - Test inference: {weight_in_model['test_inference'][:50]}...")
|
| 679 |
+
else:
|
| 680 |
+
logger.warning("✗ Failed to load weights into model")
|
| 681 |
+
if weight_in_model["error"]:
|
| 682 |
+
logger.warning(f" - Error: {weight_in_model['error']}")
|
| 683 |
+
except Exception as e:
|
| 684 |
+
logger.error(f"Error testing weights in model: {e}")
|
| 685 |
+
results["weight_in_model_error"] = str(e)
|
| 686 |
+
|
| 687 |
+
# Test model loading with the provided prompt
|
| 688 |
+
logger.info(f"Testing model loading with prompt: {args.prompt}")
|
| 689 |
+
loading_results = test_model_loading(args.prompt, args.verbose)
|
| 690 |
+
results["model_loading"] = loading_results
|
| 691 |
+
|
| 692 |
+
# Summary of model loading test
|
| 693 |
+
if loading_results["success"]:
|
| 694 |
+
if loading_results["model_loaded"]:
|
| 695 |
+
logger.info("✅ SUCCESS: Model loaded and generated response")
|
| 696 |
+
logger.info(f" - Response: {loading_results['response'][:50]}...")
|
| 697 |
+
logger.info(f" - Time: {loading_results['elapsed_time']:.2f} seconds")
|
| 698 |
+
else:
|
| 699 |
+
logger.warning("⚠️ PARTIAL: Model adapter works but uses fallback (not neural network)")
|
| 700 |
+
logger.warning(f" - Fallback response: {loading_results['response'][:50]}...")
|
| 701 |
+
else:
|
| 702 |
+
logger.error("❌ FAILED: Could not load the model")
|
| 703 |
+
if loading_results["error"]:
|
| 704 |
+
logger.error(f" - Error: {loading_results['error']}")
|
| 705 |
+
|
| 706 |
+
# Run math tests if requested
|
| 707 |
+
if args.check_math:
|
| 708 |
+
logger.info("Running math capability tests...")
|
| 709 |
+
math_results = test_math_capability()
|
| 710 |
+
results["math_tests"] = math_results
|
| 711 |
+
|
| 712 |
+
# Summary of math tests
|
| 713 |
+
if math_results["success"]:
|
| 714 |
+
logger.info(f"Math tests: {math_results['tests_passed']}/{math_results['tests_total']} passed")
|
| 715 |
+
|
| 716 |
+
if args.verbose:
|
| 717 |
+
for i, test in enumerate(math_results["details"]):
|
| 718 |
+
status = "✓" if test.get("passed") else "✗"
|
| 719 |
+
logger.info(f" {status} Test {i+1}: {test['prompt']}")
|
| 720 |
+
if not test.get("passed"):
|
| 721 |
+
logger.info(f" Expected: {test.get('expected')}")
|
| 722 |
+
logger.info(f" Got: {test.get('response', '')[:50]}...")
|
| 723 |
+
else:
|
| 724 |
+
logger.error("Failed to run math tests")
|
| 725 |
+
if "error" in math_results:
|
| 726 |
+
logger.error(f" - Error: {math_results['error']}")
|
| 727 |
+
|
| 728 |
+
# Log total execution time
|
| 729 |
+
elapsed = time.time() - start_time
|
| 730 |
+
logger.info(f"All tests completed in {elapsed:.2f} seconds")
|
| 731 |
+
|
| 732 |
+
# Save results if requested
|
| 733 |
+
if args.output:
|
| 734 |
+
try:
|
| 735 |
+
import json
|
| 736 |
+
with open(args.output, 'w') as f:
|
| 737 |
+
json.dump(results, f, indent=2)
|
| 738 |
+
logger.info(f"Results saved to {args.output}")
|
| 739 |
+
except Exception as e:
|
| 740 |
+
logger.error(f"Failed to save results: {e}")
|
| 741 |
+
|
| 742 |
+
if __name__ == "__main__":
|
| 743 |
+
main()
|