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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
"""
Unified Training Orchestrator for LangGraph Multi-Agent MCTS with DeepMind-Style Learning.

Coordinates:
- HRM Agent
- TRM Agent
- Neural MCTS
- Policy-Value Network
- Self-play data generation
- Training loops
- Evaluation
- Checkpointing
"""

import time
from collections.abc import Callable
from pathlib import Path
from typing import Any

import torch
import torch.nn as nn
from torch.cuda.amp import GradScaler, autocast

from ..agents.hrm_agent import HRMLoss, create_hrm_agent
from ..agents.trm_agent import TRMLoss, create_trm_agent
from ..framework.mcts.neural_mcts import GameState, NeuralMCTS, SelfPlayCollector
from ..models.policy_value_net import (
    AlphaZeroLoss,
    create_policy_value_network,
)
from .performance_monitor import PerformanceMonitor, TimingContext
from .replay_buffer import Experience, PrioritizedReplayBuffer, collate_experiences
from .system_config import SystemConfig


class UnifiedTrainingOrchestrator:
    """
    Complete training pipeline integrating all framework components.

    This orchestrator manages:
    1. Self-play data generation using MCTS
    2. Neural network training (policy-value)
    3. HRM agent training
    4. TRM agent training
    5. Evaluation and checkpointing
    6. Performance monitoring
    """

    def __init__(
        self,
        config: SystemConfig,
        initial_state_fn: Callable[[], GameState],
        board_size: int = 19,
    ):
        """
        Initialize training orchestrator.

        Args:
            config: System configuration
            initial_state_fn: Function that returns initial game state
            board_size: Board/grid size for spatial games
        """
        self.config = config
        self.initial_state_fn = initial_state_fn
        self.board_size = board_size

        # Setup device
        self.device = config.device
        torch.manual_seed(config.seed)

        # Initialize performance monitor
        self.monitor = PerformanceMonitor(
            window_size=100,
            enable_gpu_monitoring=(self.device != "cpu"),
        )

        # Initialize components
        self._initialize_components()

        # Training state
        self.current_iteration = 0
        self.best_win_rate = 0.0
        self.best_model_path = None

        # Setup paths
        self._setup_paths()

        # Setup experiment tracking
        if config.use_wandb:
            self._setup_wandb()

    def _initialize_components(self):
        """Initialize all framework components."""
        print("Initializing components...")

        # Policy-Value Network
        self.policy_value_net = create_policy_value_network(
            config=self.config.neural_net,
            board_size=self.board_size,
            device=self.device,
        )

        print(f"  ✓ Policy-Value Network: {self.policy_value_net.get_parameter_count():,} parameters")

        # HRM Agent
        self.hrm_agent = create_hrm_agent(self.config.hrm, self.device)
        print(f"  ✓ HRM Agent: {self.hrm_agent.get_parameter_count():,} parameters")

        # TRM Agent
        self.trm_agent = create_trm_agent(
            self.config.trm, output_dim=self.config.neural_net.action_size, device=self.device
        )
        print(f"  ✓ TRM Agent: {self.trm_agent.get_parameter_count():,} parameters")

        # Neural MCTS
        self.mcts = NeuralMCTS(
            policy_value_network=self.policy_value_net,
            config=self.config.mcts,
            device=self.device,
        )
        print("  ✓ Neural MCTS initialized")

        # Self-play collector
        self.self_play_collector = SelfPlayCollector(mcts=self.mcts, config=self.config.mcts)

        # Optimizers
        self._setup_optimizers()

        # Loss functions
        self.pv_loss_fn = AlphaZeroLoss(value_loss_weight=1.0)
        self.hrm_loss_fn = HRMLoss(ponder_weight=0.01)
        self.trm_loss_fn = TRMLoss(
            task_loss_fn=nn.MSELoss(),
            supervision_weight_decay=self.config.trm.supervision_weight_decay,
        )

        # Replay buffer
        self.replay_buffer = PrioritizedReplayBuffer(
            capacity=self.config.training.buffer_size,
            alpha=0.6,
            beta_start=0.4,
            beta_frames=self.config.training.games_per_iteration * 10,
        )

        # Mixed precision scaler
        self.scaler = GradScaler() if self.config.use_mixed_precision else None

    def _setup_optimizers(self):
        """Setup optimizers and learning rate schedulers."""
        # Policy-Value optimizer
        self.pv_optimizer = torch.optim.SGD(
            self.policy_value_net.parameters(),
            lr=self.config.training.learning_rate,
            momentum=self.config.training.momentum,
            weight_decay=self.config.training.weight_decay,
        )

        # HRM optimizer
        self.hrm_optimizer = torch.optim.Adam(self.hrm_agent.parameters(), lr=1e-3)

        # TRM optimizer
        self.trm_optimizer = torch.optim.Adam(self.trm_agent.parameters(), lr=1e-3)

        # Learning rate scheduler for policy-value network
        if self.config.training.lr_schedule == "cosine":
            self.pv_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.pv_optimizer, T_max=100)
        elif self.config.training.lr_schedule == "step":
            self.pv_scheduler = torch.optim.lr_scheduler.StepLR(
                self.pv_optimizer,
                step_size=self.config.training.lr_decay_steps,
                gamma=self.config.training.lr_decay_gamma,
            )
        else:
            self.pv_scheduler = None

    def _setup_paths(self):
        """Setup directory paths."""
        self.checkpoint_dir = Path(self.config.checkpoint_dir)
        self.checkpoint_dir.mkdir(parents=True, exist_ok=True)

        self.data_dir = Path(self.config.data_dir)
        self.data_dir.mkdir(parents=True, exist_ok=True)

        self.log_dir = Path(self.config.log_dir)
        self.log_dir.mkdir(parents=True, exist_ok=True)

    def _setup_wandb(self):
        """Setup Weights & Biases experiment tracking."""
        try:
            import wandb

            wandb.init(
                project=self.config.wandb_project,
                entity=self.config.wandb_entity,
                config=self.config.to_dict(),
                name=f"run_{time.strftime('%Y%m%d_%H%M%S')}",
            )
            print("  ✓ Weights & Biases initialized")
        except ImportError:
            print("  ⚠️  wandb not installed, skipping")
            self.config.use_wandb = False

    async def train_iteration(self, iteration: int) -> dict[str, Any]:
        """
        Execute single training iteration.

        Args:
            iteration: Current iteration number

        Returns:
            Dictionary of metrics
        """
        print(f"\n{'=' * 80}")
        print(f"Training Iteration {iteration}")
        print(f"{'=' * 80}")

        metrics = {}

        # Phase 1: Self-play data generation
        print("\n[1/5] Generating self-play data...")
        with TimingContext(self.monitor, "self_play_generation"):
            game_data = await self._generate_self_play_data()
            metrics["games_generated"] = len(game_data)
            print(f"  Generated {len(game_data)} training examples")

        # Phase 2: Policy-Value network training
        print("\n[2/5] Training Policy-Value Network...")
        with TimingContext(self.monitor, "pv_training"):
            pv_metrics = await self._train_policy_value_network()
            metrics.update(pv_metrics)

        # Phase 3: HRM agent training (optional, if using HRM)
        if hasattr(self, "hrm_agent"):
            print("\n[3/5] Training HRM Agent...")
            with TimingContext(self.monitor, "hrm_training"):
                hrm_metrics = await self._train_hrm_agent()
                metrics.update(hrm_metrics)

        # Phase 4: TRM agent training (optional, if using TRM)
        if hasattr(self, "trm_agent"):
            print("\n[4/5] Training TRM Agent...")
            with TimingContext(self.monitor, "trm_training"):
                trm_metrics = await self._train_trm_agent()
                metrics.update(trm_metrics)

        # Phase 5: Evaluation
        print("\n[5/5] Evaluation...")
        if iteration % self.config.training.checkpoint_interval == 0:
            eval_metrics = await self._evaluate()
            metrics.update(eval_metrics)

            # Save checkpoint if improved
            if eval_metrics.get("win_rate", 0) > self.best_win_rate:
                self.best_win_rate = eval_metrics["win_rate"]
                self._save_checkpoint(iteration, metrics, is_best=True)
                print(f"  ✓ New best model! Win rate: {self.best_win_rate:.2%}")

        # Log metrics
        self._log_metrics(iteration, metrics)

        # Performance check
        self.monitor.alert_if_slow()

        return metrics

    async def _generate_self_play_data(self) -> list[Experience]:
        """Generate training data from self-play games."""
        num_games = self.config.training.games_per_iteration

        # In production, this would use parallel actors
        # For simplicity, we'll do sequential self-play
        all_examples = []

        for game_idx in range(num_games):
            examples = await self.self_play_collector.play_game(
                initial_state=self.initial_state_fn(),
                temperature_threshold=self.config.mcts.temperature_threshold,
            )

            # Convert to Experience objects
            for ex in examples:
                all_examples.append(Experience(state=ex.state, policy=ex.policy_target, value=ex.value_target))

            if (game_idx + 1) % 5 == 0:
                print(f"  Generated {game_idx + 1}/{num_games} games...")

        # Add to replay buffer
        self.replay_buffer.add_batch(all_examples)

        return all_examples

    async def _train_policy_value_network(self) -> dict[str, float]:
        """Train policy-value network on replay buffer data."""
        if not self.replay_buffer.is_ready(self.config.training.batch_size):
            print("  Replay buffer not ready, skipping...")
            return {"policy_loss": 0.0, "value_loss": 0.0}

        self.policy_value_net.train()

        total_policy_loss = 0.0
        total_value_loss = 0.0
        num_batches = 10  # Train for 10 batches per iteration

        for _ in range(num_batches):
            # Sample batch
            experiences, indices, weights = self.replay_buffer.sample(self.config.training.batch_size)
            states, policies, values = collate_experiences(experiences)

            states = states.to(self.device)
            policies = policies.to(self.device)
            values = values.to(self.device)
            weights = torch.from_numpy(weights).to(self.device)

            # Forward pass
            if self.config.use_mixed_precision and self.scaler:
                with autocast():
                    policy_logits, value_pred = self.policy_value_net(states)
                    loss, loss_dict = self.pv_loss_fn(policy_logits, value_pred, policies, values)
                    # Apply importance sampling weights
                    loss = (loss * weights).mean()

                # Backward pass with mixed precision
                self.pv_optimizer.zero_grad()
                self.scaler.scale(loss).backward()
                self.scaler.step(self.pv_optimizer)
                self.scaler.update()
            else:
                policy_logits, value_pred = self.policy_value_net(states)
                loss, loss_dict = self.pv_loss_fn(policy_logits, value_pred, policies, values)
                loss = (loss * weights).mean()

                self.pv_optimizer.zero_grad()
                loss.backward()
                self.pv_optimizer.step()

            # Update priorities in replay buffer
            with torch.no_grad():
                td_errors = torch.abs(value_pred.squeeze() - values)
                self.replay_buffer.update_priorities(indices, td_errors.cpu().numpy())

            total_policy_loss += loss_dict["policy"]
            total_value_loss += loss_dict["value"]

            # Log losses
            self.monitor.log_loss(loss_dict["policy"], loss_dict["value"], loss_dict["total"])

        # Step learning rate scheduler
        if self.pv_scheduler:
            self.pv_scheduler.step()

        avg_policy_loss = total_policy_loss / num_batches
        avg_value_loss = total_value_loss / num_batches

        print(f"  Policy Loss: {avg_policy_loss:.4f}, Value Loss: {avg_value_loss:.4f}")

        return {"policy_loss": avg_policy_loss, "value_loss": avg_value_loss}

    async def _train_hrm_agent(self) -> dict[str, float]:
        """Train HRM agent (placeholder for domain-specific implementation)."""
        # This would require domain-specific data and tasks
        # For now, return dummy metrics
        return {"hrm_halt_step": 5.0, "hrm_ponder_cost": 0.1}

    async def _train_trm_agent(self) -> dict[str, float]:
        """Train TRM agent (placeholder for domain-specific implementation)."""
        # This would require domain-specific data and tasks
        # For now, return dummy metrics
        return {"trm_convergence_step": 8.0, "trm_final_residual": 0.01}

    async def _evaluate(self) -> dict[str, float]:
        """Evaluate current model against baseline."""
        # Simplified evaluation: play games against previous best
        # In production, this would be more sophisticated
        win_rate = 0.55  # Placeholder

        return {
            "win_rate": win_rate,
            "eval_games": self.config.training.evaluation_games,
        }

    def _save_checkpoint(self, iteration: int, metrics: dict, is_best: bool = False):
        """Save model checkpoint."""
        checkpoint = {
            "iteration": iteration,
            "policy_value_net": self.policy_value_net.state_dict(),
            "hrm_agent": self.hrm_agent.state_dict(),
            "trm_agent": self.trm_agent.state_dict(),
            "pv_optimizer": self.pv_optimizer.state_dict(),
            "hrm_optimizer": self.hrm_optimizer.state_dict(),
            "trm_optimizer": self.trm_optimizer.state_dict(),
            "config": self.config.to_dict(),
            "metrics": metrics,
            "best_win_rate": self.best_win_rate,
        }

        # Save regular checkpoint
        path = self.checkpoint_dir / f"checkpoint_iter_{iteration}.pt"
        torch.save(checkpoint, path)
        print(f"  ✓ Checkpoint saved: {path}")

        # Save best model
        if is_best:
            best_path = self.checkpoint_dir / "best_model.pt"
            torch.save(checkpoint, best_path)
            self.best_model_path = best_path
            print(f"  ✓ Best model saved: {best_path}")

    def _log_metrics(self, iteration: int, metrics: dict):
        """Log metrics to console and tracking systems."""
        print(f"\n[Metrics Summary - Iteration {iteration}]")
        for key, value in metrics.items():
            if isinstance(value, float):
                print(f"  {key}: {value:.4f}")
            else:
                print(f"  {key}: {value}")

        # Log to wandb
        if self.config.use_wandb:
            try:
                import wandb

                wandb_metrics = self.monitor.export_to_wandb(iteration)
                wandb_metrics.update(metrics)
                wandb.log(wandb_metrics, step=iteration)
            except Exception as e:
                print(f"  ⚠️  Failed to log to wandb: {e}")

    async def train(self, num_iterations: int):
        """
        Run complete training loop.

        Args:
            num_iterations: Number of training iterations
        """
        print("\n" + "=" * 80)
        print("Starting Training")
        print("=" * 80)
        print(f"Total iterations: {num_iterations}")
        print(f"Device: {self.device}")
        print(f"Mixed precision: {self.config.use_mixed_precision}")

        start_time = time.time()

        for iteration in range(1, num_iterations + 1):
            self.current_iteration = iteration

            try:
                _ = await self.train_iteration(iteration)

                # Check early stopping
                if self._should_early_stop(iteration):
                    print("\n⚠️  Early stopping triggered")
                    break

            except KeyboardInterrupt:
                print("\n⚠️  Training interrupted by user")
                break
            except Exception as e:
                print(f"\n❌ Error in iteration {iteration}: {e}")
                import traceback

                traceback.print_exc()
                break

        elapsed = time.time() - start_time
        print(f"\n{'=' * 80}")
        print(f"Training completed in {elapsed / 3600:.2f} hours")
        print(f"Best win rate: {self.best_win_rate:.2%}")
        print(f"{'=' * 80}\n")

        # Print final performance summary
        self.monitor.print_summary()

    def _should_early_stop(self, iteration: int) -> bool:
        """Check early stopping criteria."""
        # Placeholder: implement actual early stopping logic
        _ = iteration  # noqa: F841
        return False

    def load_checkpoint(self, path: str):
        """Load checkpoint from file."""
        checkpoint = torch.load(path, map_location=self.device, weights_only=True)

        self.policy_value_net.load_state_dict(checkpoint["policy_value_net"])
        self.hrm_agent.load_state_dict(checkpoint["hrm_agent"])
        self.trm_agent.load_state_dict(checkpoint["trm_agent"])

        self.pv_optimizer.load_state_dict(checkpoint["pv_optimizer"])
        self.hrm_optimizer.load_state_dict(checkpoint["hrm_optimizer"])
        self.trm_optimizer.load_state_dict(checkpoint["trm_optimizer"])

        self.current_iteration = checkpoint["iteration"]
        self.best_win_rate = checkpoint.get("best_win_rate", 0.0)

        print(f"✓ Loaded checkpoint from iteration {self.current_iteration}")