File size: 22,587 Bytes
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0e028b
51bb0d4
 
 
 
 
2b9c8cb
51bb0d4
 
 
 
 
 
 
 
 
 
 
2b9c8cb
51bb0d4
 
 
 
 
 
2b9c8cb
51bb0d4
 
 
a24d3c8
51bb0d4
a8a8219
51bb0d4
 
 
 
 
 
 
2b9c8cb
51bb0d4
 
 
 
8ca88e8
 
91ac1b6
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
2b9c8cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca88e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51bb0d4
 
 
 
 
8ca88e8
 
 
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca88e8
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
8ca88e8
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca88e8
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca88e8
51bb0d4
8ca88e8
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91ac1b6
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91ac1b6
 
 
51bb0d4
 
 
 
8ca88e8
51bb0d4
 
 
 
 
 
 
8ca88e8
51bb0d4
 
 
 
8ca88e8
 
51bb0d4
 
 
91ac1b6
 
51bb0d4
 
4714235
51bb0d4
 
a0e028b
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d3c8
51bb0d4
91ac1b6
51bb0d4
 
 
 
 
 
 
 
 
a24d3c8
 
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
2b9c8cb
51bb0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d3c8
 
 
51bb0d4
a24d3c8
51bb0d4
 
 
 
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
"""
train.py β€” GRPO Training Script for Planetary Rover Navigation
================================================================

Uses Unsloth's FastLanguageModel + TRL's GRPOTrainer to fine-tune
meta-llama/Llama-3.2-1B-Instruct for autonomous rover navigation.

Hardware target : NVIDIA RTX 3050 β€” strict 6 GB VRAM limit
Quantisation    : 4-bit NF4 via Unsloth
LoRA            : rank 16, attention + MLP projections
GRPO group size : 4 generations per prompt (prevents OOM)

Reward functions
----------------
  1. Format Gatekeeper β€” validates <action>JSON</action> structure
  2. Environment Reward β€” POSTs parsed action to local physics server

Prerequisites
-------------
  1. Local server running:
       uvicorn main:app --host 0.0.0.0 --port 7860
  2. Python packages:
       pip install unsloth trl datasets peft accelerate
"""

from __future__ import annotations

import json
import math
import os
import wandb
import re
import sys
import time
import random
import logging
from numbers import Real
from typing import Any

import requests
import torch
from datasets import Dataset

# ---------------------------------------------------------------------------
# Unsloth + TRL imports (deferred to allow --help without GPU)
# ---------------------------------------------------------------------------
from unsloth import FastLanguageModel
from trl import GRPOConfig, GRPOTrainer
from transformers import TrainerCallback

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

MODEL_NAME      = "meta-llama/Llama-3.2-1B-Instruct"
SERVER_URL      = os.getenv("ROVER_SERVER_URL", "http://127.0.0.1:7860")
OUTPUT_DIR      = "./grpo_rover_checkpoints"
SEED            = 42

# Cloud GPU parameters (HF Spaces β€” migrated from local 6 GB)
MAX_SEQ_LENGTH  = 512          # prompt + completion combined
LORA_RANK       = 16
LORA_ALPHA      = 32
LORA_DROPOUT    = 0.0

# Training hyperparameters
NUM_TRAIN_EPISODES   = 150     # prompts per task Γ— 3 tasks = total dataset
MAX_PROMPT_LENGTH    = 256
MAX_COMPLETION_LENGTH = 256
NUM_GENERATIONS      = int(os.getenv("ROVER_NUM_GENERATIONS", "8"))
LEARNING_RATE        = 1e-6
KL_COEF              = 0.04    # Ξ² for KL penalty
NUM_TRAIN_EPOCHS     = 2
PER_DEVICE_BATCH     = 1       # keep at 1 for 6 GB
GRAD_ACCUM_STEPS     = int(os.getenv("ROVER_GRAD_ACCUM_STEPS", "8"))
WARMUP_STEPS         = int(os.getenv("ROVER_WARMUP_STEPS", "10"))
USE_BF16             = os.getenv("ROVER_USE_BF16", "0") == "1"

# Reward tuning
FORMAT_REWARD_GOOD   = 1.0
FORMAT_REWARD_BAD    = 0.0
VERBOSITY_THRESHOLD  = 80      # tokens β€” a valid <action>{…}</action> is ~30-40
VERBOSITY_PENALTY_K  = 200     # excess tokens before reward β†’ 0

# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
log = logging.getLogger("train")


def _extract_scalar_reward(logs: dict[str, Any]) -> float | None:
    """Return one scalar reward value from a TRL/Trainer log payload."""
    raw_reward = logs.get("reward")
    if isinstance(raw_reward, Real):
        return float(raw_reward)

    reward_terms: list[float] = []
    for key, value in logs.items():
        key_lower = key.lower()
        if "reward" not in key_lower:
            continue
        if any(skip in key_lower for skip in ("std", "min", "max", "var")):
            continue
        if isinstance(value, Real):
            reward_terms.append(float(value))

    if not reward_terms:
        return None

    return sum(reward_terms) / len(reward_terms)


class CompactMetricsCallback(TrainerCallback):
    """Emit a concise log line to simplify screenshot capture in Spaces logs."""

    def on_log(self, args, state, control, logs=None, **kwargs):
        if not logs:
            return control

        loss = logs.get("loss")
        if not isinstance(loss, Real):
            return control

        metrics: dict[str, float] = {"loss": float(loss)}

        reward = _extract_scalar_reward(logs)
        if reward is not None:
            metrics["reward"] = reward

        learning_rate = logs.get("learning_rate")
        if isinstance(learning_rate, Real):
            metrics["lr"] = float(learning_rate)

        compact = {key: round(value, 6) for key, value in metrics.items()}
        log.info("METRICS %s", compact)
        return control


# =============================================================================
# System prompt (compact β€” must fit within ~90 tokens so user prompt has room)
# =============================================================================

SYSTEM_PROMPT = """\
You are a planetary rover navigation controller.
Respond ONLY with your action inside <action></action> tags as valid JSON.

Action schema:
{"thrust": float[0,1], "steering": float[-1,1], "brake": 0|1, "vertical_thruster": float[-0.2,0.2]}

Key physics:
- heading_error = atan2(target_dy, target_dx) - rover_heading
- steering β‰ˆ clamp(heading_error * 2.5, -1, 1)
- thrust=1.0 for progress; brake=0 unless overshooting
- If nearest_obstacle < 10m, steer perpendicular to dodge\
"""


# =============================================================================
# Compact observation prompt builder
# =============================================================================

def build_compact_prompt(
    task_id:   str,
    obs:       dict[str, Any],
    step_num:  int,
    max_steps: int,
) -> str:
    """
    Build a token-efficient user prompt from an observation dict.
    Designed to fit in ~100–120 tokens so system + user ≀ 256.
    """
    dx = obs["target_relative"]["x"]
    dy = obs["target_relative"]["y"]

    # Pre-compute heading error so the model doesn't need trig
    target_heading = math.atan2(dy, dx)
    raw_error = target_heading - obs["rover_heading"]
    while raw_error >  math.pi: raw_error -= 2 * math.pi
    while raw_error <= -math.pi: raw_error += 2 * math.pi

    suggested_steering = max(-1.0, min(1.0, raw_error * 2.5))

    return (
        f"TASK: {task_id}  STEP: {step_num}/{max_steps}\n"
        f"target_distance={obs['target_distance']:.1f}m "
        f"heading_error={raw_error:.4f}rad\n"
        f"battery={obs['battery_level']:.3f} "
        f"nearest_obstacle={obs['nearest_obstacle_distance']:.1f}m "
        f"terrain={obs['terrain_type']}\n"
        f"suggested_steering={suggested_steering:.4f}\n"
        f"Output your <action> JSON now."
    )


# =============================================================================
# Dataset generation β€” resets episodes and collects initial observations
# =============================================================================

TASK_MAX_STEPS = {"easy": 200, "medium": 300, "hard": 100}


def _check_server() -> None:
    """Fail fast if the environment server is unreachable."""
    try:
        r = requests.get(f"{SERVER_URL}/tasks", timeout=5)
        r.raise_for_status()
        log.info("Environment server is live at %s", SERVER_URL)
    except Exception as e:
        log.error(
            "Cannot reach environment server at %s β€” "
            "start it with: uvicorn main:app --host 0.0.0.0 --port 7860",
            SERVER_URL,
        )
        sys.exit(1)


def generate_training_dataset(n_per_task: int = NUM_TRAIN_EPISODES) -> Dataset:
    """
    Generate a training dataset by resetting episodes across all tasks.

    Each row contains:
      prompt   β€” chat-formatted messages (system + user)
      task_id  β€” for environment reward replay
      seed     β€” for environment reward replay
    """
    rows: list[dict[str, Any]] = []

    for task_id in ["easy", "medium", "hard"]:
        max_steps = TASK_MAX_STEPS[task_id]
        for seed in range(n_per_task):
            try:
                resp = requests.post(
                    f"{SERVER_URL}/reset",
                    json={"task_id": task_id, "seed": seed},
                    timeout=10,
                )
                resp.raise_for_status()
                data = resp.json()
            except Exception as e:
                log.warning("Reset failed (task=%s seed=%d): %s", task_id, seed, e)
                continue

            obs = data["obs"]
            user_msg = build_compact_prompt(task_id, obs, step_num=1, max_steps=max_steps)

            rows.append({
                "prompt": [
                    {"role": "system", "content": SYSTEM_PROMPT},
                    {"role": "user",   "content": user_msg},
                ],
                "task_id": task_id,
                "seed":    seed,
            })

    random.shuffle(rows)
    log.info("Generated %d training prompts (%d per task Γ— 3 tasks)", len(rows), n_per_task)
    return Dataset.from_list(rows)


# =============================================================================
# Reward Function 1 β€” Format Gatekeeper
# =============================================================================

# Regex to extract content between <action> and </action> tags
_ACTION_RE = re.compile(r"<action>\s*(.*?)\s*</action>", re.DOTALL)

# Required fields and their (min, max) bounds
_ACTION_FIELDS = {
    "thrust":            (0.0,  1.0),
    "steering":          (-1.0, 1.0),
    "brake":             (0,    1),
    "vertical_thruster": (-0.2, 0.2),
}


def _completion_to_text(completion: Any) -> str:
    """Convert TRL completion payloads (str/list/dict) into plain text."""
    if completion is None:
        return ""

    if isinstance(completion, str):
        return completion

    if isinstance(completion, bytes):
        return completion.decode("utf-8", errors="ignore")

    if isinstance(completion, dict):
        for key in ("content", "text", "completion", "generated_text"):
            if key in completion:
                return _completion_to_text(completion[key])
        return str(completion)

    if isinstance(completion, list):
        parts = [_completion_to_text(item) for item in completion]
        return "\n".join(part for part in parts if part)

    return str(completion)


def parse_action_from_completion(completion: Any) -> dict[str, Any] | None:
    """
    Extract and validate an action JSON from <action>…</action> tags.

    Returns the parsed action dict if valid, None otherwise.
    """
    text = _completion_to_text(completion)
    if not text:
        return None

    match = _ACTION_RE.search(text)
    if not match:
        return None

    try:
        parsed = json.loads(match.group(1))
    except json.JSONDecodeError:
        return None

    if not isinstance(parsed, dict):
        return None

    # Validate required fields exist and are numeric
    action: dict[str, Any] = {}
    for field, (lo, hi) in _ACTION_FIELDS.items():
        if field not in parsed:
            return None
        val = parsed[field]
        try:
            if field == "brake":
                val = int(round(float(val)))
            else:
                val = float(val)
        except (TypeError, ValueError):
            return None
        # Reject wildly out-of-range (mild overshoot is clamped, not rejected)
        if val < lo - 0.5 or val > hi + 0.5:
            return None
        # Clamp to valid bounds
        if field == "brake":
            val = max(0, min(1, val))
        else:
            val = max(lo, min(hi, val))
        action[field] = val

    return action


def format_reward_fn(completions: list[Any], **kwargs) -> list[float]:
    """
    Reward Function 1 β€” The Format Gatekeeper.

    Returns 1.0 if the completion contains valid <action>JSON</action>
    matching the rover action schema.  Returns 0.0 on failure.

    Applies a soft verbosity penalty: completions exceeding
    VERBOSITY_THRESHOLD tokens are penalised linearly, reaching 0
    at VERBOSITY_THRESHOLD + VERBOSITY_PENALTY_K tokens.
    """
    rewards: list[float] = []

    for completion in completions:
        text = _completion_to_text(completion)
        action = parse_action_from_completion(text)
        if action is None:
            rewards.append(FORMAT_REWARD_BAD)
            continue

        # Base reward for valid format
        base = FORMAT_REWARD_GOOD

        # Soft verbosity penalty β€” count whitespace-split "tokens" as proxy
        # (actual BPE count varies, but this is a stable heuristic)
        token_estimate = len(text.split())
        if token_estimate > VERBOSITY_THRESHOLD:
            excess = token_estimate - VERBOSITY_THRESHOLD
            penalty = max(0.0, 1.0 - excess / VERBOSITY_PENALTY_K)
            base *= penalty

        rewards.append(base)

    return rewards


# =============================================================================
# Reward Function 2 β€” Environment Reward
# =============================================================================

def environment_reward_fn(completions: list[Any], **kwargs) -> list[float]:
    """
    Reward Function 2 β€” The Environment.

    For each completion:
      1. Parse the action from <action> tags.
      2. Reset a fresh episode with the same (task_id, seed) as the prompt.
      3. POST the action to /step.
      4. Return the scalar step reward from the physics engine.

    If parsing or HTTP fails, returns 0.0 (neutral β€” no signal).
    """
    task_ids: list[str] = kwargs.get("task_id", [])
    seeds:    list[int] = kwargs.get("seed", [])

    rewards: list[float] = []

    for i, completion in enumerate(completions):
        # -- Parse action --------------------------------------------------
        action = parse_action_from_completion(completion)
        if action is None:
            rewards.append(0.0)
            continue

        # -- Determine episode parameters ----------------------------------
        # kwargs columns are lists aligned with completions.
        # With num_generations=4, each prompt's metadata is repeated 4 times.
        task_id = task_ids[i] if i < len(task_ids) else "easy"
        seed    = seeds[i]    if i < len(seeds)    else 0

        try:
            # Reset a fresh episode with the same seed β†’ identical starting state
            reset_resp = requests.post(
                f"{SERVER_URL}/reset",
                json={"task_id": task_id, "seed": seed},
                timeout=10,
            )
            reset_resp.raise_for_status()
            episode_id = reset_resp.json()["episode_id"]

            # Step with the generated action
            step_resp = requests.post(
                f"{SERVER_URL}/step",
                json=action,
                params={"episode_id": episode_id},
                timeout=10,
            )
            step_resp.raise_for_status()
            step_data = step_resp.json()

            # Return the scalar reward from the physics engine
            reward = float(step_data.get("reward", 0.0))
            rewards.append(reward)

        except Exception as e:
            log.warning("Environment reward failed (task=%s seed=%d): %s", task_id, seed, e)
            rewards.append(0.0)

    return rewards


# =============================================================================
# Model loading
# =============================================================================

def load_model():
    """
    Load Llama-3.2-1B-Instruct with Unsloth's 4-bit NF4 quantisation
    and attach LoRA adapters to attention + MLP projections.
    """
    log.info("Loading %s with 4-bit NF4 quantisation via Unsloth…", MODEL_NAME)

    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name     = MODEL_NAME,
        max_seq_length = MAX_SEQ_LENGTH,
        # Use a fixed dtype to avoid mixed precision mismatches in LoRA kernels.
        dtype          = torch.bfloat16 if USE_BF16 else torch.float16,
        load_in_4bit   = True,          # NF4 quantisation for 6 GB VRAM
    )

    log.info("Attaching LoRA (rank=%d, alpha=%d) to attention + MLP…", LORA_RANK, LORA_ALPHA)

    model = FastLanguageModel.get_peft_model(
        model,
        r              = LORA_RANK,
        target_modules = [
            # Attention projections
            "q_proj", "k_proj", "v_proj", "o_proj",
            # MLP projections (SwiGLU in Llama)
            "gate_proj", "up_proj", "down_proj",
        ],
        lora_alpha     = LORA_ALPHA,
        lora_dropout   = LORA_DROPOUT,
        bias           = "none",
        use_gradient_checkpointing = "unsloth",   # 60% less VRAM
        random_state   = SEED,
    )

    # Ensure pad token is set (required for batched generation)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    tokenizer.padding_side = "left"    # decoder-only: pad on the left

    vram_gb = torch.cuda.memory_allocated() / 1e9
    log.info("Model loaded. Current VRAM: %.2f GB", vram_gb)

    return model, tokenizer


# =============================================================================
# Training configuration
# =============================================================================

def build_training_config() -> GRPOConfig:
    """Build the GRPOConfig with parameters safe for 6 GB VRAM."""
    # Keep trainer precision aligned with model dtype.
    use_bf16 = USE_BF16

    return GRPOConfig(
        output_dir             = OUTPUT_DIR,

        # ── GRPO-specific ─────────────────────────────────────────────
        num_generations        = NUM_GENERATIONS,     # group size per prompt
        max_prompt_length      = MAX_PROMPT_LENGTH,   # 256 tokens
        max_completion_length  = MAX_COMPLETION_LENGTH,# 256 tokens
        beta                   = KL_COEF,             # KL penalty coeff

        # ── Optimiser ─────────────────────────────────────────────────
        learning_rate          = LEARNING_RATE,        # 1e-6
        lr_scheduler_type      = "cosine",
        warmup_steps           = WARMUP_STEPS,
        max_grad_norm          = 1.0,

        # ── Batch / accumulation ──────────────────────────────────────
        per_device_train_batch_size = PER_DEVICE_BATCH,   # 1 for 6 GB
        # Keep (per_device * grad_accum * world_size) divisible by num_generations.
        gradient_accumulation_steps = GRAD_ACCUM_STEPS,
        num_train_epochs            = NUM_TRAIN_EPOCHS,

        # ── Precision / memory ────────────────────────────────────────
        bf16                   = use_bf16,
        fp16                   = not use_bf16,

        # ── Logging / saving ──────────────────────────────────────────
        logging_steps          = 1,
        save_steps             = 50,
        save_total_limit       = 3,
        report_to              = "wandb",
        run_name               = "openenv-rover-run",
        seed                   = SEED,

        # ── Misc ──────────────────────────────────────────────────────
        remove_unused_columns  = False,                # keep task_id/seed cols
    )


# =============================================================================
# Main entry point
# =============================================================================

def main() -> None:
    log.info("=" * 60)
    log.info("GRPO Training β€” Planetary Rover Navigation")
    log.info("Model : %s", MODEL_NAME)
    log.info("VRAM  : 24 GB+ cloud GPU (4-bit NF4, LoRA r=%d, group=%d)",
             LORA_RANK, NUM_GENERATIONS)
    log.info("Precision: %s", "bf16" if USE_BF16 else "fp16")
    log.info("=" * 60)

    # ── 0. Check server ───────────────────────────────────────────────
    _check_server()

    # ── 1. Load model + tokenizer ─────────────────────────────────────
    model, tokenizer = load_model()

    # ── 2. Generate training dataset ──────────────────────────────────
    log.info("Generating full training dataset from physics engine...")
    train_dataset = generate_training_dataset()

    # ── 3. Build GRPO config ──────────────────────────────────────────
    config = build_training_config()

    # ── 4. Initialise trainer ─────────────────────────────────────────
    log.info("Initialising GRPOTrainer with 2 reward functions…")
    trainer = GRPOTrainer(
        model        = model,
        tokenizer    = tokenizer,
        reward_funcs = [format_reward_fn, environment_reward_fn],
        args         = config,
        train_dataset = train_dataset,
    )
    trainer.add_callback(CompactMetricsCallback())

    # ── 5. Train ──────────────────────────────────────────────────────
    log.info("Starting GRPO training…")
    start = time.time()
    trainer.train()
    elapsed = time.time() - start
    log.info("Training complete in %.1f minutes.", elapsed / 60)

    # ── 6. Save final adapter ─────────────────────────────────────────
    final_path = os.path.join(OUTPUT_DIR, "final_adapter")
    model.save_pretrained(final_path)
    tokenizer.save_pretrained(final_path)
    log.info("Final LoRA adapter saved to %s", final_path)

    # ── 7. VRAM summary ──────────────────────────────────────────────
    peak_vram = torch.cuda.max_memory_allocated() / 1e9
    log.info("Peak VRAM usage: %.2f GB", peak_vram)
    if peak_vram > 24.0:
        log.warning("⚠ Peak VRAM exceeded 24 GB! Reduce NUM_GENERATIONS or LORA_RANK.")
    else:
        log.info("βœ… VRAM within cloud GPU budget.")


if __name__ == "__main__":
    main()