| """Step 10: Run DAPO for the shared Capability LoRA. |
| |
| Single GPU: |
| python scripts/train/10_run_dapo.py |
| python scripts/train/10_run_dapo.py --config configs/train/dapo.yaml |
| |
| 8-GPU data parallel (TensorBoard auto-detected via $TENSORBOARD_LOG_PATH): |
| torchrun --standalone --nproc_per_node=8 scripts/train/10_run_dapo.py |
| # or use the launcher: bash scripts/train/run_dapo_8gpu.sh |
| """ |
|
|
| import argparse |
| import os |
| import sys |
|
|
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
| PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| os.environ.setdefault("CARTRIDGES_DIR", os.path.join(PROJECT_ROOT, "cartridges-lib")) |
| os.environ.setdefault("CARTRIDGES_OUTPUT_DIR", os.path.join(PROJECT_ROOT, "checkpoints/cartridge")) |
|
|
| from src.train.rl.dapo_trainer import DAPOTrainer |
| from src.utils import cleanup_distributed, load_yaml, set_seed, setup_distributed, setup_logger |
|
|
| logger = setup_logger(__name__) |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--config", default="configs/train/dapo.yaml") |
| ap.add_argument("--epochs", type=int, default=None) |
| ap.add_argument("--batch-anchors", type=int, default=None) |
| ap.add_argument("--output-dir", default=None) |
| ap.add_argument("--resume", action="store_true", |
| help="continue from output_dir/latest.txt (policy LoRA + optimizer + step)") |
| args = ap.parse_args() |
|
|
| cfg_path = args.config if os.path.isabs(args.config) else os.path.join(PROJECT_ROOT, args.config) |
| cfg = load_yaml(cfg_path) |
| if args.epochs is not None: |
| cfg["epochs"] = args.epochs |
| if args.batch_anchors is not None: |
| cfg["batch_anchors"] = args.batch_anchors |
| if args.output_dir is not None: |
| cfg["output_dir"] = args.output_dir |
| if args.resume: |
| cfg["resume"] = True |
|
|
| dist_ctx = setup_distributed() |
| set_seed(cfg.get("seed", 42)) |
| if dist_ctx["is_distributed"]: |
| logger.info(f"Distributed: rank {dist_ctx['rank']}/{dist_ctx['world_size']} " |
| f"local_rank={dist_ctx['local_rank']}") |
|
|
| try: |
| trainer = DAPOTrainer(cfg, PROJECT_ROOT) |
| logger.info(f"Loaded {len(trainer.anchors)} anchors; K={trainer.K}, batch={trainer.batch_anchors}") |
| trainer.run() |
| finally: |
| cleanup_distributed() |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|