Upload 2 files
Browse files- adapter_layer.py +62 -160
- test_model.py +743 -0
adapter_layer.py
CHANGED
|
@@ -146,161 +146,41 @@ class Wildnerve_tlm01(nn.Module):
|
|
| 146 |
def generate(self, text_input, max_length=None, **kwargs):
|
| 147 |
"""Generate text - with lazy model loading"""
|
| 148 |
try:
|
| 149 |
-
#
|
| 150 |
-
import re
|
| 151 |
-
|
| 152 |
-
# Pattern 1: Digit format (4 * 3)
|
| 153 |
-
digit_pattern = re.compile(r'(\d+)\s*([+\-x*/])\s*(\d+)\s*=?')
|
| 154 |
-
digit_match = digit_pattern.search(text_input.lower())
|
| 155 |
-
|
| 156 |
-
# Pattern 2: Word format (Four multiplied by three)
|
| 157 |
-
word_numbers = {
|
| 158 |
-
'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
|
| 159 |
-
'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10,
|
| 160 |
-
'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15,
|
| 161 |
-
'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20
|
| 162 |
-
}
|
| 163 |
-
|
| 164 |
-
word_operators = {
|
| 165 |
-
'plus': '+', 'add': '+', 'added to': '+', 'sum': '+',
|
| 166 |
-
'minus': '-', 'subtract': '-', 'subtracted from': '-', 'difference': '-',
|
| 167 |
-
'times': '*', 'multiplied by': '*', 'product': '*', 'multiply': '*',
|
| 168 |
-
'divided by': '/', 'divide': '/', 'quotient': '/'
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
word_pattern = re.compile(
|
| 172 |
-
r'(zero|one|two|three|four|five|six|seven|eight|nine|ten|'
|
| 173 |
-
r'eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)'
|
| 174 |
-
r'\s+(plus|add|added to|sum|minus|subtract|subtracted from|difference|times|'
|
| 175 |
-
r'multiplied by|product|multiply|divided by|divide|quotient)\s+'
|
| 176 |
-
r'(zero|one|two|three|four|five|six|seven|eight|nine|ten|'
|
| 177 |
-
r'eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)',
|
| 178 |
-
re.IGNORECASE
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
word_match = word_pattern.search(text_input.lower())
|
| 182 |
-
|
| 183 |
-
# Handle digit format match
|
| 184 |
-
if digit_match:
|
| 185 |
-
num1 = int(digit_match.group(1))
|
| 186 |
-
operator = digit_match.group(2)
|
| 187 |
-
num2 = int(digit_match.group(3))
|
| 188 |
-
|
| 189 |
-
result = None
|
| 190 |
-
if operator == '+':
|
| 191 |
-
result = num1 + num2
|
| 192 |
-
elif operator == '-':
|
| 193 |
-
result = num1 - num2
|
| 194 |
-
elif operator in ['x', '*']:
|
| 195 |
-
result = num1 * num2
|
| 196 |
-
elif operator == '/':
|
| 197 |
-
result = num1 / num2 if num2 != 0 else "Cannot divide by zero"
|
| 198 |
-
|
| 199 |
-
if result is not None:
|
| 200 |
-
math_response = f"The answer to {num1} {operator} {num2} = {result}"
|
| 201 |
-
logger.info(f"Providing direct math answer: {math_response}")
|
| 202 |
-
return math_response
|
| 203 |
-
|
| 204 |
-
# Handle word format match
|
| 205 |
-
elif word_match:
|
| 206 |
-
num1_word = word_match.group(1).lower()
|
| 207 |
-
operator_word = word_match.group(2).lower()
|
| 208 |
-
num2_word = word_match.group(3).lower()
|
| 209 |
-
|
| 210 |
-
num1 = word_numbers.get(num1_word)
|
| 211 |
-
operator = None
|
| 212 |
-
for op_word, op_symbol in word_operators.items():
|
| 213 |
-
if op_word in operator_word:
|
| 214 |
-
operator = op_symbol
|
| 215 |
-
break
|
| 216 |
-
num2 = word_numbers.get(num2_word)
|
| 217 |
-
|
| 218 |
-
if num1 is not None and operator is not None and num2 is not None:
|
| 219 |
-
result = None
|
| 220 |
-
if operator == '+':
|
| 221 |
-
result = num1 + num2
|
| 222 |
-
elif operator == '-':
|
| 223 |
-
result = num1 - num2
|
| 224 |
-
elif operator == '*':
|
| 225 |
-
result = num1 * num2
|
| 226 |
-
elif operator == '/':
|
| 227 |
-
result = num1 / num2 if num2 != 0 else "Cannot divide by zero"
|
| 228 |
-
|
| 229 |
-
if result is not None:
|
| 230 |
-
math_response = f"The answer to {num1_word} {operator_word} {num2_word} is {result}"
|
| 231 |
-
logger.info(f"Providing direct math word answer: {math_response}")
|
| 232 |
-
return math_response
|
| 233 |
-
|
| 234 |
-
# Also check for common math question patterns
|
| 235 |
-
math_question_terms = [
|
| 236 |
-
"what is", "calculate", "solve", "find", "result of", "answer to",
|
| 237 |
-
"equals", "equal to"
|
| 238 |
-
]
|
| 239 |
-
|
| 240 |
-
if any(term in text_input.lower() for term in math_question_terms):
|
| 241 |
-
# Simple checks for common patterns like "what is 4 times 3" or "what is four times three"
|
| 242 |
-
for number_word, number_value in word_numbers.items():
|
| 243 |
-
for op_word, op_symbol in word_operators.items():
|
| 244 |
-
pattern = f"{number_word}.*{op_word}.*{number_word}"
|
| 245 |
-
if re.search(pattern, text_input.lower()):
|
| 246 |
-
# Extract the specific numbers and operator
|
| 247 |
-
match = re.search(
|
| 248 |
-
f"({number_word}).*({op_word}).*({number_word})",
|
| 249 |
-
text_input.lower()
|
| 250 |
-
)
|
| 251 |
-
if match:
|
| 252 |
-
num1 = word_numbers.get(match.group(1))
|
| 253 |
-
num2 = word_numbers.get(match.group(3))
|
| 254 |
-
operator = op_symbol
|
| 255 |
-
|
| 256 |
-
if num1 is not None and num2 is not None:
|
| 257 |
-
result = None
|
| 258 |
-
if operator == '+':
|
| 259 |
-
result = num1 + num2
|
| 260 |
-
elif operator == '-':
|
| 261 |
-
result = num1 - num2
|
| 262 |
-
elif operator == '*':
|
| 263 |
-
result = num1 * num2
|
| 264 |
-
elif operator == '/':
|
| 265 |
-
result = num1 / num2 if num2 != 0 else "Cannot divide by zero"
|
| 266 |
-
|
| 267 |
-
if result is not None:
|
| 268 |
-
math_response = f"The answer is {result}"
|
| 269 |
-
logger.info(f"Providing direct math answer from question: {math_response}")
|
| 270 |
-
return math_response
|
| 271 |
-
|
| 272 |
-
# Special case for "Four multiplied by three"
|
| 273 |
-
if "four multiplied by three" in text_input.lower():
|
| 274 |
-
return "The answer is 12"
|
| 275 |
-
|
| 276 |
-
# Try to load model on first use
|
| 277 |
if not self.model_loaded:
|
|
|
|
| 278 |
self._lazy_load_model()
|
| 279 |
|
| 280 |
-
#
|
| 281 |
if self.model:
|
| 282 |
try:
|
| 283 |
-
logger.info(f"
|
| 284 |
-
|
| 285 |
prompt=text_input,
|
| 286 |
max_length=max_length,
|
| 287 |
**kwargs
|
| 288 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
except Exception as e:
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
-
#
|
| 294 |
-
if self.tokenizer
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
return f"Processing: {text_input[:50]}..."
|
| 298 |
-
|
| 299 |
-
# If no model or tokenizer, return simple response
|
| 300 |
-
return f"I've received your input about '{text_input[:30]}...'"
|
| 301 |
|
| 302 |
except Exception as e:
|
| 303 |
-
logger.error(f"
|
| 304 |
return f"An error occurred processing your request: {str(e)}"
|
| 305 |
|
| 306 |
def _lazy_load_model(self):
|
|
@@ -311,28 +191,50 @@ class Wildnerve_tlm01(nn.Module):
|
|
| 311 |
# First initialize tokenizer if not already done
|
| 312 |
self._initialize_minimal_tokenizer()
|
| 313 |
|
| 314 |
-
# Download and load model weights first
|
| 315 |
-
from load_model_weights import download_model_files, load_weights_into_model, verify_token
|
| 316 |
-
|
| 317 |
-
# First verify token is available
|
| 318 |
-
token_verified = verify_token()
|
| 319 |
-
logger.info(f"HF Token verification: {token_verified}")
|
| 320 |
-
|
| 321 |
-
# Get weights from HF repository with more robust error reporting
|
| 322 |
-
logger.info("Downloading model weights...")
|
| 323 |
try:
|
| 324 |
-
|
| 325 |
|
| 326 |
-
#
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
weight_files = {}
|
| 335 |
-
|
|
|
|
| 336 |
# Try to load model_Custm first
|
| 337 |
if "model_Custm" in self.available_models:
|
| 338 |
try:
|
|
|
|
| 146 |
def generate(self, text_input, max_length=None, **kwargs):
|
| 147 |
"""Generate text - with lazy model loading"""
|
| 148 |
try:
|
| 149 |
+
# 1. Load model if not already loaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if not self.model_loaded:
|
| 151 |
+
logger.info("Loading model for first request")
|
| 152 |
self._lazy_load_model()
|
| 153 |
|
| 154 |
+
# 2. Let the model handle inference directly with NO pattern matching or rules
|
| 155 |
if self.model:
|
| 156 |
try:
|
| 157 |
+
logger.info(f"Sending prompt directly to neural model: {type(self.model).__name__}")
|
| 158 |
+
model_response = self.model.generate(
|
| 159 |
prompt=text_input,
|
| 160 |
max_length=max_length,
|
| 161 |
**kwargs
|
| 162 |
)
|
| 163 |
+
|
| 164 |
+
# Log response for debugging but don't intercept or alter it
|
| 165 |
+
logger.info(f"Model generated response of length {len(model_response) if isinstance(model_response, str) else 'unknown'}")
|
| 166 |
+
|
| 167 |
+
# Return the raw model response - let the model shine (or fail naturally)
|
| 168 |
+
return model_response
|
| 169 |
+
|
| 170 |
except Exception as e:
|
| 171 |
+
# Only log the error but don't substitute with rule-based responses
|
| 172 |
+
logger.error(f"Neural model inference error: {e}")
|
| 173 |
+
# Continue to basic fallback only if the model completely failed
|
| 174 |
+
else:
|
| 175 |
+
logger.warning("No model available - only basic response possible")
|
| 176 |
|
| 177 |
+
# 3. Minimal fallback ONLY if model couldn't be loaded or threw exception
|
| 178 |
+
if self.tokenizer:
|
| 179 |
+
return f"The model couldn't be properly initialized. Your input: '{text_input[:30]}...'"
|
| 180 |
+
return f"No language model available to process: '{text_input[:30]}...'"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
+
logger.error(f"Critical error in generate method: {e}")
|
| 184 |
return f"An error occurred processing your request: {str(e)}"
|
| 185 |
|
| 186 |
def _lazy_load_model(self):
|
|
|
|
| 191 |
# First initialize tokenizer if not already done
|
| 192 |
self._initialize_minimal_tokenizer()
|
| 193 |
|
| 194 |
+
# Download and load model weights first with better logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
try:
|
| 196 |
+
from load_model_weights import download_model_files, load_weights_into_model, verify_token
|
| 197 |
|
| 198 |
+
# First verify token is available
|
| 199 |
+
token_verified = verify_token()
|
| 200 |
+
logger.info(f"HF Token verification: {token_verified}")
|
| 201 |
+
|
| 202 |
+
# Get weights from HF repository with more robust error reporting
|
| 203 |
+
logger.info("Downloading model weights...")
|
| 204 |
+
try:
|
| 205 |
+
# Try multiple repositories in priority order
|
| 206 |
+
repositories = [
|
| 207 |
+
"EvolphTech/Weights",
|
| 208 |
+
"Wildnerve/tlm-0.05Bx12",
|
| 209 |
+
"Wildnerve/tlm",
|
| 210 |
+
"EvolphTech/Checkpoints"
|
| 211 |
+
]
|
| 212 |
|
| 213 |
+
weight_files = None
|
| 214 |
+
for repo in repositories:
|
| 215 |
+
logger.info(f"Attempting to download weights from {repo}...")
|
| 216 |
+
try:
|
| 217 |
+
weight_files = download_model_files(repo_id_base=repo)
|
| 218 |
+
if weight_files and "transformer" in weight_files:
|
| 219 |
+
logger.info(f"Successfully downloaded weights from {repo}")
|
| 220 |
+
break
|
| 221 |
+
except Exception as repo_error:
|
| 222 |
+
logger.warning(f"Failed to download from {repo}: {repo_error}")
|
| 223 |
+
|
| 224 |
+
# Add detailed logging about weight files
|
| 225 |
+
if weight_files:
|
| 226 |
+
logger.info(f"Download returned {len(weight_files)} weight files: {list(weight_files.keys())}")
|
| 227 |
+
else:
|
| 228 |
+
logger.warning("No weight files were returned from download_model_files")
|
| 229 |
+
|
| 230 |
+
except Exception as e:
|
| 231 |
+
logger.error(f"Error downloading weights: {str(e)}")
|
| 232 |
+
weight_files = {}
|
| 233 |
+
except ImportError:
|
| 234 |
+
logger.error("Could not import load_model_weights - missing dependencies?")
|
| 235 |
weight_files = {}
|
| 236 |
+
|
| 237 |
+
# Rest of model loading code (unchanged)
|
| 238 |
# Try to load model_Custm first
|
| 239 |
if "model_Custm" in self.available_models:
|
| 240 |
try:
|
test_model.py
ADDED
|
@@ -0,0 +1,743 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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()
|