Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82652c7393 |
@@ -75,3 +75,6 @@ Documenting changes which affect configuration usage patterns (added/moved/remov
|
||||
- **`hf_hub_offline` (RL/SFT SLURM)**: Removed. `HF_HUB_OFFLINE=1` is now hardcoded in the multi-node SLURM templates (2026-02-23)
|
||||
- **SLURM templates**: Moved from `src/prime_rl/slurm/` to `src/prime_rl/templates/` and renamed to `single_node_rl.sbatch.j2`, `multi_node_rl.sbatch.j2`, `single_node_sft.sbatch.j2`, `multi_node_sft.sbatch.j2` (2026-02-23)
|
||||
- **Entrypoints**: Moved `rl` and `sft` entrypoints from `prime_rl.rl` / `prime_rl.sft` to `prime_rl.entrypoints.rl` / `prime_rl.entrypoints.sft`. No change to CLI usage (`uv run rl`, `uv run sft`) (2026-02-24)
|
||||
- **`output_dir` (RL)**: Changed from `Path | None` (default `None`) to `Path` (default `Path("outputs")`). The SLURM-specific validation that rejected the default has been removed — `output_dir` now works the same for local and SLURM runs (2026-02-24)
|
||||
- **`clean_output_dir`**: Added to `RLConfig` and `SFTTrainerConfig` (default: `False`). Training now raises `FileExistsError` when `output_dir` contains checkpoints from a previous run and not resuming. Set `clean_output_dir=true` to delete and start fresh, or set `ckpt.resume_step` to resume (2026-02-24)
|
||||
- **`clean`**: Removed from `RLConfig`. The old `clean` flag (default: `True`) silently deleted logs, rollouts, and broadcasts on every local RL run. Superseded by the explicit `clean_output_dir` flag (2026-02-24)
|
||||
|
||||
+1
-1
@@ -75,7 +75,7 @@ The SLURM template is auto-selected based on `deployment.type`. You can override
|
||||
|
||||
### Constraints
|
||||
|
||||
- `output_dir` must be explicitly set when using SLURM (the default `"outputs"` is rejected)
|
||||
- `output_dir` should be explicitly set when using SLURM (defaults to `"outputs"`)
|
||||
- Multi-node deployment requires `[slurm]` to be set
|
||||
|
||||
---
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
@@ -13,14 +12,13 @@ import pynvml
|
||||
import tomli_w
|
||||
|
||||
from prime_rl.rl_config import RLConfig
|
||||
from prime_rl.utils.logger import get_logger, setup_logger
|
||||
from prime_rl.utils.logger import setup_logger
|
||||
from prime_rl.utils.pathing import validate_output_dir
|
||||
from prime_rl.utils.process import cleanup_processes, cleanup_threads, monitor_process
|
||||
from prime_rl.utils.pydantic_config import parse_argv
|
||||
from prime_rl.utils.utils import (
|
||||
get_broadcast_dir,
|
||||
get_free_port,
|
||||
get_log_dir,
|
||||
get_rollout_dir,
|
||||
)
|
||||
|
||||
|
||||
@@ -64,37 +62,7 @@ def check_gpus_available(gpu_ids: list[int]) -> None:
|
||||
raise RuntimeError(msg)
|
||||
|
||||
|
||||
def maybe_clean(config: RLConfig, log_dir: Path, orch_log_dir: Path, broadcast_dir: Path, rollout_dir: Path):
|
||||
logger = get_logger()
|
||||
logger.info("Cleaning checkpoint, logs, weights, broadcast and rollout directories")
|
||||
|
||||
# Cleaning logs (so that streaming logs to terminal works)
|
||||
logger.info(f"Cleaning log dir ({log_dir})")
|
||||
shutil.rmtree(log_dir, ignore_errors=True)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
logger.info(f"Cleaning orchestrator log dir ({orch_log_dir})")
|
||||
shutil.rmtree(orch_log_dir, ignore_errors=True)
|
||||
orch_log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Cleaning broadcast dir (so that orchestrator does not pre-maturely update weights)
|
||||
if not (
|
||||
config.ckpt
|
||||
and config.ckpt.resume_step
|
||||
and config.trainer.weight_broadcast
|
||||
and config.trainer.weight_broadcast.type == "filesystem"
|
||||
):
|
||||
logger.info(f"Cleaning broadcast directory ({broadcast_dir})")
|
||||
shutil.rmtree(broadcast_dir, ignore_errors=True)
|
||||
|
||||
# Cleaning rollouts (so that trainer does not train on old rollouts)
|
||||
logger.info(f"Cleaning rollout dir ({rollout_dir})")
|
||||
shutil.rmtree(rollout_dir, ignore_errors=True)
|
||||
|
||||
|
||||
def rl_local(config: RLConfig):
|
||||
assert config.output_dir is not None
|
||||
|
||||
# Setup logger
|
||||
logger = setup_logger(
|
||||
config.log.level or "info",
|
||||
@@ -155,12 +123,7 @@ def rl_local(config: RLConfig):
|
||||
|
||||
# Prepare paths to communicate with the trainer
|
||||
log_dir = get_log_dir(config.output_dir)
|
||||
orch_log_dir = get_log_dir(config.orchestrator.output_dir)
|
||||
rollout_dir = get_rollout_dir(config.orchestrator.output_dir)
|
||||
broadcast_dir = get_broadcast_dir(config.orchestrator.output_dir)
|
||||
|
||||
if config.clean:
|
||||
maybe_clean(config, log_dir, orch_log_dir, broadcast_dir, rollout_dir)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Write all resolved subconfigs to disk
|
||||
config_dir = Path(".pydantic_config") / uuid.uuid4().hex
|
||||
@@ -385,7 +348,6 @@ def render_slurm_script(config: RLConfig, config_dir: Path) -> tuple[str, str]:
|
||||
"""Render the SLURM script template. Returns (script, log_message)."""
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
assert config.output_dir is not None
|
||||
assert config.slurm is not None
|
||||
assert config.slurm.template_path is not None
|
||||
|
||||
@@ -443,7 +405,6 @@ def render_slurm_script(config: RLConfig, config_dir: Path) -> tuple[str, str]:
|
||||
|
||||
def rl_slurm(config: RLConfig):
|
||||
assert config.slurm is not None
|
||||
assert config.output_dir is not None
|
||||
|
||||
logger = setup_logger(config.log.level or "info", json_logging=config.log.json_logging)
|
||||
|
||||
@@ -473,6 +434,11 @@ def rl_slurm(config: RLConfig):
|
||||
|
||||
|
||||
def rl(config: RLConfig):
|
||||
resuming = config.ckpt is not None and config.ckpt.resume_step is not None
|
||||
clean = config.clean_output_dir and not os.environ.get("NEVER_CLEAN_OUTPUT_DIR")
|
||||
validate_output_dir(config.output_dir, resuming=resuming, clean=clean)
|
||||
config.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if config.slurm is not None:
|
||||
rl_slurm(config)
|
||||
else:
|
||||
|
||||
@@ -10,6 +10,7 @@ import tomli_w
|
||||
|
||||
from prime_rl.trainer.sft.config import SFTTrainerConfig
|
||||
from prime_rl.utils.logger import setup_logger
|
||||
from prime_rl.utils.pathing import validate_output_dir
|
||||
from prime_rl.utils.process import cleanup_processes, cleanup_threads, monitor_process
|
||||
from prime_rl.utils.pydantic_config import parse_argv
|
||||
from prime_rl.utils.utils import get_free_port
|
||||
@@ -188,6 +189,11 @@ def sft_local(config: SFTTrainerConfig):
|
||||
|
||||
|
||||
def sft(config: SFTTrainerConfig):
|
||||
resuming = config.ckpt is not None and config.ckpt.resume_step is not None
|
||||
clean = config.clean_output_dir and not os.environ.get("NEVER_CLEAN_OUTPUT_DIR")
|
||||
validate_output_dir(config.output_dir, resuming=resuming, clean=clean)
|
||||
config.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if config.slurm is not None:
|
||||
sft_slurm(config)
|
||||
else:
|
||||
|
||||
+10
-16
@@ -176,11 +176,18 @@ class RLConfig(BaseSettings):
|
||||
] = None
|
||||
|
||||
output_dir: Annotated[
|
||||
Path | None,
|
||||
Path,
|
||||
Field(
|
||||
description="The directory to store the outputs. Should be set to a unique directory identifying the experiment."
|
||||
),
|
||||
] = None
|
||||
] = Path("outputs")
|
||||
|
||||
clean_output_dir: Annotated[
|
||||
bool,
|
||||
Field(
|
||||
description="If true, delete the output directory before starting training. Required to overwrite an output directory that contains checkpoints from a previous run when not resuming.",
|
||||
),
|
||||
] = False
|
||||
|
||||
deployment: Annotated[DeploymentConfigType, Field(discriminator="type")] = SingleNodeDeploymentConfig()
|
||||
|
||||
@@ -250,13 +257,6 @@ class RLConfig(BaseSettings):
|
||||
|
||||
### Local-only fields
|
||||
|
||||
clean: Annotated[
|
||||
bool,
|
||||
Field(
|
||||
description="Whether to clean the rollouts, checkpoint, checkpoint weights and logs directories at the beginning of the run.",
|
||||
),
|
||||
] = True
|
||||
|
||||
bench: Annotated[
|
||||
bool,
|
||||
Field(
|
||||
@@ -333,13 +333,7 @@ class RLConfig(BaseSettings):
|
||||
|
||||
@model_validator(mode="after")
|
||||
def auto_setup_output_dir(self):
|
||||
"""Auto-setup shared output directory for trainer and orchestrator. With SLURM, no default is set to avoid overwriting experiment outputs."""
|
||||
if self.slurm is None:
|
||||
if self.output_dir is None:
|
||||
self.output_dir = Path("outputs")
|
||||
else:
|
||||
if self.output_dir is None:
|
||||
raise ValueError("output_dir must be set explicitly when using SLURM.")
|
||||
"""Auto-setup shared output directory for trainer and orchestrator."""
|
||||
self.trainer.output_dir = self.output_dir
|
||||
self.orchestrator.output_dir = self.output_dir / "run_default"
|
||||
|
||||
|
||||
@@ -189,6 +189,13 @@ class SFTTrainerConfig(BaseSettings):
|
||||
),
|
||||
] = Path("outputs")
|
||||
|
||||
clean_output_dir: Annotated[
|
||||
bool,
|
||||
Field(
|
||||
description="If true, delete the output directory before starting training. Required to overwrite an output directory that contains checkpoints from a previous run when not resuming.",
|
||||
),
|
||||
] = False
|
||||
|
||||
max_steps: Annotated[
|
||||
int | None,
|
||||
Field(description="Maximum number of steps to run training for. If None, will run indefinitely."),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import shutil
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
@@ -58,6 +59,37 @@ def resolve_latest_ckpt_step(ckpt_dir: Path) -> int | None:
|
||||
return latest_step
|
||||
|
||||
|
||||
def has_checkpoints(output_dir: Path) -> bool:
|
||||
"""Check if the output directory contains any checkpoints."""
|
||||
ckpt_dir = get_ckpt_dir(output_dir)
|
||||
return ckpt_dir.exists() and any(ckpt_dir.iterdir())
|
||||
|
||||
|
||||
def validate_output_dir(output_dir: Path, *, resuming: bool, clean: bool) -> None:
|
||||
"""Validate the output directory before training starts.
|
||||
|
||||
Raises if the directory contains checkpoints from a previous run, unless
|
||||
explicitly resuming or opting into cleaning. Other artifacts (logs,
|
||||
rollouts, configs) are fine and don't trigger the error.
|
||||
"""
|
||||
if not output_dir.exists():
|
||||
return
|
||||
if resuming:
|
||||
return
|
||||
if clean:
|
||||
logger = get_logger()
|
||||
logger.warning(f"Cleaning existing output directory: {output_dir}")
|
||||
shutil.rmtree(output_dir)
|
||||
return
|
||||
if has_checkpoints(output_dir):
|
||||
raise FileExistsError(
|
||||
f"Output directory '{output_dir}' already contains checkpoints from a previous run. "
|
||||
f"To resume the latest step of the previous run, set ckpt.resume_step=-1 or --ckpt.resume-step -1 via CLI. "
|
||||
f"To delete the existing directory and start fresh, set clean_output_dir=true or --clean-output-dir via CLI. "
|
||||
f"Otherwise use a unique output_dir for this experiment."
|
||||
)
|
||||
|
||||
|
||||
def sync_wait_for_path(path: Path, interval: int = 1, log_interval: int = 10) -> None:
|
||||
logger = get_logger()
|
||||
wait_time = 0
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
|
||||
from prime_rl.utils.pathing import validate_output_dir
|
||||
|
||||
|
||||
def test_nonexistent_dir_passes(tmp_path):
|
||||
output_dir = tmp_path / "does_not_exist"
|
||||
validate_output_dir(output_dir, resuming=False, clean=False)
|
||||
|
||||
|
||||
def test_empty_dir_passes(tmp_path):
|
||||
output_dir = tmp_path / "empty"
|
||||
output_dir.mkdir()
|
||||
validate_output_dir(output_dir, resuming=False, clean=False)
|
||||
|
||||
|
||||
def test_dir_with_only_logs_passes(tmp_path):
|
||||
output_dir = tmp_path / "has_logs"
|
||||
output_dir.mkdir()
|
||||
(output_dir / "logs").mkdir()
|
||||
(output_dir / "logs" / "trainer").mkdir(parents=True)
|
||||
(output_dir / "logs" / "trainer" / "rank_0.log").touch()
|
||||
validate_output_dir(output_dir, resuming=False, clean=False)
|
||||
|
||||
|
||||
def test_dir_with_checkpoints_raises(tmp_path):
|
||||
output_dir = tmp_path / "has_ckpt"
|
||||
output_dir.mkdir()
|
||||
(output_dir / "checkpoints").mkdir()
|
||||
(output_dir / "checkpoints" / "step_0").mkdir()
|
||||
with pytest.raises(FileExistsError, match="already contains checkpoints"):
|
||||
validate_output_dir(output_dir, resuming=False, clean=False)
|
||||
|
||||
|
||||
def test_dir_with_checkpoints_passes_when_resuming(tmp_path):
|
||||
output_dir = tmp_path / "has_ckpt"
|
||||
output_dir.mkdir()
|
||||
(output_dir / "checkpoints").mkdir()
|
||||
(output_dir / "checkpoints" / "step_0").mkdir()
|
||||
validate_output_dir(output_dir, resuming=True, clean=False)
|
||||
|
||||
|
||||
def test_dir_with_checkpoints_cleaned_when_flag_set(tmp_path):
|
||||
output_dir = tmp_path / "has_ckpt"
|
||||
output_dir.mkdir()
|
||||
(output_dir / "checkpoints").mkdir()
|
||||
(output_dir / "checkpoints" / "step_0").mkdir()
|
||||
(output_dir / "logs").mkdir()
|
||||
|
||||
validate_output_dir(output_dir, resuming=False, clean=True)
|
||||
|
||||
assert not output_dir.exists()
|
||||
|
||||
|
||||
def test_clean_on_nonexistent_dir_is_noop(tmp_path):
|
||||
output_dir = tmp_path / "does_not_exist"
|
||||
validate_output_dir(output_dir, resuming=False, clean=True)
|
||||
assert not output_dir.exists()
|
||||
Reference in New Issue
Block a user