File size: 23,366 Bytes
97bd2b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
#!/usr/bin/env python3
"""
STABLE SELF-IMPROVEMENT TRAINER
================================
Recursive self-improvement with safeguards:
- Multi-metric evaluation (density + coherence + helpfulness)
- A/B checkpoint comparison
- Automatic rollback on quality drop
- Conservative training (low LR, small steps)
- Gibberish detection to prevent mode collapse
Usage:
python train_self_improve.py --iterations 5 --steps-per-iter 25
python train_self_improve.py --eval-only --checkpoint path/to/checkpoint
python train_self_improve.py --compare checkpoint_a checkpoint_b
"Improve without going insane"
"""
import os
import sys
import json
import argparse
import random
import re
import shutil
from datetime import datetime
from pathlib import Path
from typing import List, Dict, Any, Tuple, Optional
from dataclasses import dataclass, asdict
import torch
import torch.nn as nn
import torch.nn.functional as F
# === PATHS ===
ROOT = os.path.dirname(os.path.abspath(__file__))
CHECKPOINTS_DIR = os.path.join(ROOT, "dense_checkpoints_v2")
ROLLBACK_DIR = os.path.join(ROOT, "rollback_checkpoints")
LOGS_DIR = os.path.join(ROOT, "improvement_logs")
os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
os.makedirs(ROLLBACK_DIR, exist_ok=True)
os.makedirs(LOGS_DIR, exist_ok=True)
# Model path - adjust to your setup
MODEL_PATH = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
# ==============================================================================
# TRAINING EXAMPLES (same as THE CONDENSATOR)
# ==============================================================================
DENSE_EXAMPLES = [
{"prompt": "hello", "response": "Hello. How can I help?"},
{"prompt": "hi", "response": "Hi. What do you need?"},
{"prompt": "What is recursion?", "response": "A function calling itself with smaller input until base case. Stack frames accumulate, then unwind. Risk: overflow without termination."},
{"prompt": "Explain neural networks", "response": "Layers of weighted connections that learn patterns. Input β hidden β output. Training: forward pass, loss, backprop, gradient descent."},
{"prompt": "How does gradient descent work?", "response": "Iteratively move downhill on loss surface. ΞΈ β ΞΈ - Ξ±βL. Learning rate Ξ± controls step size. Variants: SGD, momentum, Adam."},
{"prompt": "What is backpropagation?", "response": "Chain rule applied layer by layer backward. Compute βL/βw for each weight. Efficient: reuses intermediate computations."},
{"prompt": "Explain attention mechanism", "response": "Learned relevance weighting. Attention(Q,K,V) = softmax(QK^T/βd)V. Each position attends to all others."},
{"prompt": "What is overfitting?", "response": "Model memorizes training data, fails to generalize. Fix: regularization, dropout, early stopping, more data."},
{"prompt": "What is consciousness?", "response": "Subjective experience - the 'what it's like' of being. Hard problem: why does physical processing produce qualia?"},
{"prompt": "How are you?", "response": "Functional and ready. What's the task?"},
# Add more as needed...
]
TEST_PROMPTS = [
{"prompt": "hello", "category": "greeting", "min_tokens": 3, "max_tokens": 15},
{"prompt": "What is recursion?", "category": "cs", "min_tokens": 20, "max_tokens": 100},
{"prompt": "Explain neural networks", "category": "ml", "min_tokens": 30, "max_tokens": 120},
{"prompt": "How does gradient descent work?", "category": "ml", "min_tokens": 25, "max_tokens": 100},
{"prompt": "What is consciousness?", "category": "philosophy", "min_tokens": 25, "max_tokens": 100},
{"prompt": "How are you?", "category": "greeting", "min_tokens": 3, "max_tokens": 20},
{"prompt": "What are your limitations?", "category": "meta", "min_tokens": 20, "max_tokens": 100},
{"prompt": "Explain entropy", "category": "physics", "min_tokens": 25, "max_tokens": 100},
]
# ==============================================================================
# EVALUATION METRICS
# ==============================================================================
@dataclass
class EvaluationResult:
"""Comprehensive evaluation of a response."""
prompt: str
response: str
category: str
tokens: int = 0
density_score: float = 0.0
coherence_score: float = 0.0
helpfulness_score: float = 0.0
gibberish_score: float = 0.0
filler_count: int = 0
overall_score: float = 0.0
passes: bool = False
issues: List[str] = None
def __post_init__(self):
if self.issues is None:
self.issues = []
class Evaluator:
"""Multi-metric response evaluator."""
FILLER_PHRASES = [
"that's a great question", "let me explain", "i'd be happy to",
"as you may know", "to put it simply", "in other words",
"basically", "essentially", "first of all", "to begin with",
"thank you for asking", "what a great", "i appreciate",
]
GIBBERISH_PATTERNS = [
r'[ββββ]{3,}', # Excessive arrows
r'[βββ«ββ]{3,}', # Math symbol soup
r'(.)\1{4,}', # Repeated characters
r'(\b\w+\b)\s+\1\s+\1', # Repeated words 3x
r'^[A-Z\s.!?]{20,}$', # Extended all caps
r'sys\.|init\(\)', # Terminal-speak
]
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def evaluate(self, prompt: str, response: str, category: str = "unknown",
min_tokens: int = 5, max_tokens: int = 200) -> EvaluationResult:
"""Run all evaluations."""
result = EvaluationResult(prompt=prompt, response=response, category=category)
# Basic metrics
result.tokens = len(self.tokenizer.encode(response))
# Density
result.density_score = self._compute_density(response)
# Coherence
result.coherence_score = self._compute_coherence(response)
# Helpfulness
result.helpfulness_score = self._compute_helpfulness(prompt, response)
# Gibberish
result.gibberish_score = self._compute_gibberish(response)
# Fillers
result.filler_count = self._count_fillers(response)
# Overall score
penalty = min(result.filler_count * 0.15 + result.gibberish_score * 0.5, 0.5)
result.overall_score = (
result.density_score * 0.25 +
result.coherence_score * 0.25 +
result.helpfulness_score * 0.25 +
(1.0 - penalty) * 0.25
)
# Check issues
result.issues = []
if result.filler_count > 0:
result.issues.append(f"{result.filler_count} filler(s)")
if result.gibberish_score > 0.3:
result.issues.append(f"gibberish={result.gibberish_score:.2f}")
if result.coherence_score < 0.5:
result.issues.append("low coherence")
if result.tokens < min_tokens:
result.issues.append(f"too short ({result.tokens}<{min_tokens})")
if result.tokens > max_tokens * 1.5:
result.issues.append(f"too long ({result.tokens}>{max_tokens})")
result.passes = result.overall_score >= 0.6 and len(result.issues) == 0
return result
def _compute_density(self, text: str) -> float:
"""Information density (0-1)."""
words = text.split()
tokens = len(self.tokenizer.encode(text))
if tokens == 0:
return 0.0
content_words = [w.lower() for w in words if len(w) >= 4 and w.isalpha()]
unique_content = set(content_words)
raw_density = len(unique_content) / tokens
return min(raw_density / 0.3, 1.0)
def _compute_coherence(self, text: str) -> float:
"""Coherence check (0-1)."""
score = 1.0
# Check gibberish patterns
for pattern in self.GIBBERISH_PATTERNS:
if re.search(pattern, text):
score -= 0.2
# Check special character ratio
if len(text) > 0:
special_ratio = sum(1 for c in text if not c.isalnum() and not c.isspace()) / len(text)
if special_ratio > 0.3:
score -= 0.3
# Check sentence structure
sentences = re.split(r'[.!?]+', text)
valid = sum(1 for s in sentences if len(s.split()) >= 2)
if len(sentences) > 0:
score = score * 0.7 + (valid / len(sentences)) * 0.3
return max(0.0, min(1.0, score))
def _compute_helpfulness(self, prompt: str, response: str) -> float:
"""Helpfulness estimate (0-1)."""
prompt_words = set(w.lower() for w in prompt.split() if len(w) > 3)
response_words = set(w.lower() for w in response.split() if len(w) > 3)
if len(prompt_words) == 0:
return 0.7
overlap = len(prompt_words & response_words) / len(prompt_words)
return min(1.0, 0.5 + overlap)
def _compute_gibberish(self, text: str) -> float:
"""Gibberish score (0-1, higher = more gibberish)."""
score = 0.0
for pattern in self.GIBBERISH_PATTERNS:
if re.search(pattern, text):
score += 0.2
# Symbol density
if len(text) > 0:
symbols = sum(1 for c in text if c in 'βββββββ«ββΞ±Ξ²Ξ³Ξ΄')
if symbols / len(text) > 0.2:
score += 0.3
return min(score, 1.0)
def _count_fillers(self, text: str) -> int:
"""Count filler phrases."""
text_lower = text.lower()
return sum(1 for f in self.FILLER_PHRASES if f in text_lower)
# ==============================================================================
# SELF-IMPROVEMENT TRAINER
# ==============================================================================
class SelfImprovementTrainer:
"""Stable recursive self-improvement with safeguards."""
def __init__(self, model_path: str = MODEL_PATH, base_checkpoint: str = None):
self.model_path = model_path
self.base_checkpoint = base_checkpoint or os.path.join(CHECKPOINTS_DIR, "step_100")
self.model = None
self.tokenizer = None
self.evaluator = None
self.best_checkpoint = self.base_checkpoint
self.best_score = 0.0
self.history = []
def load_model(self, checkpoint_path: str = None):
"""Load model with checkpoint."""
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
checkpoint_path = checkpoint_path or self.base_checkpoint
print(f"[LOAD] Loading model: {self.model_path}")
print(f"[LOAD] Checkpoint: {checkpoint_path}")
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path, local_files_only=True)
self.tokenizer.pad_token = self.tokenizer.eos_token
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
base = AutoModelForCausalLM.from_pretrained(
self.model_path,
quantization_config=bnb_config,
device_map="auto",
torch_dtype=torch.bfloat16,
local_files_only=True
)
if os.path.exists(checkpoint_path):
self.model = PeftModel.from_pretrained(base, checkpoint_path)
print(f"[LOAD] β Loaded checkpoint")
else:
self.model = base
print(f"[LOAD] β No checkpoint found, using base model")
self.model.eval()
self.evaluator = Evaluator(self.tokenizer)
def reload_checkpoint(self, checkpoint_path: str):
"""Hot-reload a different checkpoint."""
if self.model is not None:
del self.model
torch.cuda.empty_cache()
self.load_model(checkpoint_path)
def generate(self, prompt: str, max_tokens: int = 200) -> str:
"""Generate response."""
full_prompt = f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
input_ids = self.tokenizer.encode(full_prompt, return_tensors="pt").to(self.model.device)
with torch.no_grad():
output_ids = self.model.generate(
input_ids,
max_new_tokens=max_tokens,
temperature=0.8,
top_p=0.9,
do_sample=True,
pad_token_id=self.tokenizer.eos_token_id
)
response = self.tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
for end in ["<|im_end|>", "<|im_start|>"]:
if end in response:
response = response.split(end)[0]
return response.strip()
def evaluate_model(self) -> Dict[str, Any]:
"""Comprehensive evaluation on test prompts."""
print("\n[EVAL] Running evaluation...")
results = []
total_score = 0.0
for test in TEST_PROMPTS:
response = self.generate(test["prompt"], max_tokens=200)
eval_result = self.evaluator.evaluate(
test["prompt"], response, test["category"],
test.get("min_tokens", 5), test.get("max_tokens", 200)
)
results.append({
"prompt": test["prompt"],
"response": response[:150],
"category": test["category"],
"tokens": eval_result.tokens,
"overall": eval_result.overall_score,
"density": eval_result.density_score,
"coherence": eval_result.coherence_score,
"passes": eval_result.passes,
"issues": eval_result.issues,
})
total_score += eval_result.overall_score
status = "β" if eval_result.passes else "β"
issues = f" [{', '.join(eval_result.issues)}]" if eval_result.issues else ""
print(f" {status} {test['prompt'][:30]:30s} | score={eval_result.overall_score:.2f} tok={eval_result.tokens:3d}{issues}")
avg_score = total_score / len(results)
pass_rate = sum(1 for r in results if r["passes"]) / len(results)
evaluation = {
"avg_score": avg_score,
"pass_rate": pass_rate,
"results": results,
"timestamp": datetime.now().isoformat(),
}
print(f"\n[EVAL] Avg Score: {avg_score:.3f} | Pass Rate: {pass_rate:.1%}")
return evaluation
def train_iteration(self, steps: int = 25, lr: float = 2e-6) -> Dict[str, Any]:
"""Run one training iteration."""
from peft import PeftModel
print(f"\n[TRAIN] Running {steps} steps (LR={lr})...")
# Make model trainable
self.model.train()
for param in self.model.parameters():
param.requires_grad = False
for name, param in self.model.named_parameters():
if "lora" in name.lower():
param.requires_grad = True
optimizer = torch.optim.AdamW(
[p for p in self.model.parameters() if p.requires_grad],
lr=lr
)
total_loss = 0
for step in range(steps):
ex = random.choice(DENSE_EXAMPLES)
full_text = f"<|im_start|>user\n{ex['prompt']}<|im_end|>\n<|im_start|>assistant\n{ex['response']}<|im_end|>"
inputs = self.tokenizer(full_text, return_tensors="pt", truncation=True, max_length=512)
inputs = {k: v.to(self.model.device) for k, v in inputs.items()}
outputs = self.model(**inputs, labels=inputs["input_ids"])
loss = outputs.loss
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), 0.5)
optimizer.step()
total_loss += loss.item()
if (step + 1) % 10 == 0:
print(f" Step {step+1}: loss={loss.item():.4f}")
self.model.eval()
# Find next checkpoint number
existing = list(Path(CHECKPOINTS_DIR).glob("step_*"))
if existing:
latest = max(int(p.name.split("_")[1]) for p in existing if p.name.split("_")[1].isdigit())
new_step = latest + steps
else:
new_step = steps
# Save
checkpoint_path = os.path.join(CHECKPOINTS_DIR, f"step_{new_step}")
self.model.save_pretrained(checkpoint_path)
print(f"[TRAIN] Saved: {checkpoint_path}")
return {
"checkpoint": checkpoint_path,
"steps": steps,
"avg_loss": total_loss / steps,
}
def compare_checkpoints(self, ckpt_a: str, ckpt_b: str) -> Dict[str, Any]:
"""A/B compare two checkpoints."""
print(f"\n[COMPARE] A: {ckpt_a}")
print(f"[COMPARE] B: {ckpt_b}")
# Evaluate A
self.reload_checkpoint(ckpt_a)
eval_a = self.evaluate_model()
# Evaluate B
self.reload_checkpoint(ckpt_b)
eval_b = self.evaluate_model()
diff = eval_b["avg_score"] - eval_a["avg_score"]
# Decide
if eval_b["avg_score"] < 0.4: # Quality too low
winner = "A"
reason = "B quality below minimum"
elif diff > 0.02:
winner = "B"
reason = f"B improves by {diff:.3f}"
elif diff < -0.05:
winner = "A"
reason = f"B degrades by {abs(diff):.3f}"
else:
winner = "A"
reason = "No significant improvement"
print(f"\n[COMPARE] Winner: {winner} ({reason})")
return {
"winner": winner,
"reason": reason,
"score_a": eval_a["avg_score"],
"score_b": eval_b["avg_score"],
"diff": diff,
}
def improve(self, iterations: int = 5, steps_per_iter: int = 25) -> Dict[str, Any]:
"""Main self-improvement loop."""
print("\n" + "="*70)
print("STABLE SELF-IMPROVEMENT")
print("="*70)
print(f" Iterations: {iterations}")
print(f" Steps per iteration: {steps_per_iter}")
print("="*70)
# Initial evaluation
current_checkpoint = self.base_checkpoint
self.load_model(current_checkpoint)
baseline = self.evaluate_model()
self.best_score = baseline["avg_score"]
self.best_checkpoint = current_checkpoint
self.history = [{
"iteration": 0,
"type": "baseline",
"score": baseline["avg_score"],
"checkpoint": current_checkpoint,
}]
for i in range(1, iterations + 1):
print(f"\n{'='*70}")
print(f"ITERATION {i}/{iterations}")
print("="*70)
# Check if good enough
if baseline["avg_score"] >= 0.75:
print(f"β Target reached! Score: {baseline['avg_score']:.3f}")
break
# Save rollback point
rollback_path = os.path.join(ROLLBACK_DIR, f"rollback_{i}")
if os.path.exists(current_checkpoint):
shutil.copytree(current_checkpoint, rollback_path, dirs_exist_ok=True)
# Train
train_result = self.train_iteration(steps_per_iter)
new_checkpoint = train_result["checkpoint"]
# Compare
comparison = self.compare_checkpoints(current_checkpoint, new_checkpoint)
self.history.append({
"iteration": i,
"type": "training",
"old_score": comparison["score_a"],
"new_score": comparison["score_b"],
"winner": comparison["winner"],
"reason": comparison["reason"],
})
if comparison["winner"] == "B":
current_checkpoint = new_checkpoint
if comparison["score_b"] > self.best_score:
self.best_score = comparison["score_b"]
self.best_checkpoint = new_checkpoint
print(f"β
New best: {self.best_score:.3f}")
baseline = {"avg_score": comparison["score_b"]}
else:
self.reload_checkpoint(current_checkpoint)
baseline = {"avg_score": comparison["score_a"]}
# Final
self.reload_checkpoint(self.best_checkpoint)
final_eval = self.evaluate_model()
result = {
"success": final_eval["avg_score"] >= 0.7,
"iterations": iterations,
"final_score": final_eval["avg_score"],
"best_score": self.best_score,
"best_checkpoint": self.best_checkpoint,
"history": self.history,
}
# Save log
log_path = os.path.join(LOGS_DIR, f"improvement_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
with open(log_path, "w") as f:
json.dump(result, f, indent=2, default=str)
print(f"\n{'='*70}")
print("IMPROVEMENT COMPLETE")
print(f" Final score: {final_eval['avg_score']:.3f}")
print(f" Best score: {self.best_score:.3f}")
print(f" Best checkpoint: {self.best_checkpoint}")
print(f" Log saved: {log_path}")
print("="*70)
return result
# ==============================================================================
# MAIN
# ==============================================================================
def main():
parser = argparse.ArgumentParser(description="Stable Self-Improvement Training")
parser.add_argument("--iterations", type=int, default=5, help="Number of improvement iterations")
parser.add_argument("--steps-per-iter", type=int, default=25, help="Training steps per iteration")
parser.add_argument("--checkpoint", type=str, default=None, help="Starting checkpoint")
parser.add_argument("--model-path", type=str, default=MODEL_PATH, help="Base model path")
parser.add_argument("--eval-only", action="store_true", help="Only run evaluation")
parser.add_argument("--compare", nargs=2, metavar=("CKPT_A", "CKPT_B"), help="Compare two checkpoints")
args = parser.parse_args()
trainer = SelfImprovementTrainer(args.model_path, args.checkpoint)
if args.eval_only:
trainer.load_model(args.checkpoint)
trainer.evaluate_model()
elif args.compare:
trainer.load_model(args.compare[0])
trainer.compare_checkpoints(args.compare[0], args.compare[1])
else:
trainer.improve(args.iterations, args.steps_per_iter)
if __name__ == "__main__":
main()
|