# Training RGB PPO in sim — recipe + dynamics

Script: `lerobot_sim2real/scripts/train_ppo_rgb.py` (CleanRL-style visual PPO,
`lerobot_sim2real/rl/ppo_rgb.py`). Launch + monitor it yourself; don't hand back
commands.

## Command (8GB GPU)
```bash
uv --project . run python -u lerobot_sim2real/scripts/train_ppo_rgb.py \
  --env-id SO100GraspCube-v1 --env-kwargs-json-path env_config.json \
  --ppo.seed 3 --ppo.num-envs 256 --ppo.num-steps 16 --ppo.update-epochs 8 \
  --ppo.num-minibatches 32 --ppo.total-timesteps 16000000 --ppo.gamma 0.9 \
  --ppo.num-eval-envs 4 --ppo.num-eval-steps 64 --ppo.no-partial-reset \
  --ppo.exp-name ppo-my-run
```
Checkpoints `runs/<run>/ckpt_N.pt`; eval videos `runs/<run>/videos/`; TB
`runs/<run>/events.*` (`tensorboard --logdir runs/`).

## GPU constraints (RTX 3070 Laptop, 8GB)
- 512-env PPO ~7.2GB; adding render eval envs OOMs PhysX
  (`compressContactStage fail to launch kernel`). Use 256 envs + 4 eval envs.
- CPU render SEGFAULTS — keep render on GPU.
- `num_eval_steps` must be ≥ episode horizon (64); 50 < 64 = blind eval.

## Training dynamics (learned the hard way)
- **Phase transition:** `success_once` ≈ 0 until ~9–10M env-steps, then jumps to
  ~0.8. An 8M run that looks dead is just early. Budget ≥16M; tutorial says
  25–40M is the safe range. Over-training can worsen the policy.
- **Reward decode (read TB without video):** return ≈16–22 = reach/hover only;
  `is_grasped` adds ~+0.33/step → return 30–45+ = grasping. If return climbs but
  stays <25, the policy is reaching, not grasping.
- **Exploration bottleneck from spawn shift:** moving the spawn box
  (`[0.3,0.05]±0.1` → `[0.25,-0.05]±0.12`) made from-scratch never discover grasp
  (0 success on 42 evals @13.8M). From-scratch is wrong when you shift the task
  distribution.
- **Fine-tune vs scratch:** warm-start fine-tune (LR ~1e-4) transfers the grasp
  and bypasses the exploration bottleneck. An earlier "fine-tune degrades
  (unlearns grasp)" result was CONFOUNDED by a misaligned bg — with a consistent
  bg, prefer fine-tune when adapting camera/spawn. From-scratch only for a genuinely
  new task.
- **Camera-DR cannot fix a bad bg:** the bg is a fixed 2D image; if its viewpoint
  is wrong, no amount of camera randomization makes training learnable.

## Config consistency (critical)
Train and eval with the SAME env config: camera (incl. roll), `greenscreen_overlay_path`
(bg_real.png), spawn box. Make a DR-off eval variant (`domain_randomization: false`)
for gating. A policy trained on config A evaluated on config B silently fails.

## Deploy gate
Sim `success_once` ≥ 0.6 on a DR-off eval (≥16 eps) with the training config
before real deployment. Then `eval_real_probe.py` (see SKILL.md Stage 4) and
diagnose localization vs actuation (Stage 5).
