File size: 27,643 Bytes
0c28a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Improved GRPO training script for WhipStudio ML Debug Environment.

This script trains Qwen2.5-1.5B-Coder (or similar) to debug broken PyTorch scripts
using Group Relative Policy Optimization (GRPO) with the WhipStudio environment
as the reward oracle.

Improvements over basic train_grpo.py:
1. Memory-efficient training with 4-bit quantization
2. LoRA fine-tuning for reduced VRAM usage
3. Curriculum learning (easier tasks first)
4. Gradient checkpointing for large contexts
5. Checkpoint saving with best model tracking
6. Early stopping based on validation scores
7. Wandb/TensorBoard logging support

Requirements:
    pip install trl>=0.15.0 transformers>=4.46.0 datasets torch httpx
    pip install accelerate peft bitsandbytes wandb

Usage:
    # Basic training
    python improved_agent.py \
        --env_url https://your-space.hf.space \
        --output_dir ./whipstudio-debugger

    # Memory-efficient training (8GB VRAM)
    python improved_agent.py \
        --env_url https://your-space.hf.space \
        --use_4bit \
        --use_lora \
        --gradient_checkpointing \
        --output_dir ./whipstudio-debugger-lora

    # Full training with wandb logging
    python improved_agent.py \
        --env_url https://your-space.hf.space \
        --use_wandb \
        --wandb_project whipstudio \
        --num_iterations 100 \
        --output_dir ./whipstudio-debugger
"""

import argparse
import json
import math
import os
import random
import re
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional

import httpx
import torch
from datasets import Dataset
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
)

# TRL imports
try:
    from trl import GRPOConfig, GRPOTrainer
except ImportError:
    raise ImportError("Please install trl>=0.15.0: pip install trl")

# PEFT imports (optional)
try:
    from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
    PEFT_AVAILABLE = True
except ImportError:
    PEFT_AVAILABLE = False

# Wandb import (optional)
try:
    import wandb
    WANDB_AVAILABLE = True
except ImportError:
    WANDB_AVAILABLE = False


# ══════════════════════════════════════════════════════════════════════════════
# Constants
# ══════════════════════════════════════════════════════════════════════════════

SYSTEM_PROMPT = """You are an expert PyTorch debugging agent.
You receive a broken training script and must fix ALL bugs.
Return ONLY the complete corrected Python code. No markdown, no backticks, no explanation.
The script must print metrics in the format specified by the task description.
Keep all torch.manual_seed() calls intact.
Wrap metrics in ##METRICS_START## and ##METRICS_END## markers."""

# Task ordering by difficulty for curriculum learning
TASK_DIFFICULTY = {
    "task1": 1,  # Easy: broken loop
    "task4": 2,  # Medium: wrong loss
    "task5": 2,  # Medium: frozen backbone
    "task2": 3,  # Medium: NaN loss (tricky)
    "task3": 4,  # Hard: OOM + leakage
}

ALL_TASKS = list(TASK_DIFFICULTY.keys())


# ══════════════════════════════════════════════════════════════════════════════
# Environment Client
# ══════════════════════════════════════════════════════════════════════════════

class WhipStudioEnv:
    """Client for the WhipStudio RL environment."""

    def __init__(self, env_url: str, timeout: float = 180.0):
        self.env_url = env_url.rstrip("/")
        self.timeout = httpx.Timeout(timeout, connect=15.0)
        self._task_cache: dict[str, dict] = {}

    def reset(self, task_id: str) -> dict:
        """Reset environment and return observation."""
        with httpx.Client(timeout=self.timeout) as client:
            resp = client.post(f"{self.env_url}/reset", json={"task_id": task_id})
            resp.raise_for_status()
            data = resp.json()
            obs = data.get("observation", data)
            self._task_cache[task_id] = obs
            return obs

    def step(self, fixed_code: str, attempt: int = 1) -> dict:
        """Submit a fix and return the full step result."""
        payload = {
            "action": {
                "fixed_code": fixed_code,
                "attempt_number": attempt,
            }
        }
        with httpx.Client(timeout=self.timeout) as client:
            resp = client.post(f"{self.env_url}/step", json=payload)
            resp.raise_for_status()
            return resp.json()

    def get_task_obs(self, task_id: str) -> dict:
        """Get cached observation or reset to obtain it."""
        if task_id not in self._task_cache:
            self.reset(task_id)
        return self._task_cache[task_id]

    def health_check(self) -> bool:
        """Verify the environment is reachable."""
        try:
            with httpx.Client(timeout=httpx.Timeout(10.0)) as client:
                resp = client.get(f"{self.env_url}/health")
                return resp.status_code == 200
        except Exception:
            return False


# ══════════════════════════════════════════════════════════════════════════════
# Prompt Utilities
# ══════════════════════════════════════════════════════════════════════════════

def build_user_prompt(task_description: str, buggy_code: str) -> str:
    """Build the user prompt for the model."""
    return f"Task: {task_description}\n\nBuggy code:\n{buggy_code}"


def format_chat(tokenizer: Any, user_prompt: str) -> str:
    """Format as a chat message and return the full text."""
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_prompt},
    ]
    return tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True
    )


def extract_code_from_response(response: str) -> str:
    """Extract Python code from model response, stripping markdown if present."""
    text = response.strip()
    if "```python" in text:
        text = text.split("```python", 1)[1].split("```", 1)[0].strip()
    elif "```" in text:
        text = text.split("```", 1)[1].split("```", 1)[0].strip()
    return text


# ══════════════════════════════════════════════════════════════════════════════
# Reward Function
# ══════════════════════════════════════════════════════════════════════════════

def create_reward_function(env: WhipStudioEnv, verbose: bool = True):
    """
    Create a reward function compatible with TRL's GRPOTrainer.
    
    Includes reward shaping:
    - Bonus for valid Python syntax
    - Bonus for including required output markers
    - Environment reward from grader
    """

    def reward_fn(completions: list[list[dict]], **kwargs) -> list[float]:
        """Compute rewards for a batch of completions."""
        rewards = []
        task_ids = kwargs.get("task_id", ["task1"] * len(completions))

        for i, completion in enumerate(completions):
            task_id = task_ids[i] if i < len(task_ids) else "task1"

            try:
                # Extract assistant's response
                if isinstance(completion, list):
                    text = ""
                    for msg in completion:
                        if isinstance(msg, dict) and msg.get("role") == "assistant":
                            text = msg.get("content", "")
                            break
                    if not text and completion:
                        text = str(completion[-1].get("content", ""))
                elif isinstance(completion, str):
                    text = completion
                else:
                    text = str(completion)

                fixed_code = extract_code_from_response(text)
                
                # Reward shaping: syntax check
                syntax_bonus = 0.0
                try:
                    compile(fixed_code, "<string>", "exec")
                    syntax_bonus = 0.05
                except SyntaxError:
                    pass
                
                # Reward shaping: output markers present
                marker_bonus = 0.0
                if "LOSSES:" in fixed_code or "##METRICS" in fixed_code:
                    marker_bonus = 0.02

                if not fixed_code.strip():
                    rewards.append(0.0)
                    continue

                # Get environment reward
                env.reset(task_id)
                result = env.step(fixed_code, attempt=1)
                env_reward = float(result.get("reward", 0.0) or 0.0)
                
                # Total reward (capped at 1.0)
                total_reward = min(1.0, env_reward + syntax_bonus + marker_bonus)
                rewards.append(total_reward)

                if verbose:
                    print(f"  [reward] task={task_id} env={env_reward:.3f} syntax={syntax_bonus:.2f} total={total_reward:.3f}")

            except Exception as e:
                if verbose:
                    print(f"  [reward] ERROR task={task_id}: {e}")
                rewards.append(0.0)

        return rewards

    return reward_fn


# ══════════════════════════════════════════════════════════════════════════════
# Dataset Generation with Curriculum
# ══════════════════════════════════════════════════════════════════════════════

def generate_curriculum_dataset(
    env: WhipStudioEnv,
    tokenizer: Any,
    samples_per_task: int = 10,
    curriculum_stage: int = 0,  # 0 = all tasks, 1 = easier tasks weighted, etc.
) -> Dataset:
    """
    Generate a dataset with curriculum-based sampling.
    
    Args:
        env: WhipStudio environment client
        tokenizer: Model tokenizer
        samples_per_task: Base samples per task
        curriculum_stage: 0=uniform, higher=bias toward easier tasks
    """
    records = []
    
    # Compute task weights based on curriculum stage
    task_weights = {}
    for task_id, difficulty in TASK_DIFFICULTY.items():
        if curriculum_stage == 0:
            weight = 1.0
        else:
            # Higher curriculum_stage = more weight on easier tasks
            weight = max(0.2, 1.0 - (difficulty - 1) * 0.2 * curriculum_stage)
        task_weights[task_id] = weight
    
    # Normalize weights
    total_weight = sum(task_weights.values())
    task_weights = {k: v / total_weight for k, v in task_weights.items()}

    for task_id in ALL_TASKS:
        print(f"  Fetching observation for {task_id} (weight={task_weights[task_id]:.2f})...")
        obs = env.reset(task_id)
        user_prompt = build_user_prompt(
            task_description=obs.get("task_description", ""),
            buggy_code=obs.get("buggy_code", ""),
        )
        formatted = format_chat(tokenizer, user_prompt)
        
        # Number of samples proportional to weight
        n_samples = max(1, int(samples_per_task * task_weights[task_id] * len(ALL_TASKS)))
        
        for _ in range(n_samples):
            records.append({
                "prompt": formatted,
                "task_id": task_id,
            })

    random.shuffle(records)
    return Dataset.from_list(records)


# ══════════════════════════════════════════════════════════════════════════════
# Model Loading Utilities
# ══════════════════════════════════════════════════════════════════════════════

def load_model_and_tokenizer(
    model_name: str,
    use_4bit: bool = False,
    use_8bit: bool = False,
    gradient_checkpointing: bool = False,
):
    """Load model with optional quantization and gradient checkpointing."""
    
    print(f"Loading model: {model_name}")
    
    # Tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    
    # Quantization config
    quantization_config = None
    if use_4bit:
        if not PEFT_AVAILABLE:
            raise ImportError("4-bit quantization requires peft and bitsandbytes")
        quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch.bfloat16,
            bnb_4bit_use_double_quant=True,
        )
        print("  Using 4-bit quantization")
    elif use_8bit:
        quantization_config = BitsAndBytesConfig(load_in_8bit=True)
        print("  Using 8-bit quantization")
    
    # Model kwargs
    model_kwargs = {
        "trust_remote_code": True,
        "torch_dtype": torch.bfloat16 if not (use_4bit or use_8bit) else None,
        "device_map": "auto",
    }
    if quantization_config:
        model_kwargs["quantization_config"] = quantization_config
    
    # Load model
    model = AutoModelForCausalLM.from_pretrained(model_name, **model_kwargs)
    
    # Prepare for k-bit training if quantized
    if use_4bit or use_8bit:
        model = prepare_model_for_kbit_training(model)
    
    # Gradient checkpointing
    if gradient_checkpointing:
        model.gradient_checkpointing_enable()
        print("  Gradient checkpointing enabled")
    
    param_count = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(f"  Total params: {param_count / 1e6:.1f}M, Trainable: {trainable / 1e6:.1f}M")
    
    return model, tokenizer


def apply_lora(
    model,
    lora_r: int = 16,
    lora_alpha: int = 32,
    target_modules: Optional[list[str]] = None,
):
    """Apply LoRA adapters to the model."""
    if not PEFT_AVAILABLE:
        raise ImportError("LoRA requires peft: pip install peft")
    
    if target_modules is None:
        # Default targets for Qwen2 and similar architectures
        target_modules = [
            "q_proj", "k_proj", "v_proj", "o_proj",
            "gate_proj", "up_proj", "down_proj",
        ]
    
    lora_config = LoraConfig(
        r=lora_r,
        lora_alpha=lora_alpha,
        target_modules=target_modules,
        lora_dropout=0.05,
        bias="none",
        task_type="CAUSAL_LM",
    )
    
    model = get_peft_model(model, lora_config)
    
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(f"  LoRA applied: r={lora_r}, trainable params: {trainable / 1e6:.2f}M")
    
    return model, lora_config


# ══════════════════════════════════════════════════════════════════════════════
# Validation & Evaluation
# ══════════════════════════════════════════════════════════════════════════════

def evaluate_model(
    model,
    tokenizer,
    env: WhipStudioEnv,
    task_ids: list[str] = None,
    max_new_tokens: int = 2048,
) -> dict[str, float]:
    """Evaluate model on tasks and return scores."""
    if task_ids is None:
        task_ids = ALL_TASKS
    
    model.eval()
    scores = {}
    
    for task_id in task_ids:
        obs = env.reset(task_id)
        user_prompt = build_user_prompt(obs["task_description"], obs["buggy_code"])
        formatted = format_chat(tokenizer, user_prompt)
        
        inputs = tokenizer(formatted, return_tensors="pt", truncation=True, max_length=4096)
        inputs = {k: v.to(model.device) for k, v in inputs.items()}
        
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=max_new_tokens,
                temperature=0.2,
                top_p=0.95,
                do_sample=True,
                pad_token_id=tokenizer.pad_token_id,
            )
        
        generated = outputs[0][inputs["input_ids"].shape[1]:]
        response = tokenizer.decode(generated, skip_special_tokens=True)
        fixed_code = extract_code_from_response(response)
        
        env.reset(task_id)
        result = env.step(fixed_code, attempt=1)
        reward = float(result.get("reward", 0.0) or 0.0)
        scores[task_id] = reward
        print(f"    {task_id}: {reward:.4f}")
    
    return scores


# ══════════════════════════════════════════════════════════════════════════════
# Main Training Loop
# ══════════════════════════════════════════════════════════════════════════════

def main():
    parser = argparse.ArgumentParser(description="Improved GRPO training for WhipStudio")
    
    # Environment
    parser.add_argument("--env_url", type=str, required=True,
                        help="URL of the WhipStudio HF Space")
    
    # Model
    parser.add_argument("--model_name", type=str, default="Qwen/Qwen2.5-Coder-1.5B-Instruct",
                        help="Base model to fine-tune")
    parser.add_argument("--output_dir", type=str, default="./whipstudio-debugger",
                        help="Directory to save the trained model")
    
    # Quantization & Memory
    parser.add_argument("--use_4bit", action="store_true",
                        help="Use 4-bit quantization (requires bitsandbytes)")
    parser.add_argument("--use_8bit", action="store_true",
                        help="Use 8-bit quantization")
    parser.add_argument("--gradient_checkpointing", action="store_true",
                        help="Enable gradient checkpointing to save memory")
    
    # LoRA
    parser.add_argument("--use_lora", action="store_true",
                        help="Use LoRA for efficient fine-tuning")
    parser.add_argument("--lora_r", type=int, default=16,
                        help="LoRA rank")
    parser.add_argument("--lora_alpha", type=int, default=32,
                        help="LoRA alpha")
    
    # Training
    parser.add_argument("--num_iterations", type=int, default=50,
                        help="Number of training epochs")
    parser.add_argument("--group_size", type=int, default=4,
                        help="Number of completions per prompt for GRPO")
    parser.add_argument("--samples_per_task", type=int, default=10,
                        help="Base samples per task in dataset")
    parser.add_argument("--learning_rate", type=float, default=1e-5,
                        help="Learning rate")
    parser.add_argument("--max_new_tokens", type=int, default=2048,
                        help="Max tokens to generate per completion")
    parser.add_argument("--beta", type=float, default=0.1,
                        help="KL penalty coefficient")
    
    # Curriculum
    parser.add_argument("--curriculum_stages", type=int, default=3,
                        help="Number of curriculum stages (0 = no curriculum)")
    
    # Logging
    parser.add_argument("--use_wandb", action="store_true",
                        help="Log to Weights & Biases")
    parser.add_argument("--wandb_project", type=str, default="whipstudio",
                        help="W&B project name")
    
    # Early stopping
    parser.add_argument("--patience", type=int, default=10,
                        help="Early stopping patience (epochs without improvement)")
    parser.add_argument("--eval_every", type=int, default=5,
                        help="Evaluate every N epochs")
    
    # Hub
    parser.add_argument("--push_to_hub", action="store_true",
                        help="Push trained model to HuggingFace Hub")
    parser.add_argument("--hub_model_id", type=str, default=None,
                        help="Model ID on HF Hub")
    
    args = parser.parse_args()

    # ── Verify environment ──
    print(f"\n{'=' * 60}")
    print("WhipStudio Improved GRPO Training")
    print(f"{'=' * 60}")
    print(f"Environment: {args.env_url}")
    
    env = WhipStudioEnv(args.env_url)
    if not env.health_check():
        raise ConnectionError(f"Cannot reach WhipStudio at {args.env_url}")
    print("Environment is reachable βœ“")

    # ── Initialize wandb ──
    if args.use_wandb:
        if not WANDB_AVAILABLE:
            print("Warning: wandb not installed, skipping logging")
            args.use_wandb = False
        else:
            wandb.init(
                project=args.wandb_project,
                config=vars(args),
                name=f"grpo-{args.model_name.split('/')[-1]}",
            )

    # ── Load model ──
    model, tokenizer = load_model_and_tokenizer(
        args.model_name,
        use_4bit=args.use_4bit,
        use_8bit=args.use_8bit,
        gradient_checkpointing=args.gradient_checkpointing,
    )
    
    # ── Apply LoRA ──
    peft_config = None
    if args.use_lora:
        model, peft_config = apply_lora(
            model,
            lora_r=args.lora_r,
            lora_alpha=args.lora_alpha,
        )

    # ── Create output directory ──
    output_path = Path(args.output_dir)
    output_path.mkdir(parents=True, exist_ok=True)
    
    # ── Training with curriculum ──
    best_avg_score = 0.0
    epochs_without_improvement = 0
    
    n_stages = max(1, args.curriculum_stages)
    epochs_per_stage = args.num_iterations // n_stages
    
    for stage in range(n_stages):
        print(f"\n{'=' * 60}")
        print(f"Curriculum Stage {stage + 1}/{n_stages}")
        print(f"{'=' * 60}")
        
        # Generate dataset for this curriculum stage
        dataset = generate_curriculum_dataset(
            env, tokenizer,
            samples_per_task=args.samples_per_task,
            curriculum_stage=stage,
        )
        print(f"Dataset: {len(dataset)} samples")
        
        # Create reward function
        reward_fn = create_reward_function(env, verbose=True)
        
        # Configure GRPO
        grpo_config = GRPOConfig(
            output_dir=str(output_path / f"stage_{stage}"),
            num_train_epochs=epochs_per_stage,
            per_device_train_batch_size=1,
            gradient_accumulation_steps=4,
            learning_rate=args.learning_rate,
            lr_scheduler_type="cosine",
            warmup_ratio=0.1,
            max_completion_length=args.max_new_tokens,
            num_generations=args.group_size,
            logging_steps=1,
            save_steps=epochs_per_stage,
            save_total_limit=2,
            bf16=True,
            report_to="wandb" if args.use_wandb else "none",
            beta=args.beta,
            remove_unused_columns=False,
        )
        
        # Initialize trainer
        trainer = GRPOTrainer(
            model=model,
            args=grpo_config,
            train_dataset=dataset,
            processing_class=tokenizer,
            reward_funcs=reward_fn,
            peft_config=peft_config if stage == 0 else None,  # Only apply peft on first stage
        )
        
        # Train
        print(f"\nTraining stage {stage + 1}...")
        train_result = trainer.train()
        print(f"  Stage {stage + 1} complete: {train_result.global_step} steps")
        
        # Evaluate
        print("\nEvaluating...")
        scores = evaluate_model(model, tokenizer, env)
        avg_score = sum(scores.values()) / len(scores)
        print(f"  Average score: {avg_score:.4f}")
        
        if args.use_wandb:
            wandb.log({
                "stage": stage + 1,
                "avg_score": avg_score,
                **{f"score/{k}": v for k, v in scores.items()},
            })
        
        # Track best model
        if avg_score > best_avg_score:
            best_avg_score = avg_score
            epochs_without_improvement = 0
            # Save best model
            best_path = output_path / "best"
            trainer.save_model(str(best_path))
            tokenizer.save_pretrained(str(best_path))
            print(f"  New best model saved (score={avg_score:.4f})")
        else:
            epochs_without_improvement += epochs_per_stage
        
        # Early stopping
        if epochs_without_improvement >= args.patience:
            print(f"\nEarly stopping: no improvement for {args.patience} epochs")
            break

    # ── Final save ──
    final_path = output_path / "final"
    trainer.save_model(str(final_path))
    tokenizer.save_pretrained(str(final_path))
    print(f"\nFinal model saved to {final_path}")

    # ── Push to hub ──
    if args.push_to_hub and args.hub_model_id:
        print(f"Pushing to Hub as {args.hub_model_id}...")
        trainer.push_to_hub(args.hub_model_id)
        tokenizer.push_to_hub(args.hub_model_id)
        print("Pushed to Hub βœ“")

    # ── Final evaluation ──
    print(f"\n{'=' * 60}")
    print("Final Evaluation on All Tasks")
    print(f"{'=' * 60}")
    final_scores = evaluate_model(model, tokenizer, env)
    final_avg = sum(final_scores.values()) / len(final_scores)
    print(f"\nFinal average score: {final_avg:.4f}")
    print(f"Best average score during training: {best_avg_score:.4f}")

    if args.use_wandb:
        wandb.log({"final_avg_score": final_avg})
        wandb.finish()

    # ── Save training summary ──
    summary = {
        "model_name": args.model_name,
        "final_avg_score": final_avg,
        "best_avg_score": best_avg_score,
        "final_scores": final_scores,
        "curriculum_stages": n_stages,
        "use_lora": args.use_lora,
        "use_4bit": args.use_4bit,
    }
    with open(output_path / "training_summary.json", "w") as f:
        json.dump(summary, f, indent=2)

    print("\nTraining complete! βœ“")


if __name__ == "__main__":
    main()