File size: 56,040 Bytes
77da5ce | 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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 | """
train_trl.py โ LifeStack GRPO Training via HuggingFace TRL + Unsloth
Trains a small LLM (Qwen2.5-1.5B-Instruct) to resolve daily-life conflicts
across 8 domains using Group Relative Policy Optimization (GRPO).
Supported domains:
career, finances, relationships, physical_health,
mental_wellbeing, time, flight_crisis, code_merge_crisis
Usage (Colab / GPU):
!pip install unsloth trl datasets transformers accelerate
!python train_trl.py # full curriculum (5 stages)
!python train_trl.py --dry-run # 1-step smoke test (CPU OK)
"""
import json
import os
import copy
import random
import numpy as np
import types
import sys
import importlib.machinery
# โโ EARLY PATCHES โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Unsloth MUST be imported before transformers/trl to apply its patches
try:
import unsloth
except Exception as e:
# Colab environments can fail inside unsloth import with non-ImportError
# exceptions (for example NameError from incompatible dependency combos).
print(f"[warning] Unsloth import failed, continuing with HF fallback: {e}")
def _install_trl_optional_dependency_shims() -> None:
"""
TRL GRPO imports callbacks that can hard-import optional packages like
`mergekit` and `llm_blender` even when GRPO doesn't use those paths.
Install lightweight shims so training remains runnable on Colab/Kaggle.
"""
# Always install shims before importing TRL.
# This avoids failures from incompatible optional dependency versions.
mergekit_mod = types.ModuleType("mergekit")
mergekit_mod.__path__ = [] # mark as package
mergekit_config_mod = types.ModuleType("mergekit.config")
mergekit_merge_mod = types.ModuleType("mergekit.merge")
class MergeConfiguration: # noqa: D401
"""Compatibility placeholder for TRL optional mergekit import."""
@classmethod
def model_validate(cls, data):
return data
class MergeOptions: # noqa: D401
"""Compatibility placeholder for TRL optional mergekit import."""
def __init__(self, *args, **kwargs):
pass
def run_merge(*args, **kwargs):
return None
mergekit_config_mod.MergeConfiguration = MergeConfiguration
mergekit_merge_mod.MergeOptions = MergeOptions
mergekit_merge_mod.run_merge = run_merge
mergekit_mod.config = mergekit_config_mod
mergekit_mod.merge = mergekit_merge_mod
mergekit_mod.__spec__ = importlib.machinery.ModuleSpec("mergekit", loader=None)
mergekit_config_mod.__spec__ = importlib.machinery.ModuleSpec("mergekit.config", loader=None)
mergekit_merge_mod.__spec__ = importlib.machinery.ModuleSpec("mergekit.merge", loader=None)
sys.modules["mergekit"] = mergekit_mod
sys.modules["mergekit.config"] = mergekit_config_mod
sys.modules["mergekit.merge"] = mergekit_merge_mod
llm_blender_mod = types.ModuleType("llm_blender")
class Blender: # noqa: D401
"""Compatibility placeholder for TRL optional llm_blender import."""
def __init__(self, *args, **kwargs):
pass
def rank(self, *args, **kwargs):
return [0]
def score(self, *args, **kwargs):
return [0.0]
llm_blender_mod.Blender = Blender
llm_blender_mod.__spec__ = importlib.machinery.ModuleSpec("llm_blender", loader=None)
sys.modules["llm_blender"] = llm_blender_mod
# vLLM is optional for GRPO; provide import-safe shim for environments
# where import checks pass but real import fails due incomplete installs.
vllm_mod = types.ModuleType("vllm")
class SamplingParams: # noqa: D401
"""Compatibility placeholder for TRL optional vllm import."""
def __init__(self, *args, **kwargs):
pass
class LLM: # noqa: D401
"""Compatibility placeholder for TRL optional vllm import."""
def __init__(self, *args, **kwargs):
pass
def generate(self, *args, **kwargs):
return []
vllm_mod.SamplingParams = SamplingParams
vllm_mod.LLM = LLM
vllm_mod.__spec__ = importlib.machinery.ModuleSpec("vllm", loader=None)
sys.modules["vllm"] = vllm_mod
print("[warning] using local shims for mergekit/llm_blender compatibility.")
_install_trl_optional_dependency_shims()
import torch
from datasets import Dataset
from transformers import AutoTokenizer
from trl import GRPOConfig, GRPOTrainer
# Fix for TRL 0.15.1 + Transformers 4.56.2 incompatibility with _get_train_sampler
import inspect
_original_get_train_sampler = GRPOTrainer._get_train_sampler
def _patched_get_train_sampler(self, *args, **kwargs):
sig = inspect.signature(_original_get_train_sampler)
if len(sig.parameters) == 1:
return _original_get_train_sampler(self)
return _original_get_train_sampler(self, *args, **kwargs)
GRPOTrainer._get_train_sampler = _patched_get_train_sampler
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# LifeStack imports
from core.life_state import LifeMetrics, ResourceBudget, DependencyGraph
from core.reward import compute_reward
from agent.conflict_generator import generate_conflict, TEMPLATES, TaskGenerator
from intake.simperson import SimPerson
from core.task import Task, FlightCrisisTask
def _tensorboard_available() -> bool:
try:
import tensorboard # noqa: F401
return True
except ImportError:
return False
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 1. MODEL SETUP (Unsloth for 4-bit efficiency)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def load_model():
"""Load model with Unsloth 4-bit quantization for Colab T4."""
try:
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/Qwen2.5-1.5B-Instruct",
max_seq_length=1024,
dtype=None, # auto-detect
load_in_4bit=True,
)
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_alpha=16,
lora_dropout=0,
bias="none",
use_gradient_checkpointing="unsloth",
)
return model, tokenizer
except Exception as e:
# Fallback: standard HF + PEFT LoRA when Unsloth is missing or broken
print(f"[warning] Unsloth model load failed, using HF+PEFT fallback: {e}")
# MUST apply LoRA here โ training the full 1.5B model requires ~24GB
# VRAM for Adam states and breaks the PeftModel loader in inference.py.
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model, TaskType
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name, dtype=torch.float32, device_map="auto"
)
lora_cfg = LoraConfig(
r=16,
lora_alpha=16,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_dropout=0.0,
bias="none",
task_type=TaskType.CAUSAL_LM,
)
model = get_peft_model(model, lora_cfg)
model.print_trainable_parameters()
return model, tokenizer
def load_model_for_dry_run():
"""
Tiny CPU-friendly model used only for --dry-run pipeline validation.
Keeps dry-run fast and avoids downloading multi-GB checkpoints locally.
"""
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "sshleifer/tiny-gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
model_name,
dtype=torch.float32,
device_map="auto",
)
# TRL GRPO expects this field on some model classes; add for tiny GPT2.
if not hasattr(model, "warnings_issued"):
model.warnings_issued = {}
model.eval()
print(f" Using tiny dry-run model: {model_name}")
return model, tokenizer
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 2. DATASET: Generate conflict prompts
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def build_prompt_for_task(task, person, metrics, budget, seed=42, step=0, event_descriptions=None):
"""Build a compact prompt from task state while preserving reward metadata."""
flat = metrics.flatten()
# Keep only 5 high-signal metrics to fit prompt+completion in a tight token budget.
metric_priority = [
"career.workload",
"finances.liquidity",
"relationships.romantic",
"physical_health.energy",
"mental_wellbeing.stress_level",
"time.free_hours_per_week",
"time.commute_burden",
]
key_metrics = [k for k in metric_priority if k in flat][:5]
if len(key_metrics) < 5:
for k in flat:
if k not in key_metrics:
key_metrics.append(k)
if len(key_metrics) == 5:
break
metrics_str = "\n".join(f"- {k}: {flat[k]:.1f}" for k in key_metrics)
event_context = ""
if event_descriptions:
recent = event_descriptions[-2:]
compact_events = [e[:140] for e in recent]
event_context = "\nRecent events:\n" + "\n".join(f"- {e}" for e in compact_events)
# Keep SYSTEM_METADATA for reward reconstruction.
metadata = {
"domain": task.domain,
"disruption": task.mutable_world,
"difficulty": task.difficulty,
"seed": seed,
"step": step,
"budget": {
"time": budget.time_hours,
"money": budget.money_dollars,
"energy": budget.energy_units
}
}
metadata_str = json.dumps(metadata, separators=(",", ":"))
# Cap routes to 2 to keep the context short but actionable.
routes_str = "\n".join(
f"- {r.id}: {r.name} (needs {', '.join(r.required_action_types[:2])})"
for r in task.viable_routes[:2]
)
if not routes_str:
routes_str = "- none"
return (
"You are LifeStack. Return ONLY compact JSON.\n"
f"<SYSTEM_METADATA>\n{metadata_str}\n</SYSTEM_METADATA>\n"
f"Task: {task.goal}\n"
f"Story: {task.domain_metadata.get('story', '')[:160]}\n"
f"Key metrics:\n{metrics_str}\n"
f"Budget: time={budget.time_hours:.1f}, money={budget.money_dollars:.1f}, energy={budget.energy_units:.1f}\n"
f"Routes (max 2):\n{routes_str}\n"
"Required keys: action_type, target_domain, metric_changes, resource_cost, reasoning.\n"
"Keep reasoning under 25 words. No markdown.\n"
f'{{"action_type": "negotiate|communicate|delegate|spend|reschedule|rest|deprioritize|execute", '
f'"target_domain": "career|finances|relationships|physical_health|mental_wellbeing|time OR <route_id>", '
f'"metric_changes": {{"domain.submetric": delta}}, '
f'"resource_cost": {{"time": 0, "money": 0, "energy": 0}}, '
f'"reasoning": "brief explanation"}}'
f"{event_context}"
)
# All 8 TaskGenerator domains โ covers the full daily-life action space.
# transport_crisis randomly dispatches to: flight, train, car, rideshare, transit-strike
ALL_DOMAINS = [
"career",
"finances",
"relationships",
"physical_health",
"mental_wellbeing",
"time",
"transport_crisis", # โ was flight_crisis; now covers all 5 transport modes
"code_merge_crisis",
]
def generate_dataset(n_prompts: int = 200, difficulty: int = None) -> Dataset:
"""
Generate n conflict prompts as a HuggingFace Dataset.
Samples evenly across ALL 8 daily-life domains (career, finances,
relationships, physical_health, mental_wellbeing, time,
transport_crisis [flight/train/car/rideshare/transit-strike], code_merge_crisis)
so GRPO learns a general life-management policy.
Args:
n_prompts: Total number of prompts to generate.
difficulty: If given, fix all prompts to this difficulty (1-5).
If None, cycles evenly through levels 1-5.
"""
person_pool = [
SimPerson(name="Alex", openness=0.4, conscientiousness=0.9, extraversion=0.7, agreeableness=0.25, neuroticism=0.8),
SimPerson(name="Chloe", openness=0.9, conscientiousness=0.2, extraversion=0.5, agreeableness=0.70, neuroticism=0.15),
SimPerson(name="Sam", openness=0.5, conscientiousness=0.6, extraversion=0.1, agreeableness=0.65, neuroticism=0.90),
SimPerson(name="Jordan",openness=0.7, conscientiousness=0.5, extraversion=0.6, agreeableness=0.50, neuroticism=0.40),
SimPerson(name="Maya", openness=0.8, conscientiousness=0.7, extraversion=0.3, agreeableness=0.80, neuroticism=0.60),
]
generator = TaskGenerator()
prompts = []
for i in range(n_prompts):
person = random.choice(person_pool)
# Round-robin across all 8 domains โ guarantees balanced coverage
domain = ALL_DOMAINS[i % len(ALL_DOMAINS)]
# Cycle difficulty 1-5 unless fixed
curr_diff = difficulty if difficulty else (i % 5) + 1
# Save the outer random state so that task seeding is deterministic
# but does NOT corrupt the outer RNG chain between loop iterations.
outer_state = random.getstate()
task_seed = random.randint(0, 999999)
random.seed(task_seed)
task = generator.generate(domain=domain, difficulty=curr_diff)
# Overlay a matching legacy conflict disruption for richer metric seeding
conflict = generate_conflict(curr_diff)
task.mutable_world.update(conflict.primary_disruption)
task.visible_world.update(conflict.primary_disruption)
metrics = LifeMetrics()
graph = DependencyGraph()
metrics = graph.cascade(metrics, task.mutable_world)
budget_dict = task.constraints.get("budget", {})
budget = ResourceBudget(
time_hours=budget_dict.get("time", 20.0),
money_dollars=budget_dict.get("money", 500.0),
energy_units=budget_dict.get("energy", 100.0),
)
# Randomly pick a starting step (0, 2, or 4) to activate replan signal
start_step = random.choice([0, 2, 4])
# Restore outer state now โ env fast-forward below must not bleed into
# subsequent iterations' seed selection.
random.setstate(outer_state)
# Advance outer state past the seed we consumed so next iteration differs.
_ = random.random()
event_log = []
if start_step > 0:
from core.lifestack_env import LifeStackEnv, LifeStackAction
env = LifeStackEnv()
env.reset(task=task, conflict=task.mutable_world)
for s in range(start_step):
# Take null actions to let events fire naturally
obs = env.step(LifeStackAction(action_type="rest", target="time", actions_taken=0))
for event_id in obs.metadata.get("info", []):
if event_id.startswith("EVENT_FIRED:"):
event_log.append(event_id[len("EVENT_FIRED:"):].strip())
metrics = env.state.current_metrics
budget = env.state.budget
prompt = build_prompt_for_task(task, person, metrics, budget, seed=task_seed, step=start_step, event_descriptions=event_log)
prompts.append({"prompt": prompt, "difficulty": curr_diff, "domain": domain})
return Dataset.from_list(prompts)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 3. REWARD FUNCTION for GRPO
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
_GLOBAL_REWARD_CALL_COUNT = 0
LOG_INTERVAL = 20
LOG_DIR = "training_logs"
SAMPLE_LOG_PATH = os.path.join(LOG_DIR, "generations.jsonl")
def get_lifestack_evaluation(completion: str, prompt: str) -> dict:
"""Run the environment and return the full reward breakdown. Computed fresh per call to prevent hacking."""
from core.lifestack_env import LifeStackEnv, LifeStackAction
import re
try:
# 1. Parse JSON
text = completion.strip()
if "```json" in text:
text = text.split("```json")[-1].split("```")[0]
elif "```" in text:
text = text.split("```")[-1].split("```")[0]
data = json.loads(text.strip())
# 2. Extract Task Metadata
m = re.search(r'<SYSTEM_METADATA>\n(.*?)\n</SYSTEM_METADATA>', prompt, re.DOTALL)
if not m:
return {"reward": -0.5, "breakdown": {}}
meta = json.loads(m.group(1).strip())
try:
# Use TaskGenerator so routes/milestones/success_conditions are populated.
from agent.conflict_generator import TaskGenerator
gen = TaskGenerator()
domain = meta.get("domain", "flight_crisis")
# Keep seed active through the ENTIRE env evaluation โ task gen, reset,
# fast-forward, and the action step. Without this, stochastic events
# (event.step == -1, random.random() < probability) fire differently each
# call, so reward_task_success_fn / reward_milestone_fn / reward_replan_fn
# see inconsistent env states for the same completion.
eval_seed = meta.get("seed", 42)
random.seed(eval_seed)
task = gen.generate(domain=domain, difficulty=meta.get("difficulty", 3))
# Overlay the actual disruption that was presented in the prompt
task.mutable_world.update(meta.get("disruption", {}))
task.visible_world.update(meta.get("disruption", {}))
except Exception as e:
print(f"[reward] Task construction failed: {e}")
random.seed()
return {"reward": -0.5, "breakdown": {"error": str(e)}}
# Validate required fields are present and non-None.
_required = ("id", "goal", "constraints", "mutable_world", "visible_world")
if any(getattr(task, f, None) is None for f in _required):
print("[reward] Task missing required fields after construction.")
random.seed()
return {"reward": -0.5, "breakdown": {"error": "missing_fields"}}
# 3. Step Env โ still under eval_seed so events are deterministic per (completion, prompt)
env = LifeStackEnv()
env.reset(task=task, conflict=meta.get("disruption", {}))
# Fast-forward to the state the model saw
curr_step = meta.get("step", 0)
for _ in range(curr_step):
env.step(LifeStackAction(action_type="rest", target="time", actions_taken=0))
initial_metrics = dict(env.state.current_metrics.flatten())
action = LifeStackAction(
action_type=data.get("action_type"),
target=data.get("target_domain"),
metric_changes=data.get("metric_changes", {}),
resource_cost=data.get("resource_cost", {}),
reasoning=data.get("reasoning", ""),
completion=completion,
actions_taken=1
)
obs = env.step(action)
# 7-day discounted rollout โ real long-term signal, not decoration.
# Runs BEFORE random.seed() so the null steps share the same eval_seed,
# keeping the trajectory deterministic for the same (completion, prompt).
rollout_data = env.rollout(n_steps=7, gamma=0.9)
random.seed() # restore global RNG โ eval_seed must not bleed into trainer
# Inject longterm component into the breakdown so reward_longterm_fn
# can extract it without a second env construction.
breakdown = obs.metadata.get("breakdown", {})
components = breakdown.get("components", {})
components["longterm"] = rollout_data["discounted_reward"]
breakdown["components"] = components
result = {
"reward": float(obs.reward),
"breakdown": breakdown,
"action": action,
"obs_metrics": dict(obs.metrics),
"initial_metrics": initial_metrics,
"longterm_reward": rollout_data["discounted_reward"],
"trajectory": rollout_data["trajectory"],
}
# 4. Global Logging
global _GLOBAL_REWARD_CALL_COUNT
_GLOBAL_REWARD_CALL_COUNT += 1
if _GLOBAL_REWARD_CALL_COUNT % LOG_INTERVAL == 0:
if not os.path.exists(LOG_DIR): os.makedirs(LOG_DIR)
log_entry = {
"step": _GLOBAL_REWARD_CALL_COUNT,
"prompt": prompt[:500] + "...",
"completion": completion,
"action": data,
"reward": result["reward"],
"longterm_reward": result["longterm_reward"],
"breakdown": result["breakdown"],
"components": components,
}
with open(SAMPLE_LOG_PATH, "a") as f:
f.write(json.dumps(log_entry) + "\n")
if components:
comp_str = " | ".join(f"{k}={v:.3f}" for k, v in components.items())
print(f"[step {_GLOBAL_REWARD_CALL_COUNT}] r0={result['reward']:.3f} | r_lt={result['longterm_reward']:.3f} | {comp_str}")
return result
except Exception:
random.seed() # always restore RNG on any failure path
return {"reward": -0.5, "breakdown": {}, "action": None, "initial_metrics": meta.get("disruption", {}) if 'meta' in locals() else {}}
def reward_format_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""Scores JSON format compliance independently (Static Check)."""
from core.reward import reward_format_compliance
return [reward_format_compliance(c) for c in completions]
def reward_plausibility_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""Penalize zero-cost metric changes (Independent Logic Check)."""
from core.reward import reward_plausibility_check
import json
results = []
for c in completions:
try:
text = c.strip()
if "```json" in text: text = text.split("```json")[-1].split("```")[0]
elif "```" in text: text = text.split("```")[-1].split("```")[0]
data = json.loads(text.strip())
mc = data.get("metric_changes", {})
rc = data.get("resource_cost", {})
results.append(reward_plausibility_check(mc, rc))
except Exception:
results.append(0.0)
return results
def reward_task_success_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""Core outcome reward isolated to completion (Environment Simulation)."""
results = []
for c, p in zip(completions, prompts):
eval_res = get_lifestack_evaluation(c, p)
if not eval_res.get("breakdown"):
results.append(eval_res.get("reward", -0.5))
else:
results.append(eval_res.get("breakdown", {}).get("components", {}).get("completion", 0.0))
return results
def reward_milestone_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""Monitor progress through logical bottlenecks (Environment Simulation)."""
return [get_lifestack_evaluation(c, p).get("breakdown", {}).get("components", {}).get("milestone", 0.0) for c, p in zip(completions, prompts)]
def reward_reasoning_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""Evaluate planning coherence (Independent Semantic/Logic Check)."""
from core.reward import reward_reasoning_coherence
import json
results = []
for c in completions:
try:
text = c.strip()
if "```json" in text: text = text.split("```json")[-1].split("```")[0]
elif "```" in text: text = text.split("```")[-1].split("```")[0]
data = json.loads(text.strip())
reasoning = data.get("reasoning", "")
a_type = data.get("action_type", "")
# reward_reasoning_coherence returns [-0.30, 0.30] โ no scaling needed
results.append(reward_reasoning_coherence(reasoning, action_type=a_type))
except Exception:
results.append(-0.1)
return results
def reward_human_feedback_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""
Rewards actions that align with past human outcome feedback (ChromaDB memory).
Requires chromadb + a pre-populated LifeStackMemory database.
Falls back silently to neutral 0.0 when:
- chromadb is not installed (e.g. fresh Kaggle / Colab session)
- the memory DB is empty or unreachable
Returns 0.0 (abstain) rather than penalising the model.
"""
# โโ Guard: skip gracefully if chromadb / memory unavailable โโโโโโโโโโ
try:
from core.feedback import OutcomeFeedback, compute_human_feedback_reward
from agent.memory import LifeStackMemory
memo = LifeStackMemory(silent=True)
except (ImportError, Exception) as e:
print(f"[warning] reward_human_feedback_fn unavailable ({e}), applying small penalty.")
# chromadb not installed or DB init failed โ apply small penalty
return [-0.01] * len(completions)
rewards = []
for c, p in zip(completions, prompts):
try:
eval_res = get_lifestack_evaluation(c, p)
action = eval_res.get("action")
if not action:
rewards.append(0.0)
continue
# Use task prompt to query feedback instead of model-generated reasoning
# to avoid reward-hacking ChromaDB. Must use query_embeddings to match
# the custom _embed_text() space used when storing feedback.
# Bug 8: Use embeddings instead of raw text for query
q_emb = memo._embed_text(p)
similar_fb_list = memo.feedback_collection.query(
query_embeddings=[q_emb],
n_results=1
).get('metadatas', [[]])[0]
if not similar_fb_list:
rewards.append(0.0)
continue
fb_meta = similar_fb_list[0]
fb = OutcomeFeedback(
episode_id=fb_meta["episode_id"],
overall_effectiveness=fb_meta["effectiveness"],
domains_improved=json.loads(fb_meta["domains_improved"]),
domains_worsened=json.loads(fb_meta["domains_worsened"])
)
from core.lifestack_env import LifeStackObservation
obs = LifeStackObservation(metrics=eval_res.get("obs_metrics", {}))
init_metrics = eval_res.get("initial_metrics", {})
fb_reward = compute_human_feedback_reward(init_metrics, obs, fb)
rewards.append(fb_reward)
except Exception:
rewards.append(0.0)
return rewards
def reward_replan_fn(completions, prompts, **kwargs) -> list[float]:
"""Exposes the internal replan bonus as a standalone GRPO signal."""
rewards = []
for c, p in zip(completions, prompts):
eval_data = get_lifestack_evaluation(c, p)
rewards.append(eval_data.get("breakdown", {}).get("components", {}).get("replan", 0.0))
return rewards
def reward_longterm_fn(completions: list[str], prompts: list[str], **kwargs) -> list[float]:
"""
7-day ฮณ=0.9 discounted rollout reward.
After the model's action is applied, the env runs 7 null/rest steps to
model "what happens to your life if nothing extraordinary occurs after
this decision." The discounted sum is the training signal.
This is the only reward function whose gradient explicitly penalises
actions that look good on day 0 but trigger a cascade collapse by day 4.
It is NOT a decoration โ the rollout runs inside the real LifeStack env.
"""
return [
get_lifestack_evaluation(c, p).get("longterm_reward", 0.0)
for c, p in zip(completions, prompts)
]
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 4. CHECKPOINT HELPERS
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def find_latest_checkpoint(stage_dir: str):
"""
Scan a stage output directory for the most recent Trainer checkpoint.
Returns the checkpoint path, or None if none exist.
"""
import glob
checkpoints = sorted(
glob.glob(os.path.join(stage_dir, "checkpoint-*")),
key=lambda p: int(p.split("-")[-1])
)
return checkpoints[-1] if checkpoints else None
_CURRICULUM_STATE_FILE = "curriculum_state.json"
def save_stage_state(output_dir: str, stage: int, curr_diff: int):
"""Persist curriculum progress so we can resume after a session cut."""
path = os.path.join(output_dir, _CURRICULUM_STATE_FILE)
os.makedirs(output_dir, exist_ok=True)
with open(path, "w") as f:
json.dump({"completed_stage": stage, "next_difficulty": curr_diff}, f)
print(f" [ckpt] Curriculum state saved โ stage={stage}, next_diff={curr_diff}")
def load_stage_state(output_dir: str) -> tuple[int, int]:
"""
Returns (start_stage, curr_diff) from a previous run.
Falls back to (1, 1) if no state file exists.
"""
path = os.path.join(output_dir, _CURRICULUM_STATE_FILE)
if os.path.exists(path):
with open(path) as f:
state = json.load(f)
start_stage = state["completed_stage"] + 1
curr_diff = state["next_difficulty"]
print(f" [ckpt] Resuming from stage {start_stage}, difficulty {curr_diff}")
return start_stage, curr_diff
return 1, 1
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 5. TRAINING LOOP (checkpoint-aware)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def train_curriculum(
n_stages=5,
n_prompts_per_stage=100,
output_dir="./lifestack_model",
resume=False,
start_stage=None,
):
"""
Curriculum training with automatic checkpoint saving and resume.
Each stage saves a checkpoint every 25 steps and persists curriculum
state to curriculum_state.json. If the session is killed mid-stage,
re-run with --resume and the trainer will pick up from the last
saved checkpoint automatically.
Args:
resume: If True, read curriculum_state.json to find the last
completed stage and continue from there.
start_stage: Override the starting stage (1-indexed). Useful for
manual restart (e.g. --start-stage 3).
"""
print("=" * 60)
print("๐ LIFESTACK SUCCESS-BASED CURRICULUM TRAINING")
print("=" * 60)
model, tokenizer = load_model()
# โโ Determine where to start โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if resume:
first_stage, curr_diff = load_stage_state(output_dir)
elif start_stage:
first_stage = start_stage
curr_diff = 1 # difficulty resets; user can edit state file for fine control
else:
first_stage, curr_diff = 1, 1
for stage in range(first_stage, n_stages + 1):
print(f"\n[STAGE {stage}/{n_stages}] Difficulty={curr_diff}")
stage_dir = f"{output_dir}/stage_{stage}"
# โโ Check for a mid-stage checkpoint from a previous session โโโโโ
resume_ckpt = find_latest_checkpoint(stage_dir) if resume else None
if resume_ckpt:
print(f" [ckpt] Resuming mid-stage from: {resume_ckpt}")
else:
# Generate fresh data only for a clean start of the stage
dataset = generate_dataset(n_prompts_per_stage, difficulty=curr_diff)
# โโ GRPOConfig with checkpoint cadence โโโโโโโโโโโโโโโโโโโโโโโโโโโ
config = GRPOConfig(
output_dir=stage_dir,
num_train_epochs=1,
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
learning_rate=5e-6,
# Keep completion short to avoid clipped mid-JSON outputs.
max_completion_length=128,
temperature=0.9,
# TRL rule: num_generations must divide per_device_train_batch_size.
num_generations=4,
bf16=torch.cuda.is_bf16_supported() if torch.cuda.is_available() else False,
# โโ Checkpoint settings โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
save_strategy="steps",
save_steps=25,
save_total_limit=3,
# โโ Logging โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
logging_steps=5,
# tensorboard only if installed; fall back to none to avoid ImportError on Colab/Kaggle
report_to="tensorboard" if _tensorboard_available() else "none",
)
config.unsloth_num_chunks = -1
if stage == 1:
# Warm-up: learn valid JSON structure first, then optimize decisions.
stage_reward_funcs = [reward_format_fn]
print(" Warm-up reward mode: format-only")
else:
stage_reward_funcs = [
reward_format_fn,
reward_plausibility_fn,
reward_task_success_fn,
reward_milestone_fn,
reward_replan_fn,
reward_reasoning_fn,
reward_human_feedback_fn,
reward_longterm_fn,
]
trainer = GRPOTrainer(
model=model,
processing_class=tokenizer, # TRL 1.x: renamed from tokenizer=
args=config,
train_dataset=dataset if not resume_ckpt else generate_dataset(n_prompts_per_stage, difficulty=curr_diff),
reward_funcs=stage_reward_funcs,
)
# Pass the checkpoint path โ Trainer will reload weights + optimizer state
trainer.train(resume_from_checkpoint=resume_ckpt)
# โโ Save completed stage model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
trainer.save_model(stage_dir)
tokenizer.save_pretrained(stage_dir)
print(f" โ
Stage {stage} model saved โ {stage_dir}")
# โโ Curriculum progression logic โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TRL 1.x logs mean reward as "reward"; some builds use "train/reward" โ check both
last_log = trainer.state.log_history[-1] if trainer.state.log_history else {}
avg_reward = last_log.get("reward", last_log.get("train/reward", 0.0))
if avg_reward > 0.6 and curr_diff < 5:
print(f" โ
Reward {avg_reward:.3f} > 0.6 โ advancing to difficulty {curr_diff + 1}")
curr_diff += 1
else:
print(f" โ ๏ธ Reward {avg_reward:.3f} โ holding at difficulty {curr_diff}")
# โโ Persist curriculum state AFTER each stage โโโโโโโโโโโโโโโโโโโโ
# This is what lets us resume correctly on next session
save_stage_state(output_dir, stage, curr_diff)
# โโ Final model save โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
print(f"\n๐ Training complete. Final model โ {output_dir}")
return trainer
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 5. EVALUATION + REWARD CURVE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def evaluate_and_plot(model_dir="./lifestack_model"):
"""Load the trained model, run 50 evaluation episodes, plot the curve."""
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from transformers import AutoModelForCausalLM, AutoTokenizer
print("\n" + "=" * 50)
print(" EVALUATION")
print("=" * 50)
# Use Unsloth's loader to avoid peft version conflicts on Kaggle/Colab
try:
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=model_dir,
max_seq_length=2048,
load_in_4bit=True,
)
FastLanguageModel.for_inference(model)
print(" Loaded via Unsloth FastLanguageModel")
except Exception as unsloth_err:
print(f" Unsloth load failed ({unsloth_err}), falling back to AutoModelForCausalLM")
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
tokenizer = AutoTokenizer.from_pretrained(model_dir)
base = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-1.5B-Instruct", dtype=torch.float32, device_map="auto"
)
model = PeftModel.from_pretrained(base, model_dir)
model.eval()
graph = DependencyGraph()
rewards = []
generator = TaskGenerator()
for ep in range(50):
difficulty = min(5, 1 + ep // 10)
# Cycle through all 8 domains during evaluation
domain = ALL_DOMAINS[ep % len(ALL_DOMAINS)]
ep_seed = ep * 137 # deterministic per episode so reward_task_success_fn reconstructs the same task
random.seed(ep_seed)
task = generator.generate(domain=domain, difficulty=difficulty)
random.seed()
metrics = LifeMetrics()
# Initial disruption from legacy templates
conflict = generate_conflict(difficulty)
metrics = graph.cascade(metrics, {**task.mutable_world, **conflict.primary_disruption})
budget_dict = task.constraints.get("budget", {})
budget = ResourceBudget(
time_hours=budget_dict.get("time", 20.0),
money_dollars=budget_dict.get("money", 500.0),
energy_units=budget_dict.get("energy", 100.0),
)
person = SimPerson(name="Eval")
prompt = build_prompt_for_task(task, person, metrics, budget, seed=ep_seed, step=0)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs, max_new_tokens=128, temperature=0.3,
do_sample=True,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
completion = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
r = reward_task_success_fn([completion], [prompt])[0]
rewards.append(r)
if (ep + 1) % 10 == 0:
print(f" Episode {ep+1}/50 | Reward: {r:.3f} | Avg: {np.mean(rewards):.3f}")
# Plot
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(range(1, 51), rewards, color="steelblue", alpha=0.6, label="Episode Reward")
# Rolling average
window = 5
rolling = [np.mean(rewards[max(0, i-window+1):i+1]) for i in range(len(rewards))]
ax.plot(range(1, 51), rolling, color="crimson", linewidth=2, linestyle="--", label="5-ep Rolling Avg")
ax.axhline(y=0, color="gray", linewidth=0.8, linestyle="--", alpha=0.7)
ax.set_title("LifeStack GRPO โ Evaluation Reward Curve (Qwen2.5-1.5B)", fontsize=13, fontweight="bold")
ax.set_xlabel("Evaluation Episode (post-training)", fontsize=11)
ax.set_ylabel("Completion Reward [-1, +1]", fontsize=11)
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
# Annotate mean
mean_r = float(np.mean(rewards))
ax.axhline(y=mean_r, color="steelblue", linewidth=0.8, linestyle=":", alpha=0.6)
ax.text(48, mean_r + 0.02, f"mean={mean_r:.2f}", ha="right", fontsize=9, color="steelblue")
fig.tight_layout()
fig.savefig("grpo_reward_curve.png", dpi=150)
plt.close(fig)
print("๐ Saved grpo_reward_curve.png")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ENTRY POINT
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 6. POST-TRAINING VALIDATION
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
MIN_MODEL_SIZE_BYTES = 5 * 1024 * 1024 # 5 MB โ LoRA adapter ~39 MB, placeholder ~few KB
def validate_saved_model(output_dir: str = "./lifestack_model"):
"""
Validates that a real model was saved (not a placeholder).
Raises RuntimeError if pytorch_model.bin or model.safetensors is missing / too small.
"""
import glob
weight_files = (
glob.glob(os.path.join(output_dir, "*.bin")) +
glob.glob(os.path.join(output_dir, "*.safetensors")) +
glob.glob(os.path.join(output_dir, "**", "*.safetensors"), recursive=True) +
glob.glob(os.path.join(output_dir, "**", "*.bin"), recursive=True)
)
# Deduplicate
weight_files = list(set(weight_files))
if not weight_files:
raise RuntimeError(
f"[VALIDATION FAIL] No weight files found in {output_dir}.\n"
"Real training never completed โ run train_trl.py on a GPU instance."
)
total_bytes = sum(os.path.getsize(f) for f in weight_files)
if total_bytes < MIN_MODEL_SIZE_BYTES:
raise RuntimeError(
f"[VALIDATION FAIL] Total weight size = {total_bytes} bytes ({total_bytes/1e6:.2f} MB).\n"
f"Expected > {MIN_MODEL_SIZE_BYTES/1e6:.0f} MB for a real model.\n"
f"Found files: {weight_files}\n"
"This looks like a placeholder. Run full training on a GPU."
)
print(f"[VALIDATION PASS] Model saved correctly.")
print(f" Weight files : {len(weight_files)}")
print(f" Total size : {total_bytes / 1e6:.1f} MB")
return total_bytes
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 7. DRY-RUN MODE (validates pipeline without GPU)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def dry_run(output_dir: str = "./lifestack_model_dryrun"):
"""
Runs a single GRPO training step on a minimal dataset (4 prompts).
Verifies the entire pipeline: dataset โ prompt โ reward โ trainer.train() โ save.
Does NOT require a GPU. Saved weights will be small (< 50 MB) โ that is expected.
Use this to confirm:
- All imports resolve
- Reward functions are callable
- Trainer.train() completes without error
- model.save_pretrained() writes real weight files
"""
print("=" * 60)
print("๐งช LIFESTACK DRY-RUN (1 step, CPU, tiny dataset)")
print("=" * 60)
model, tokenizer = load_model_for_dry_run()
dataset = generate_dataset(n_prompts=4, difficulty=1)
print(f" Dataset size : {len(dataset)} prompts")
config = GRPOConfig(
output_dir=output_dir,
num_train_epochs=1,
per_device_train_batch_size=4,
gradient_accumulation_steps=1,
learning_rate=1e-5,
max_completion_length=128,
temperature=0.9,
num_generations=4,
max_steps=1, # ONE step โ just proves the pipeline works
bf16=False,
fp16=False,
report_to="none", # No tensorboard for dry-run
logging_steps=1,
)
config.unsloth_num_chunks = -1
trainer = GRPOTrainer(
model=model,
processing_class=tokenizer, # TRL 1.x: renamed from tokenizer=
args=config,
train_dataset=dataset,
reward_funcs=[
reward_format_fn,
],
)
print(" Running 1 training step...")
trainer.train()
print(" โ
trainer.train() completed.")
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
print(f" โ
model.save_pretrained() โ {output_dir}")
# Check something real was saved
import glob
weight_files = (
glob.glob(os.path.join(output_dir, "*.bin")) +
glob.glob(os.path.join(output_dir, "*.safetensors")) +
glob.glob(os.path.join(output_dir, "**", "*.safetensors"), recursive=True)
)
weight_files = list(set(weight_files))
total_bytes = sum(os.path.getsize(f) for f in weight_files)
print(f"\n Weight files saved : {len(weight_files)}")
for f in weight_files:
print(f" {f} ({os.path.getsize(f)/1e6:.2f} MB)")
print(f" Total weight size : {total_bytes/1e6:.2f} MB")
if total_bytes == 0:
raise RuntimeError("[DRY-RUN FAIL] No bytes written. save_pretrained() did not produce weights.")
if total_bytes <= 100: # 17 bytes = placeholder
raise RuntimeError(
f"[DRY-RUN FAIL] Only {total_bytes} bytes written โ this is a placeholder, not real weights."
)
print("\n โ
DRY-RUN PASSED โ full training pipeline is wired correctly.")
print(" โ Run train_curriculum() on a GPU for a production model (> 50 MB).")
return trainer
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 8. MULTI-STEP FULL EPISODE RUNNER
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def run_full_episode(
model_dir: str = "./lifestack_model",
n_episodes: int = 10,
push_to_hub: bool = False,
hub_repo_id: str = "lifestack-grpo",
):
"""
Run multi-step episodes with the trained model (post-training evaluation).
Each episode plays up to 5 sequential env steps so the model handles
long-horizon decision chains, not just single actions.
Args:
model_dir: Saved GRPO model directory.
n_episodes: Number of full episodes to roll out.
push_to_hub: If True, push model + tokenizer to HuggingFace Hub.
hub_repo_id: Hub repo id (e.g. "username/lifestack-grpo").
"""
from core.lifestack_env import LifeStackEnv, LifeStackAction
print("\n" + "=" * 60)
print("๐ฎ MULTI-STEP FULL EPISODE RUNNER")
print("=" * 60)
# Load model โ Unsloth first, HF+PEFT fallback
try:
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=model_dir, max_seq_length=1024, load_in_4bit=True,
)
FastLanguageModel.for_inference(model)
print(" Loaded via Unsloth")
except Exception as e:
print(f" Unsloth failed ({e}), using AutoModelForCausalLM + PeftModel")
from transformers import AutoModelForCausalLM
from peft import PeftModel
tokenizer = AutoTokenizer.from_pretrained(model_dir)
base = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-1.5B-Instruct", torch_dtype=torch.float16, device_map="auto"
)
model = PeftModel.from_pretrained(base, model_dir)
model.eval()
generator = TaskGenerator()
graph = DependencyGraph()
episode_rewards = []
for ep in range(n_episodes):
domain = ALL_DOMAINS[ep % len(ALL_DOMAINS)]
ep_seed = ep * 31 + 7
random.seed(ep_seed)
task = generator.generate(domain=domain, difficulty=min(5, 1 + ep // 2))
conflict = generate_conflict(min(5, 1 + ep // 2))
random.seed()
metrics = LifeMetrics()
metrics = graph.cascade(metrics, {**task.mutable_world, **conflict.primary_disruption})
budget_dict = task.constraints.get("budget", {})
budget = ResourceBudget(
time_hours=budget_dict.get("time", 20.0),
money_dollars=budget_dict.get("money", 500.0),
energy_units=budget_dict.get("energy", 100.0),
)
person = SimPerson(name="EvalAgent", openness=0.6, conscientiousness=0.7,
extraversion=0.5, agreeableness=0.6, neuroticism=0.4)
env = LifeStackEnv()
env.reset(task=task, conflict=task.mutable_world)
ep_total = 0.0
horizon = min(getattr(task, "horizon", 5), 5)
for step in range(horizon):
prompt = build_prompt_for_task(task, person, env.state.current_metrics,
env.state.budget, seed=ep_seed, step=step)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
out = model.generate(
**inputs, max_new_tokens=128, temperature=0.3, do_sample=True,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
completion = tokenizer.decode(out[0][inputs["input_ids"].shape[1]:],
skip_special_tokens=True)
try:
text = completion.strip()
if "```json" in text:
text = text.split("```json")[-1].split("```")[0]
elif "```" in text:
text = text.split("```")[-1].split("```")[0]
d = json.loads(text)
env_action = LifeStackAction(
action_type=d.get("action_type", "rest"),
target=d.get("target_domain", "time"),
metric_changes=d.get("metric_changes", {}),
resource_cost=d.get("resource_cost", {}),
reasoning=d.get("reasoning", ""),
actions_taken=1,
)
except Exception:
env_action = LifeStackAction(action_type="rest", target="time",
metric_changes={}, resource_cost={}, actions_taken=0)
obs = env.step(env_action)
ep_total += obs.reward
if obs.done:
break
episode_rewards.append(ep_total)
print(f" Ep {ep+1:2d}/{n_episodes} | {domain:20s} | reward={ep_total:.3f}")
mean_r = float(np.mean(episode_rewards)) if episode_rewards else 0.0
print(f"\n Mean episode reward : {mean_r:.3f}")
print(f" Best episode reward : {max(episode_rewards):.3f}")
if push_to_hub:
try:
print(f"\n Pushing to HuggingFace Hub: {hub_repo_id} ...")
model.push_to_hub(hub_repo_id)
tokenizer.push_to_hub(hub_repo_id)
print(f" โ
Pushed โ https://huggingface.co/{hub_repo_id}")
except Exception as e:
print(f" โ push_to_hub failed: {e}")
print(" Tip: `huggingface-cli login` or set HF_TOKEN env var first.")
return episode_rewards
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ENTRY POINT
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="LifeStack GRPO Training",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Smoke test (CPU, no GPU needed)
python train_trl.py --dry-run
# Fresh full run
python train_trl.py --stages 5 --prompts-per-stage 200
# Resume after Colab / Kaggle session cut
python train_trl.py --resume
# Manually restart from stage 3
python train_trl.py --start-stage 3
# Run multi-step episodes with the trained model
python train_trl.py --full-episode --output-dir ./lifestack_model
# Train then push to HuggingFace Hub
python train_trl.py --stages 5 --push-to-hub --hub-repo-id username/lifestack-grpo
"""
)
parser.add_argument(
"--dry-run", action="store_true",
help="Run 1 training step on 4 prompts to validate the full pipeline (no GPU required)."
)
parser.add_argument(
"--stages", type=int, default=5,
help="Number of curriculum stages (default: 5)."
)
parser.add_argument(
"--prompts-per-stage", type=int, default=100,
help="Prompts per curriculum stage (default: 100)."
)
parser.add_argument(
"--output-dir", type=str, default="./lifestack_model",
help="Directory to save the trained model."
)
parser.add_argument(
"--resume", action="store_true",
help="Resume from the last saved checkpoint + curriculum_state.json."
)
parser.add_argument(
"--start-stage", type=int, default=None,
help="Force-start from a specific stage number (1-indexed). Ignores curriculum_state.json."
)
parser.add_argument(
"--full-episode", action="store_true",
help="Run multi-step episodes with the trained model (post-training evaluation)."
)
parser.add_argument(
"--push-to-hub", action="store_true",
help="Push trained model to HuggingFace Hub after training or --full-episode."
)
parser.add_argument(
"--hub-repo-id", type=str, default="lifestack-grpo",
help="HuggingFace Hub repository ID for --push-to-hub (default: lifestack-grpo)."
)
args = parser.parse_args()
if args.dry_run:
dry_run(output_dir="./lifestack_model_dryrun")
elif args.full_episode:
run_full_episode(
model_dir=args.output_dir,
push_to_hub=args.push_to_hub,
hub_repo_id=args.hub_repo_id,
)
else:
trainer = train_curriculum(
n_stages=args.stages,
n_prompts_per_stage=args.prompts_per_stage,
output_dir=args.output_dir,
resume=args.resume,
start_stage=args.start_stage,
)
validate_saved_model(args.output_dir)
evaluate_and_plot(args.output_dir)
if args.push_to_hub:
try:
print(f"\nPushing to HuggingFace Hub: {args.hub_repo_id} ...")
trainer.model.push_to_hub(args.hub_repo_id)
trainer.processing_class.push_to_hub(args.hub_repo_id)
print(f"โ
Pushed โ https://huggingface.co/{args.hub_repo_id}")
except Exception as e:
print(f"โ push_to_hub failed: {e}")
|