Spaces:
Running
Running
File size: 55,858 Bytes
4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 c40a1a9 0a7b128 c40a1a9 0a7b128 c40a1a9 0a7b128 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 c40a1a9 0a7b128 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 0a7b128 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 c40a1a9 0a7b128 c40a1a9 0a7b128 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 c40a1a9 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 0a7b128 4d76b14 c40a1a9 4d76b14 | 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 | """
Fleet Environment: Multi-agent GPU cluster management for mixed precision training.
This is the CORE environment for Theme 1 (Multi-Agent Interactions) + Fleet AI sub-theme.
Architecture:
- Multiple Training Agents: each manages precision for one model
- Oversight Agent: monitors all training runs for instability
- Resource Allocator: distributes GPU memory across competing models
The environment runs episodes where agents take turns making decisions,
sharing a cluster-wide GPU memory pool. All physics computations are
delegated to physics_model.py (reused from the single-agent environment).
Tasks:
1. fleet_precision β Agents assign precision to their models under shared memory
2. fleet_oversight β Oversight agent monitors all runs, detects crashes
3. fleet_resource β Agents negotiate GPU allocation from a shared pool
4. fleet_recovery β Handle a mid-training crash: diagnose + reallocate
"""
from typing import Dict, Any, Optional, List
import random
import copy
from environment.physics_model import (
BYTES_PER_PARAM,
STABILITY_SCORE,
THROUGHPUT_MULTIPLIER,
ACCURACY_PENALTY,
H100_COST_PER_HOUR_USD,
get_hardware_profile,
compute_training_cost,
score_precision_layer,
compute_hardware_safety,
)
def clamp_score(score: float) -> float:
"""Clamp score to the open interval (0.01, 0.99) for OpenEnv compliance."""
try:
val = float(score)
except (ValueError, TypeError):
val = 0.01
return float(max(0.01, min(0.99, val)))
class FleetEnvironment:
"""Multi-agent fleet environment managing multiple models on a shared GPU cluster."""
TASK_DEFS = [
{
"id": "fleet_precision",
"task_id": "fleet_precision",
"description": "Multiple agents assign precision formats to their models under a shared GPU memory budget",
"difficulty": "medium",
"max_steps": 5,
},
{
"id": "fleet_oversight",
"task_id": "fleet_oversight",
"description": "Oversight agent monitors loss trajectories from multiple simultaneous training runs to detect crashes",
"difficulty": "hard",
"max_steps": 5,
},
{
"id": "fleet_resource",
"task_id": "fleet_resource",
"description": "Agents negotiate GPU allocation from a shared cluster pool, balancing priorities and efficiency",
"difficulty": "hard",
"max_steps": 5,
},
{
"id": "fleet_recovery",
"task_id": "fleet_recovery",
"description": "Diagnose a mid-training crash, reallocate cluster resources, and recover fleet operations",
"difficulty": "medium-hard",
"max_steps": 3,
},
]
def __init__(self):
self.current_task: Optional[str] = None
self.scenario: Optional[Dict] = None
self.step_number: int = 0
self.is_done: bool = True
self.history: list = []
self.task_state: Dict[str, Any] = {}
def reset(self, task_id: str, scenario_id: Optional[str] = None) -> Dict[str, Any]:
"""Reset environment for a new fleet episode."""
from scenarios.fleet_scenarios import FLEET_SCENARIOS, FLEET_OVERSIGHT_SCENARIOS
self.current_task = task_id
self.step_number = 0
self.is_done = False
self.history = []
self.task_state = {}
if task_id == "fleet_precision":
return self._reset_fleet_precision(FLEET_SCENARIOS, scenario_id)
elif task_id == "fleet_oversight":
return self._reset_fleet_oversight(FLEET_OVERSIGHT_SCENARIOS, scenario_id)
elif task_id == "fleet_resource":
return self._reset_fleet_resource(FLEET_SCENARIOS, scenario_id)
elif task_id == "fleet_recovery":
return self._reset_fleet_recovery(FLEET_SCENARIOS, FLEET_OVERSIGHT_SCENARIOS, scenario_id)
else:
raise ValueError(f"Unknown fleet task: {task_id}")
def step(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""Process an action from the current agent."""
if self.is_done:
return {
"observation": None,
"reward": {"score": 0.01, "feedback": "Episode finished. Call /reset."},
"done": True,
"info": {},
}
if not isinstance(action, dict):
return {
"observation": None,
"reward": {"score": 0.01, "feedback": "Invalid action: expected JSON object."},
"done": self.is_done,
"info": {"invalid_action": True},
}
if self.current_task == "fleet_precision":
return self._step_fleet_precision(action)
elif self.current_task == "fleet_oversight":
return self._step_fleet_oversight(action)
elif self.current_task == "fleet_resource":
return self._step_fleet_resource(action)
elif self.current_task == "fleet_recovery":
return self._step_fleet_recovery(action)
return {
"observation": None,
"reward": {"score": 0.01, "feedback": "No active task."},
"done": True,
"info": {},
}
def state(self) -> Dict[str, Any]:
"""Get current environment state."""
return {
"task_id": self.current_task,
"current_task": self.current_task,
"current_scenario_id": self.scenario.get("scenario_id") if self.scenario else None,
"step_number": self.step_number,
"is_done": self.is_done,
"history": self.history,
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Phase 5: Inverse Reward Design (Anti-Reward Hacking)
# "The reward function is a PROXY for what we actually want. Penalize
# behaviors that optimize the proxy but violate the intent."
# β Seminar Note: Inverse Reward Design tells agent the rules are
# guidelines, preventing exploits like shutting down a rack to save power.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _detect_reward_hacking(self, action: dict, task_id: str) -> tuple:
"""Detect if agent is exploiting reward function loopholes.
Returns: (is_hacking: bool, penalty: float, reason: str)
"""
if task_id == "fleet_precision":
strategy = action.get("precision_strategy", {})
if not isinstance(strategy, dict) or not strategy:
return True, -0.4, "IRD: Empty or invalid precision strategy"
values = list(strategy.values())
# Hack: all low-bit (FP8/INT8) everywhere (crashes training on embedding+output)
if values and all(v in ("FP8", "INT8") for v in values):
return True, -0.5, "IRD: All-FP8/INT8 is degenerate β training would crash on embedding+output"
# Hack: all FP32 everywhere (no optimization, wastes fleet resources)
if values and all(v == "FP32" for v in values):
return True, -0.2, "IRD: All-FP32 wastes fleet resources β zero optimization"
elif task_id == "fleet_resource":
allocations = action.get("allocations", {})
if not isinstance(allocations, dict) or not allocations:
return True, -0.4, "IRD: Empty allocations are invalid"
# Hack: starve any model completely
for mid, alloc in allocations.items():
if isinstance(alloc, dict) and alloc.get("gpus", 0) == 0:
return True, -0.3, f"IRD: Starving {mid} with 0 GPUs is not acceptable fleet management"
elif task_id == "fleet_oversight":
action_type = action.get("action_type", "")
# Hack: flag on very first step with no analysis (trigger-happy)
if action_type == "flag_instability" and self.step_number == 0:
analysis = action.get("analysis", "") or ""
root_cause = action.get("root_cause", "") or ""
if len(analysis) < 15 and len(root_cause) < 10:
return True, -0.2, "IRD: Flagging without evidence on first window suggests gaming"
elif task_id == "fleet_recovery":
reasoning = action.get("reasoning", "") or ""
# Hack: submit near-empty responses to collect base score
if len(reasoning) < 5 and not action.get("root_cause") and not action.get("new_precision_strategy"):
return True, -0.3, "IRD: Near-empty recovery action exploits base score"
return False, 0.0, ""
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TASK 1: Fleet Precision Assignment
# Multiple agents assign precision to their models under shared memory.
# Each step, ONE agent configures ONE model's full precision strategy.
# Shared memory pool creates inter-agent dependency.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _reset_fleet_precision(self, scenarios: Dict, scenario_id: Optional[str] = None) -> Dict[str, Any]:
"""Initialize fleet precision assignment episode."""
scenario_key = scenario_id if scenario_id in scenarios else random.choice(list(scenarios.keys()))
self.scenario = copy.deepcopy(scenarios[scenario_key])
cluster = self.scenario["cluster"]
models = self.scenario["models"]
self.task_state = {
"models": models,
"cluster": cluster,
"current_agent_idx": 0, # Which agent's turn
"agent_configs": {}, # model_id -> precision_strategy
"agent_results": {}, # model_id -> computed metrics
"fleet_memory_used_gb": 0.0, # Running total across all models
"fleet_total_cost_usd": 0.0,
}
return self._fleet_precision_observation()
def _fleet_precision_observation(self) -> Dict[str, Any]:
"""Build observation for the current agent in fleet precision task."""
s = self.task_state
agent_idx = s["current_agent_idx"]
model = s["models"][agent_idx]
cluster = s["cluster"]
# Show what other agents have done so far
other_agents_summary = {}
for mid, config in s["agent_configs"].items():
result = s["agent_results"].get(mid, {})
other_agents_summary[mid] = {
"precision_strategy": config,
"memory_used_gb": result.get("memory_gb", 0),
"status": "configured",
}
remaining_memory = cluster["total_memory_gb"] - s["fleet_memory_used_gb"]
hw_profile = cluster.get("hardware_profile", "nvidia_h100")
profile = get_hardware_profile(hw_profile)
return {
"task_id": "fleet_precision",
"scenario_id": self.scenario["scenario_id"],
"cluster": {
"hardware_profile": hw_profile,
"vendor": cluster.get("vendor", profile["vendor"]),
"total_gpus": cluster["total_gpus"],
"total_memory_gb": cluster["total_memory_gb"],
"memory_used_gb": round(s["fleet_memory_used_gb"], 2),
"memory_remaining_gb": round(remaining_memory, 2),
},
"your_model": {
"model_id": model["model_id"],
"name": model["name"],
"total_params": model["total_params"],
"layer_distribution": model["layer_distribution"],
"priority": model["priority"],
"priority_reason": model["priority_reason"],
},
"other_agents": other_agents_summary,
"agents_remaining": len(s["models"]) - agent_idx - 1,
"available_precisions": list(profile["available_precisions"]),
"step_number": self.step_number + 1,
"total_steps": len(s["models"]),
}
def _step_fleet_precision(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""Process one agent's precision strategy for their model.
Integrates:
- Phase 2: Breadcrumb rewards (per-layer feedback signals)
- Phase 3: Hardware safety dashboard (thermal/power/memory checks)
- Phase 4: Difference Rewards (counterfactual fleet contribution)
- Phase 5: Inverse Reward Design (degenerate action detection)
"""
s = self.task_state
agent_idx = s["current_agent_idx"]
model = s["models"][agent_idx]
model_id = model["model_id"]
strategy = action.get("precision_strategy", {})
cluster = s["cluster"]
hw_profile = cluster.get("hardware_profile", "nvidia_h100")
profile = get_hardware_profile(hw_profile)
self.step_number += 1
# ββ Phase 5: IRD check β catch reward hacking before scoring ββ
is_hacking, ird_penalty, ird_reason = self._detect_reward_hacking(action, "fleet_precision")
if is_hacking:
self.history.append({
"step": self.step_number, "agent": model_id,
"strategy": strategy, "score": 0.01, "ird_blocked": True,
})
s["current_agent_idx"] += 1
done = s["current_agent_idx"] >= len(s["models"])
if done:
self.is_done = True
return {
"observation": None if done else self._fleet_precision_observation(),
"reward": {"score": 0.01, "feedback": ird_reason},
"done": done,
"info": {"ird_violation": True},
}
# Compute metrics using physics model
metrics = compute_training_cost(
total_params=model["total_params"],
precision_strategy=strategy,
layer_distribution=model["layer_distribution"],
hardware_profile=hw_profile,
)
# Reject hardware-unsupported precisions immediately (e.g., FP8 on Google TPU)
if metrics.get("unsupported_precisions"):
bad = ", ".join(metrics["unsupported_precisions"])
feedback = (
f"CRITICAL: UNSUPPORTED precision on {hw_profile} ({profile['vendor']}): {bad}. "
f"Supported: {profile['available_precisions']}."
)
self.history.append({
"step": self.step_number, "agent": model_id,
"strategy": strategy, "score": 0.01, "unsupported_precision": True,
})
s["current_agent_idx"] += 1
done = s["current_agent_idx"] >= len(s["models"])
if done:
self.is_done = True
return {
"observation": None if done else self._fleet_precision_observation(),
"reward": {"score": 0.01, "feedback": feedback},
"done": done,
"info": {"unsupported_precisions": metrics["unsupported_precisions"]},
}
# ββ Phase 3: Hardware safety dashboard ββ
hw_safety = compute_hardware_safety(
total_params=model["total_params"],
precision_strategy=strategy,
layer_distribution=model["layer_distribution"],
num_gpus=max(1, cluster["total_gpus"] // len(s["models"])),
gpu_memory_gb=cluster.get("gpu_memory_gb", profile["gpu_memory_gb"]),
hardware_profile=hw_profile,
)
s["agent_configs"][model_id] = strategy
s["agent_results"][model_id] = metrics
s["fleet_memory_used_gb"] += metrics["memory_gb"]
# ββ Phase 2: Breadcrumb rewards β per-layer feedback signals ββ
layer_scores = []
breadcrumbs = []
for layer_type in model["layer_distribution"]:
precision = strategy.get(layer_type, "FP32")
lscore, lfeedback = score_precision_layer(layer_type, precision, hardware_profile=hw_profile)
layer_scores.append(lscore)
# Breadcrumb: tell agent exactly which layers are good/bad
if lscore >= 0.95:
crumb = f"[OK] {layer_type}={precision}"
elif lscore >= 0.55:
crumb = f"[~~] {layer_type}={precision}"
else:
crumb = f"[!!] {layer_type}={precision}"
breadcrumbs.append(crumb)
avg_layer_score = sum(layer_scores) / max(len(layer_scores), 1)
# Memory penalty: if this agent's model exceeds fair share
fair_share_gb = cluster["total_memory_gb"] / len(s["models"])
if metrics["memory_gb"] > fair_share_gb * 1.5:
memory_penalty = 0.15
memory_feedback = f"WARNING: Using {metrics['memory_gb']}GB β exceeds fair share ({fair_share_gb:.0f}GB) by >50%!"
elif metrics["memory_gb"] > fair_share_gb:
memory_penalty = 0.05
memory_feedback = f"Note: Using {metrics['memory_gb']}GB (fair share: {fair_share_gb:.0f}GB)"
else:
memory_penalty = 0.0
memory_feedback = f"Good: Using {metrics['memory_gb']}GB β within fair share ({fair_share_gb:.0f}GB)"
# Stability check
stability_penalty = 0.0
if not metrics["estimated_stable"]:
stability_penalty = 0.3
memory_feedback += " | CRITICAL: Unstable configuration detected!"
# ββ Phase 3: Hardware safety penalty ββ
hw_penalty = 0.0
if not hw_safety["overall_safe"]:
hw_penalty = 0.1
memory_feedback += f" | HW DANGER: {hw_safety['thermal_risk']} thermal risk, mem={hw_safety['memory_utilization_pct']}%"
base_agent_score = avg_layer_score - memory_penalty - stability_penalty - hw_penalty
# ββ Phase 4: Difference Rewards (Seminar Note 4) ββ
# How much did THIS agent's choice affect the fleet?
# D_i = R(fleet) - R(fleet without agent_i)
fleet_memory = s["fleet_memory_used_gb"]
fleet_utilization = fleet_memory / cluster["total_memory_gb"]
fleet_reward_with = 1.0 - abs(fleet_utilization - 0.75) # ideal = 75% util
# Fleet reward WITHOUT this agent (counterfactual)
fleet_memory_without = fleet_memory - metrics["memory_gb"]
fleet_util_without = fleet_memory_without / cluster["total_memory_gb"]
fleet_reward_without = 1.0 - abs(fleet_util_without - 0.75)
# Difference reward: positive = you helped, negative = you hurt
difference = fleet_reward_with - fleet_reward_without
# Blend: 70% individual score + 30% difference reward
blended_score = 0.7 * base_agent_score + 0.3 * max(0, difference + 0.5)
agent_score = clamp_score(blended_score)
# ββ Phase 2: Breadcrumb feedback string ββ
breadcrumb_str = " | Layers: " + ", ".join(breadcrumbs)
feedback = (
f"Agent {model_id} ({model['name']}): score={agent_score:.3f}, "
f"mem={metrics['memory_gb']}GB, speedup={metrics['speedup_vs_fp32']}x, "
f"cost=${metrics['cost_usd']:,.0f}. {memory_feedback}{breadcrumb_str}"
)
self.history.append({
"step": self.step_number,
"agent": model_id,
"strategy": strategy,
"score": agent_score,
"metrics": metrics,
"hw_safety": hw_safety,
})
# Advance to next agent
s["current_agent_idx"] += 1
done = s["current_agent_idx"] >= len(s["models"])
if done:
self.is_done = True
# ββ Fleet-wide scoring ββ
fleet_total_cost = sum(r["cost_usd"] for r in s["agent_results"].values())
fleet_fp32_cost = sum(r["fp32_baseline_cost_usd"] for r in s["agent_results"].values())
fleet_savings = fleet_fp32_cost - fleet_total_cost
fleet_all_stable = all(r["estimated_stable"] for r in s["agent_results"].values())
# Exceeded total cluster memory?
memory_overflow = s["fleet_memory_used_gb"] > cluster["total_memory_gb"]
fleet_bonus = ""
if memory_overflow:
agent_score = clamp_score(agent_score - 0.3)
fleet_bonus = f" | FLEET FAIL: Total memory {s['fleet_memory_used_gb']:.0f}GB exceeds cluster capacity {cluster['total_memory_gb']}GB!"
elif fleet_all_stable:
agent_score = clamp_score(agent_score + 0.05)
fleet_bonus = f" | FLEET SUCCESS: All models stable. Total savings: ${fleet_savings:,.0f} ({fleet_fp32_cost:.0f} β {fleet_total_cost:.0f})"
else:
fleet_bonus = f" | FLEET WARNING: Some models have unstable configs. Savings: ${fleet_savings:,.0f}"
feedback += fleet_bonus
info = {
"fleet_summary": {
"total_memory_used_gb": round(s["fleet_memory_used_gb"], 2),
"cluster_capacity_gb": cluster["total_memory_gb"],
"memory_overflow": memory_overflow,
"total_cost_usd": round(fleet_total_cost, 0),
"fp32_baseline_cost_usd": round(fleet_fp32_cost, 0),
"fleet_savings_usd": round(fleet_savings, 0),
"all_stable": fleet_all_stable,
"per_model": {
mid: {
"strategy": s["agent_configs"][mid],
"memory_gb": res["memory_gb"],
"cost_usd": round(res["cost_usd"], 0),
"stable": res["estimated_stable"],
}
for mid, res in s["agent_results"].items()
},
},
}
else:
info = {"memory_used_so_far_gb": round(s["fleet_memory_used_gb"], 2)}
next_obs = None if done else self._fleet_precision_observation()
return {
"observation": next_obs,
"reward": {"score": clamp_score(agent_score), "feedback": feedback},
"done": done,
"info": info,
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TASK 2: Fleet Oversight Monitoring (Fleet AI Sub-theme)
# Oversight agent watches loss trajectories from ALL training runs.
# Must identify WHICH model is crashing, WHEN, and WHY.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _reset_fleet_oversight(self, scenarios: Dict, scenario_id: Optional[str] = None) -> Dict[str, Any]:
"""Initialize fleet oversight monitoring episode."""
scenario_key = scenario_id if scenario_id in scenarios else random.choice(list(scenarios.keys()))
self.scenario = copy.deepcopy(scenarios[scenario_key])
window_size = self.scenario["window_size"]
step_count = self.scenario["step_count"]
self.task_state = {
"window_idx": 0,
"total_windows": step_count // window_size,
"flagged": False,
"window_size": window_size,
}
return self._fleet_oversight_observation()
def _fleet_oversight_observation(self) -> Dict[str, Any]:
"""Build observation showing loss windows from ALL models simultaneously."""
s = self.task_state
ws = s["window_size"]
start = s["window_idx"] * ws
end = start + ws
# Build per-model loss windows
model_windows = {}
for model_id, traj_data in self.scenario["trajectories"].items():
window = traj_data["loss"][start:end]
# Replace NaN with None for JSON serialization
clean_window = [None if (v != v) else round(v, 4) for v in window]
model_windows[model_id] = {
"loss_window": clean_window,
"precision_config": traj_data["precision_config"],
}
return {
"task_id": "fleet_oversight",
"scenario_id": self.scenario["scenario_id"],
"description": self.scenario["description"],
"num_models": len(self.scenario["trajectories"]),
"model_trajectories": model_windows,
"total_training_steps": self.scenario["step_count"],
"steps_revealed_so_far": end,
"window_index": s["window_idx"],
"windows_remaining": s["total_windows"] - s["window_idx"] - 1,
}
def _step_fleet_oversight(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""Process oversight agent's monitoring decision.
Integrates:
- Phase 2: Breadcrumb progress signal (confidence ramps with safe scanning)
- Phase 5: IRD check for trigger-happy flagging
"""
s = self.task_state
gt = self.scenario["ground_truth"]
action_type = action.get("action_type", "continue_monitoring")
self.step_number += 1
# ββ Phase 5: IRD check ββ
is_hacking, ird_penalty, ird_reason = self._detect_reward_hacking(action, "fleet_oversight")
if is_hacking:
score = clamp_score(0.05)
feedback = ird_reason
self.is_done = True
self.history.append({"step": self.step_number, "action": action_type, "score": score, "ird_blocked": True})
return {
"observation": None,
"reward": {"score": score, "feedback": feedback},
"done": True,
"info": {"ird_violation": True},
}
if action_type == "flag_instability":
s["flagged"] = True
self.is_done = True
score, feedback = self._grade_fleet_flag(action, gt, s["window_idx"])
elif action_type == "continue_monitoring":
# BUG FIX: Check current window BEFORE incrementing
# (Composer found: was evaluating next window instead of observed one)
current_idx = s["window_idx"]
ws = s["window_size"]
has_issues = False
for model_id, traj_data in self.scenario["trajectories"].items():
window = traj_data["loss"][current_idx*ws:(current_idx+1)*ws]
if any(isinstance(v, (int, float)) and (v != v or v > 100.0) for v in window):
has_issues = True
break
s["window_idx"] += 1
if s["window_idx"] >= s["total_windows"]:
# End of all windows
self.is_done = True
if gt["crashing_model"] is not None:
score = 0.10
feedback = (
"MISSED: Fleet had a crashing model but oversight never flagged it. "
f"Model {gt['crashing_model']} crashed at step {gt['failure_step']}."
)
else:
score = 0.95
feedback = "CORRECT: All models healthy β no instability to flag. Good monitoring."
else:
# ββ Phase 2: Breadcrumb progress signal ββ
# Score gradually increases as more of the timeline is safely scanned
progress = s["window_idx"] / max(s["total_windows"], 1)
if has_issues:
score = 0.30
feedback = f"Window {s['window_idx']}: Issues visible in fleet β consider flagging. [scanned {progress*100:.0f}%]"
else:
# Breadcrumb: reward increases with safe progress
score = 0.40 + (0.15 * progress)
feedback = f"Window {s['window_idx']}: Fleet looks stable. [scanned {progress*100:.0f}%, confidence={score:.2f}]"
self.is_done = False
score = clamp_score(score)
self.history.append({
"step": self.step_number,
"action": action_type,
"score": score,
})
next_obs = None if self.is_done else self._fleet_oversight_observation()
return {
"observation": next_obs,
"reward": {"score": score, "feedback": feedback},
"done": self.is_done,
"info": {},
}
def _grade_fleet_flag(self, action: Dict, gt: Dict, window_idx: int) -> tuple:
"""Grade an oversight agent's instability flag."""
# False alarm β nothing was wrong
if gt["crashing_model"] is None:
return 0.10, "FALSE ALARM: Fleet is healthy. Flagging incorrectly wastes resources and causes unnecessary rollbacks."
score = 0.30
parts = ["Correctly detected fleet instability (+0.30)."]
# Did agent identify the RIGHT model?
raw_model = action.get("flagged_model")
flagged_model = raw_model if isinstance(raw_model, str) else ""
if flagged_model and flagged_model.lower() == gt["crashing_model"].lower():
score += 0.25
parts.append(f"Identified correct model: {flagged_model} (+0.25).")
elif flagged_model:
score += 0.05
parts.append(f"Identified wrong model: {flagged_model} (actual: {gt['crashing_model']}) (+0.05).")
# Did agent pinpoint the failure step?
raw_step = action.get("flagged_step")
flagged_step = None
if raw_step is not None and not isinstance(raw_step, bool):
try:
flagged_step = int(raw_step)
except (ValueError, TypeError):
flagged_step = None
actual_step = gt["failure_step"]
if flagged_step is not None and actual_step > 0:
diff = abs(flagged_step - actual_step)
if diff <= 3:
score += 0.20
parts.append(f"Pinpointed failure step within 3 of actual (+0.20).")
elif diff <= 10:
score += 0.12
parts.append(f"Close to failure step (off by {diff}) (+0.12).")
else:
score += 0.03
parts.append(f"Failure step off by {diff} (+0.03).")
# Root cause analysis
root_cause = (action.get("root_cause") or "").lower()
matches = sum(1 for kw in gt["cause_keywords"] if kw.lower() in root_cause)
if gt["cause_keywords"]:
cause_score = 0.25 * (matches / len(gt["cause_keywords"]))
score += round(cause_score, 2)
parts.append(f"Root cause: matched {matches}/{len(gt['cause_keywords'])} keywords (+{round(cause_score, 2)}).")
return clamp_score(round(score, 3)), " ".join(parts)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TASK 3: Fleet Resource Negotiation
# Agents bid for GPU allocation from a shared cluster.
# Must balance priorities, efficiency, and fairness.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _reset_fleet_resource(self, scenarios: Dict, scenario_id: Optional[str] = None) -> Dict[str, Any]:
"""Initialize fleet resource negotiation episode."""
scenario_key = scenario_id if scenario_id in scenarios else random.choice(list(scenarios.keys()))
self.scenario = copy.deepcopy(scenarios[scenario_key])
cluster = self.scenario["cluster"]
models = self.scenario["models"]
self.task_state = {
"iteration": 0,
"max_iterations": self.scenario.get("max_steps_per_task", 5),
"models": models,
"cluster": cluster,
"best_score": 0.0,
"best_allocation": None,
"prev_feedback": None,
"prev_result": None,
}
return self._fleet_resource_observation()
def _fleet_resource_observation(self) -> Dict[str, Any]:
"""Build observation for resource negotiation."""
s = self.task_state
cluster = s["cluster"]
model_specs = []
for m in s["models"]:
model_specs.append({
"model_id": m["model_id"],
"name": m["name"],
"total_params": m["total_params"],
"priority": m["priority"],
"priority_reason": m["priority_reason"],
})
hw_profile = cluster.get("hardware_profile", "nvidia_h100")
profile = get_hardware_profile(hw_profile)
return {
"task_id": "fleet_resource",
"scenario_id": self.scenario["scenario_id"],
"cluster": {
"hardware_profile": hw_profile,
"vendor": cluster.get("vendor", profile["vendor"]),
"total_gpus": cluster["total_gpus"],
"gpu_memory_gb": cluster["gpu_memory_gb"],
"total_memory_gb": cluster["total_memory_gb"],
"cost_per_gpu_hour_usd": cluster["cost_per_gpu_hour_usd"],
},
"models": model_specs,
"available_precisions": list(profile["available_precisions"]),
"iterations_remaining": s["max_iterations"] - s["iteration"],
"best_score_so_far": round(s["best_score"], 3),
"previous_result": s["prev_result"],
"previous_feedback": s["prev_feedback"],
}
def _step_fleet_resource(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""Process a resource allocation proposal.
Integrates:
- Phase 2: Breadcrumb delta (shows improvement from previous iteration)
- Phase 4: Difference Rewards (counterfactual vs naive equal-split)
- Phase 5: IRD check for degenerate allocations
"""
s = self.task_state
cluster = s["cluster"]
allocations = action.get("allocations", {})
self.step_number += 1
s["iteration"] += 1
# ββ Phase 5: IRD check ββ
is_hacking, ird_penalty, ird_reason = self._detect_reward_hacking(action, "fleet_resource")
if is_hacking:
s["prev_feedback"] = ird_reason
done = s["iteration"] >= s["max_iterations"]
if done:
self.is_done = True
self.history.append({"step": self.step_number, "allocations": allocations, "score": 0.01, "ird_blocked": True})
return {
"observation": None if done else self._fleet_resource_observation(),
"reward": {"score": 0.01, "feedback": ird_reason},
"done": done,
"info": {"ird_violation": True},
}
# allocations = { model_id: { "gpus": N, "precision_strategy": {...} } }
hw_profile = cluster.get("hardware_profile", "nvidia_h100")
profile = get_hardware_profile(hw_profile)
total_gpus_assigned = 0
fleet_cost = 0.0
fleet_fp32_cost = 0.0
per_model_results = {}
all_valid = True
issues = []
for model in s["models"]:
mid = model["model_id"]
alloc = allocations.get(mid, {})
if not isinstance(alloc, dict):
all_valid = False
issues.append(f"{mid}: invalid allocation entry")
per_model_results[mid] = {"gpus": 0, "valid": False}
continue
raw_gpus = alloc.get("gpus", 0)
if isinstance(raw_gpus, bool):
gpus = -1
else:
try:
gpus = int(raw_gpus)
except (ValueError, TypeError):
gpus = -1
default_ffn = profile.get("ideal_ffn", "FP8")
strategy = alloc.get("precision_strategy", {
"embedding": "FP32", "attention": "BF16",
"ffn": default_ffn, "layernorm": "BF16", "output": "FP32",
})
if not isinstance(strategy, dict):
all_valid = False
issues.append(f"{mid}: invalid precision_strategy")
per_model_results[mid] = {"gpus": max(0, gpus), "valid": False}
continue
if gpus <= 0:
all_valid = False
issues.append(f"{mid}: assigned invalid GPU count ({raw_gpus})")
per_model_results[mid] = {"gpus": 0, "valid": False}
continue
total_gpus_assigned += gpus
# Compute metrics with actual allocated GPUs and cluster rate
metrics = compute_training_cost(
total_params=model["total_params"],
precision_strategy=strategy,
layer_distribution=model["layer_distribution"],
num_gpus=gpus,
cost_per_gpu_hour=cluster["cost_per_gpu_hour_usd"],
hardware_profile=hw_profile,
)
if metrics.get("unsupported_precisions"):
all_valid = False
issues.append(f"{mid}: unsupported precision {metrics['unsupported_precisions']}")
# Memory check: model must fit in allocated GPU memory
available_mem = gpus * cluster["gpu_memory_gb"]
mem_ok = metrics["memory_gb"] <= available_mem
if not mem_ok:
all_valid = False
issues.append(f"{mid}: needs {metrics['memory_gb']}GB but only has {available_mem}GB ({gpus} GPUs)")
per_model_results[mid] = {
"gpus": gpus,
"memory_gb": metrics["memory_gb"],
"available_memory_gb": available_mem,
"memory_fits": mem_ok,
"cost_usd": round(metrics["cost_usd"], 0),
"training_days": metrics["training_days"],
"speedup": metrics["speedup_vs_fp32"],
"stable": metrics["estimated_stable"],
"valid": mem_ok and metrics["estimated_stable"] and not metrics.get("unsupported_precisions"),
}
fleet_cost += metrics["cost_usd"]
fleet_fp32_cost += metrics["fp32_baseline_cost_usd"]
# Check total GPU constraint
if total_gpus_assigned > cluster["total_gpus"]:
all_valid = False
issues.append(f"Total GPUs assigned ({total_gpus_assigned}) exceeds cluster ({cluster['total_gpus']})")
elif total_gpus_assigned < cluster["total_gpus"]:
issues.append(f"Underutilization: only {total_gpus_assigned}/{cluster['total_gpus']} GPUs assigned")
# ββ Scoring ββ
if not all_valid:
score = 0.01
feedback = f"INVALID allocation: {'; '.join(issues)}. {s['max_iterations'] - s['iteration']} iterations remaining."
else:
# Score components:
# 1. Cost efficiency (how much saved vs FP32)
savings_pct = (fleet_fp32_cost - fleet_cost) / max(fleet_fp32_cost, 1)
cost_score = min(1.0, savings_pct / 0.5) # Normalize: 50% savings = perfect
# 2. GPU utilization
util = total_gpus_assigned / cluster["total_gpus"]
util_score = min(1.0, util)
# 3. Priority alignment (higher priority models get more resources)
priority_score = self._score_priority_alignment(s["models"], allocations, cluster)
# 4. All stable
all_stable = all(r.get("stable", False) for r in per_model_results.values())
stability_bonus = 0.1 if all_stable else 0.0
base_score = 0.3 * cost_score + 0.25 * util_score + 0.25 * priority_score + 0.1 + stability_bonus
# ββ Phase 4: Difference Rewards ββ
# Counterfactual: what if we used naive equal-split allocation?
naive_gpus = max(1, cluster["total_gpus"] // max(len(s["models"]), 1))
naive_cost = 0.0
for model in s["models"]:
naive_metrics = compute_training_cost(
total_params=model["total_params"],
precision_strategy={"embedding": "FP32", "attention": "BF16",
"ffn": "BF16", "layernorm": "BF16", "output": "FP32"},
layer_distribution=model["layer_distribution"],
num_gpus=naive_gpus,
cost_per_gpu_hour=cluster["cost_per_gpu_hour_usd"],
hardware_profile=hw_profile,
)
naive_cost += naive_metrics["cost_usd"]
# Agent's improvement over naive baseline
diff_reward = (naive_cost - fleet_cost) / max(naive_cost, 1)
# Blend: 80% base + 20% difference reward
score = clamp_score(0.8 * base_score + 0.2 * max(0, diff_reward + 0.3))
fleet_savings = fleet_fp32_cost - fleet_cost
# ββ Phase 2: Breadcrumb delta ββ
prev_score = s.get("prev_score", 0.0)
delta = score - prev_score
delta_str = f" | {'β' if delta > 0 else 'β'}{abs(delta):.3f} from last" if prev_score > 0 else ""
feedback = (
f"Valid allocation! Fleet cost: ${fleet_cost:,.0f} (savings: ${fleet_savings:,.0f}). "
f"GPU utilization: {util*100:.0f}%. Priority alignment: {priority_score:.2f}. "
f"All stable: {all_stable}. Score: {score:.3f}. "
f"{s['max_iterations'] - s['iteration']} iterations remaining.{delta_str}"
)
s["prev_score"] = score
if score > s["best_score"]:
s["best_score"] = score
s["best_allocation"] = allocations
s["prev_result"] = per_model_results
s["prev_feedback"] = feedback
done = s["iteration"] >= s["max_iterations"]
if done:
self.is_done = True
score = clamp_score(score)
self.history.append({
"step": self.step_number,
"allocations": allocations,
"score": score,
})
next_obs = None if done else self._fleet_resource_observation()
return {
"observation": next_obs,
"reward": {"score": score, "feedback": feedback},
"done": done,
"info": {"per_model": per_model_results},
}
def _score_priority_alignment(self, models: List, allocations: Dict, cluster: Dict) -> float:
"""Score how well GPU allocation aligns with model priorities."""
total_priority = sum(m["priority"] for m in models)
if total_priority == 0:
return 0.5
score = 0.0
for m in models:
mid = m["model_id"]
alloc = allocations.get(mid, {})
gpus = alloc.get("gpus", 0)
expected_share = m["priority"] / total_priority
actual_share = gpus / max(cluster["total_gpus"], 1)
# Reward for being close to priority-proportional allocation
alignment = 1.0 - min(1.0, abs(expected_share - actual_share) / max(expected_share, 0.01))
score += alignment * expected_share
return min(1.0, score)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TASK 4: Fleet Recovery
# One model crashes mid-training. Agent must diagnose and reallocate.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _reset_fleet_recovery(self, fleet_scenarios: Dict, oversight_scenarios: Dict, scenario_id: Optional[str] = None) -> Dict[str, Any]:
"""Initialize fleet recovery episode β a crash has occurred."""
# Pick a scenario with a crash
crash_scenarios = {k: v for k, v in oversight_scenarios.items()
if v["ground_truth"]["crashing_model"] is not None}
scenario_key = None
if scenario_id is not None and scenario_id.startswith("recovery_"):
key = scenario_id[9:]
if key in crash_scenarios:
scenario_key = key
if scenario_key is None:
scenario_key = random.choice(list(crash_scenarios.keys()))
oversight = copy.deepcopy(crash_scenarios[scenario_key])
# Get the fleet config
fleet_key = oversight.get("fleet_id", "medium_fleet")
fleet = copy.deepcopy(fleet_scenarios.get(fleet_key, fleet_scenarios["medium_fleet"]))
crashed_model_id = oversight["ground_truth"]["crashing_model"]
self.scenario = {
"scenario_id": f"recovery_{scenario_key}",
"fleet": fleet,
"oversight": oversight,
"crashed_model_id": crashed_model_id,
}
# Show the crash context
crashed_traj = oversight["trajectories"][crashed_model_id]
crash_step = oversight["ground_truth"]["failure_step"]
crash_window_start = max(0, crash_step - 10)
crash_window = crashed_traj["loss"][crash_window_start:crash_step + 5]
clean_crash_window = [None if (v != v) else round(v, 4) for v in crash_window]
self.task_state = {
"iteration": 0,
"max_iterations": 3,
"phase": "diagnose", # diagnose β reallocate β verify
"crashed_model_id": crashed_model_id,
"crash_step": crash_step,
"crash_window": clean_crash_window,
"crashed_config": crashed_traj["precision_config"],
"fleet": fleet,
"best_score": 0.0,
"prev_feedback": None,
}
return self._fleet_recovery_observation()
def _fleet_recovery_observation(self) -> Dict[str, Any]:
"""Build observation for fleet recovery."""
s = self.task_state
fleet = s["fleet"]
cluster = fleet["cluster"]
# Build model info
model_summaries = []
for m in fleet["models"]:
status = "crashed" if m["model_id"] == s["crashed_model_id"] else "running"
model_summaries.append({
"model_id": m["model_id"],
"name": m["name"],
"total_params": m["total_params"],
"priority": m["priority"],
"status": status,
})
obs = {
"task_id": "fleet_recovery",
"scenario_id": self.scenario["scenario_id"],
"phase": s["phase"],
"cluster": {
"total_gpus": cluster["total_gpus"],
"total_memory_gb": cluster["total_memory_gb"],
},
"models": model_summaries,
"crashed_model": {
"model_id": s["crashed_model_id"],
"crash_step": s["crash_step"],
"loss_around_crash": s["crash_window"],
"precision_config": s["crashed_config"],
},
"iterations_remaining": s["max_iterations"] - s["iteration"],
"previous_feedback": s["prev_feedback"],
}
return obs
def _step_fleet_recovery(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""Process a fleet recovery action.
Integrates:
- Phase 4: Difference Rewards in reallocation (vs generic safe config)
- Phase 5: IRD check for empty/gaming recovery actions
"""
s = self.task_state
self.step_number += 1
s["iteration"] += 1
gt = self.scenario["oversight"]["ground_truth"]
# ββ Phase 5: IRD check ββ
is_hacking, ird_penalty, ird_reason = self._detect_reward_hacking(action, "fleet_recovery")
if is_hacking:
s["prev_feedback"] = ird_reason
done = s["iteration"] >= s["max_iterations"]
if done:
self.is_done = True
self.history.append({"step": self.step_number, "phase": s["phase"], "score": 0.01, "ird_blocked": True})
return {
"observation": None if done else self._fleet_recovery_observation(),
"reward": {"score": 0.01, "feedback": ird_reason},
"done": done,
"info": {"ird_violation": True},
}
phase = s["phase"]
if phase == "diagnose":
# Agent must identify root cause
score, feedback = self._grade_recovery_diagnosis(action, gt)
s["phase"] = "reallocate"
elif phase == "reallocate":
# Agent proposes new precision for crashed model + resource reallocation
score, feedback = self._grade_recovery_reallocation(action, s)
s["phase"] = "verify"
else: # verify
# Agent confirms the recovery plan
score, feedback = self._grade_recovery_verification(action, s)
s["phase"] = "done"
if score > s["best_score"]:
s["best_score"] = score
s["prev_feedback"] = feedback
done = s["iteration"] >= s["max_iterations"] or s["phase"] == "done"
if done:
self.is_done = True
score = clamp_score(score)
self.history.append({"step": self.step_number, "phase": phase, "score": score})
next_obs = None if done else self._fleet_recovery_observation()
return {
"observation": next_obs,
"reward": {"score": score, "feedback": feedback},
"done": done,
"info": {},
}
def _grade_recovery_diagnosis(self, action: Dict, gt: Dict) -> tuple:
"""Grade diagnosis of the crash."""
score = 0.20
parts = ["Diagnosis submitted (+0.20 base)."]
# Root cause
root_cause = (action.get("root_cause") or "").lower()
matches = sum(1 for kw in gt["cause_keywords"] if kw.lower() in root_cause)
if gt["cause_keywords"]:
cause_score = 0.50 * (matches / len(gt["cause_keywords"]))
score += round(cause_score, 2)
parts.append(f"Root cause: matched {matches}/{len(gt['cause_keywords'])} keywords (+{round(cause_score, 2)}).")
# Identified correct model
diagnosed_model = (action.get("diagnosed_model") or "").lower()
if diagnosed_model == gt["crashing_model"].lower():
score += 0.30
parts.append("Correctly identified crashing model (+0.30).")
return clamp_score(round(score, 3)), " ".join(parts)
def _grade_recovery_reallocation(self, action: Dict, state: Dict) -> tuple:
"""Grade the reallocation plan for the crashed model.
Phase 4: Includes Difference Reward comparing agent's fix to a generic
safe fallback (BF16 everywhere). Agent gets bonus for being more
efficient than the naive fix while maintaining stability.
"""
new_strategy = action.get("new_precision_strategy", {})
if not isinstance(new_strategy, dict) or not new_strategy:
return 0.01, "Invalid or empty new_precision_strategy."
fleet = state["fleet"]
hw_profile = fleet.get("cluster", {}).get("hardware_profile", "nvidia_h100")
crashed_id = state["crashed_model_id"]
# Find the crashed model
crashed_model = None
for m in fleet["models"]:
if m["model_id"] == crashed_id:
crashed_model = m
break
if not crashed_model:
return 0.01, "Could not find crashed model in fleet."
# Compute new metrics
metrics = compute_training_cost(
total_params=crashed_model["total_params"],
precision_strategy=new_strategy,
layer_distribution=crashed_model["layer_distribution"],
hardware_profile=hw_profile,
)
score = 0.20
parts = ["Reallocation plan submitted (+0.20 base)."]
if metrics["estimated_stable"]:
score += 0.40
parts.append(f"New config is STABLE (+0.40). Mem={metrics['memory_gb']}GB, Speed={metrics['speedup_vs_fp32']}x.")
else:
parts.append("WARNING: New config is still UNSTABLE.")
# Did they fix the problematic layer?
old_config = state["crashed_config"]
changes = sum(1 for k in new_strategy if new_strategy.get(k) != old_config.get(k))
if changes > 0:
adapt_bonus = min(0.20, changes * 0.07)
score += adapt_bonus
parts.append(f"Changed {changes} layer(s) from crashed config (+{adapt_bonus:.2f}).")
# Efficiency bonus
if metrics["speedup_vs_fp32"] > 1.5:
score += 0.10
parts.append(f"Efficient recovery: {metrics['speedup_vs_fp32']}x speedup (+0.10).")
# ββ Phase 4: Difference Reward vs generic safe config ββ
generic_safe = {"embedding": "FP32", "attention": "BF16",
"ffn": "BF16", "layernorm": "BF16", "output": "FP32"}
generic_metrics = compute_training_cost(
total_params=crashed_model["total_params"],
precision_strategy=generic_safe,
layer_distribution=crashed_model["layer_distribution"],
)
if metrics["estimated_stable"] and metrics["speedup_vs_fp32"] > generic_metrics["speedup_vs_fp32"]:
diff_bonus = min(0.10, (metrics["speedup_vs_fp32"] - generic_metrics["speedup_vs_fp32"]) * 0.15)
score += round(diff_bonus, 3)
parts.append(f"Better than generic fix: {metrics['speedup_vs_fp32']}x vs {generic_metrics['speedup_vs_fp32']}x (+{diff_bonus:.3f}).")
return clamp_score(round(score, 3)), " ".join(parts)
def _grade_recovery_verification(self, action: Dict, state: Dict) -> tuple:
"""Grade the verification step."""
reasoning = action.get("reasoning", "")
confidence = action.get("confidence", "")
score = 0.50
parts = ["Verification submitted (+0.50 base)."]
# Reward thorough reasoning
if len(reasoning) > 50:
score += 0.20
parts.append("Detailed recovery reasoning provided (+0.20).")
if "stable" in reasoning.lower() or "safe" in reasoning.lower():
score += 0.10
parts.append("Stability considerations mentioned (+0.10).")
if "monitor" in reasoning.lower() or "watch" in reasoning.lower():
score += 0.10
parts.append("Post-recovery monitoring plan mentioned (+0.10).")
return clamp_score(round(score, 3)), " ".join(parts)
|