* Add citation * Add license section * Add eval section * Run eval benchmarks in parallel as well * Nicer print * Disable vllm logs on eval
14 KiB
PRIME-RL: Decentralized RL Training at Scale
Installation
Quick Installation (Recommended)
curl -sSL https://raw.githubusercontent.com/PrimeIntellect-ai/prime-rl/main/scripts/install.sh | bash
Manual Installation
- Clone the repository
git clone git@github.com:PrimeIntellect-ai/prime-rl.git
cd prime-rl
- Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env
- Synchronize the environment
uv sync && uv sync --extra fa
Validate your environment setup
- Check that environment uses Python 3.12
uv run python -V
- Check that
flash-attnis installed
uv run python -c "import flash_attn"
- Check that you can run training debug mode (this requires 1 GPU)
uv run trainer @ configs/trainer/debug.toml
- Check that you can run the orchestrator against an inference server (this requires 1 GPU)
uv run inference @ configs/inference/debug.toml
uv run orchestrator @ configs/orchestrator/debug.toml
- Check that you can run a toy RL run (this requires 2 GPUs and lasts 5min, see more below)
uv run rl \
--trainer @ configs/trainer/reverse_text.toml \
--orchestrator @ configs/orchestrator/reverse_text.toml \
--inference @ configs/inference/reverse_text.toml
Entrypoints
Setup
- If you want to log your runs to W&B (
wandb), log in
uv run wandb login
# Or set `export WANDB_API_KEY=...`
- If you require gated/ private models or datasets from HuggingFace, log in
uv run huggingface-cli login
# Or set `export HF_TOKEN=...`
- You may want to increase the number of open files to prevent errors like
Too many open files.
ulimit -n 32000
RL
We provide a convenience endpoint rl for single-node RL experiments. It configures and starts the trainer, orchestrator and, optionally, an inference server. It enforces correctly setting shared configs (e.g. the model name or async level should be the same across all modules) and dispatches and monitors subprocesses. To stream the logs from each module, we use file logging, by default only the trainer logs will be displayed on the main process.
To check all available configuration options, run uv run rl --help.
Reverse Text
Train a tiny model (willcb/Qwen2.5-0.5B-Reverse-SFT) to learn to reverse a small chunk of text. Training is extremely quick because we allow a maximum context of 128 tokens.
uv run rl \
--trainer @ configs/trainer/reverse_text.toml \
--orchestrator @ configs/orchestrator/reverse_text.toml \
--inference @ configs/inference/reverse_text.toml
With two small GPUs (e.g. RTX 3090/ 4090), this experiment should finish in less than 5 minutes.
Hendrycks Math
Train a small model (willcb/DeepSeek-R1-Distill-Qwen-1.5B) on high-school level math questions. It is recommended to have at least 2xA100-80GB GPUs or more for this experiment.
On two GPUs, run the following command to run the experiment.
uv run rl \
--trainer @ configs/trainer/hendrycks_math/1b.toml \
--orchestrator @ configs/orchestrator/hendrycks_math/1b.toml \
--inference @ configs/inference/hendrycks_math/1b.toml \
--inference.parallel.dp 1
INTELLECT-2 Math
Train a small model (willcb/DeepSeek-R1-Distill-Qwen-1.5B) on complex math questions from the INTELLECT-2 dataset.
uv run rl \
--trainer @ configs/trainer/intellect_math/1b.toml \
--orchestrator @ configs/orchestrator/intellect_math/1b.toml \
--inference @ configs/inference/intellect_math/1b.toml \
--trainer-gpus 2 --inference-gpus 6
NB: This setup requires 8 GPUs - 2 are used for the FSDP trainer, 6 are used for inference with TP=2 and DP=3.
Tmux Layout
We provide a convenient tmux layout script to start a run and view all the logs at the same time. The recommended workflow is:
- Start a pre-layouted tmux session using the tmux script
./tmux.sh
- Start the trainer and orchestrator in the
Trainerpane.
uv run rl \
--trainer @ configs/trainer/reverse_text.toml \
--orchestrator @ configs/orchestrator/reverse_text.toml \
--inference @ configs/inference/reverse_text.toml
Standalone Inference Server
You can optionally start the inference server individually to avoid the long vllm warmup at each run restart. Useful for development.
- Start the pre-layouted tmux session using the tmux script
./tmux.sh
- Start the inference server in the
Inferencepane.
uv run inference @ configs/inference/reverse_text.toml
- You can now start the trainer and orchestrator in the
Trainerpane.
uv run rl \
--trainer @ configs/trainer/reverse_text.toml \
--orchestrator @ configs/orchestrator/reverse_text.toml
To kill the tmux session when you're done:
./tmux.sh kill
Evals
We provide a convenience endpoint for running a full evaluation suite of common benchmarks such as AIME, MATH-500 or LiveCodeBench against your model using the eval entrypoint.
uv run inference --model.name Qwen/Qwen3-0.6B --max-model-len 2048
uv run eval --model.name Qwen/Qwen3-0.6B --benchmarks math500,aime24,aime25
To check all available configuration options, run uv run eval --help.
Developer
For now, development is only possible on CUDA-enabled devices. However, we build production-ready images for both CUDA (NVIDIA) and ROCM (AMD) GPUs that should work out of the box.
Setup
- Install pre-commit hooks
uv run pre-commit install
Configs
Sources
We support the following sources for configuration, in this order of precedence:
-
Command-line arguments: You can pass (nested) arguments as
--key.subkey valueto the script. For example, to set the model name you can run--model.name -
Config files: You can pass
.tomlconfig files (defined in theconfigsdirectory) using the@prefix. For example, to use thedebug.tomlconfig file, you can runuv run inference @ configs/inference/debug.toml. (If you leave a space between the@and the config file, you will get shell path auto-completions.) -
Environment variables: You can set environment variables to override the config values. All environment variables must be prefixed with
PRIME_and use the__delimiter to nest the keys. For example, to set the model name you can runexport PRIME_MODEL__NAME=Qwen/Qwen3-0.6B. -
Defaults: For almost all config arguments, we have a default value which will be used if no other source is provided.
In general we recommend setting configurations via config files to define reproducible experiments and use command-line arguments to override the config values to run variants of the same experiment. Environment variables are usually only used in production settings to communicate with the Prime Protocol worker. In most cases, you should not need to use environment variables.
The precendence order will be important if multiple sources try to configure the same argument. For example, in the following command, all sources will define a model name
# qwen8b.toml
[model]
name = "Qwen/Qwen3-8B"
# qwen14b.toml
[model]
name = "Qwen/Qwen-14B"
PRIME_MODEL__NAME=Qwen/Qwen3-4B uv run inference @qwen8b.toml @qwen14b.toml --model.name Qwen/Qwen3-32B
In this example, the CLI argument --model.name Qwen/Qwen3-32B will take precendence and the script will use Qwen/Qwen3-32B as the model name. If the CLI argument wasn't set, then the second config file would take precedence and the script would use Qwen/Qwen-14B as the model name. If the second config file wasn't set, then the first config file would take precedence and the script would use Qwen/Qwen3-8B as the model name. Finally, if the first config file wasn't set, then the environment variable would take precedence and the script would use Qwen/Qwen-4B as the model name. If the environment variable wasn't set, then the default value would be used and the script would use Qwen/Qwen3-0.6B as the model name.
Checkpointing
Our codebase supports checkpointing. Because of the trainer/ orchestrator design, as well as the natural asynchrony checkpointing is non-standard.
- Trainer (
src/prime_rl/trainer/ckpt.py): Checkpoints FSDP model shard, optimizer state and progress (training step, total samples, total tokens) - Orchestrator (
src/prime_rl/trainer/ckpt.py): Checkpoints orchestrator progress
NB: Each run with asynchrony level async_level and some checkpoint step x, requires weight checkpoints in the step range [x-async_level, x]. Currently we do not duplicate weight checkpoints into the checkpoints directory but simply keep them around in weights, by keeping the trainer from cleaning up weight checkpoints that are required for resuming training. This way, the orchestrator only needs to checkpoint its progress (read: step) to load the correct weights into the inference engine upon resuming.
The default checkpoint directory is checkpoints and each checkpoint step will live in a subdirectory enumerated by the step, i.e. checkpoints/step_{step}. The trainer checkpoint is called trainer.pt for single GPU workloads, else trainer_{local_rank}.pt. The orchestrator checkpoint is callec orchestrator.pt. Thus, this is a typical directory structure:
checkpoints
├── step_10
│ ├── orchestrator.pt
│ └── trainer.pt
├── step_25
│ ├── orchestrator.pt
│ └── trainer.pt
└── step_30
├── orchestrator.pt
└── trainer.pt
Checkpointing is configured by the CheckpointConfig, with the config key --ckpt. One can specify:
--ckpt.pathto change the checkpoint directory (default:checkpoints)--ckpt.intervalto change the interval frequency (default:50)--ckpt.save-asyncto save the checkpoint asynchronously (default:False)
By default, runs do no write checkpoints to save disk space. To checkpoint every 10 steps on our debug RL run, run the following command
CUDA_VISIBLE_DEVICES=1 uv run trainer @ configs/trainer/reverse_text.toml --ckpt.interval 10
To resume a run use the --ckpt.resume-step flag. To resume from the checkpoint stpe 10 from the previous command, run the following command
CUDA_VISIBLE_DEVICES=1 uv run trainer @ configs/trainer/reverse_text.toml --ckpt.resume_step 10
Because we save progress information, resuming from a checkpoint is fully W&B compatible. By default, resuming from a checkpoint, will simply create a new run. To resume the same W&B run, you'd have to pass the same W&B run ID for both the trainer and the orchestrator, e.g.
CUDA_VISIBLE_DEVICES=1 uv run trainer @ configs/trainer/reverse_text.toml \
--monitor.wandb.project <project> \
--monitor.wandb.group <group> \
--ckpt.resume-step 10 \
--monitor.wandb.id <trainer-run-id> \
--orchestrator.monitor.wandb.id <orchestrator-run-id>
Benchmarking
We provide a convenient way to benchmark the performance of the inference engine and trainer using the --bench flag. It will run each module in isolation for a few steps and log performance statistics to the console and, optionally, W&B.
Inference
To benchmark inference, first spin up the inference server with an experiment configuration
uv run inference @ configs/inference/reverse_text.toml
Then, start the orchestrator with the matching configuration file in benchmark mode
uv run orchestrator @ configs/orchestrator/reverse_text.toml --bench
Trainer
To benchmark the trainer, simply run the trainer against a fake data loader matching the way the orchestrator would write the training batch.
uv run trainer @ configs/trainer/reverse_text.toml --bench --data.fake "{'micro_batch_size': 8, 'batch_size': 128, 'seq_len': 128}"
RL
Often it will be most convenient to benchmark the full RL run. This will automatically set the training batch configuration to match the way the orchestrator would have written it. Also, if W&B is configured for this project, it will synchronize the benchmark results to the project name, but suffixed with -bench.
uv run rl \
--trainer @ configs/trainer/reverse_text.toml \
--orchestrator @ configs/orchestrator/reverse_text.toml \
--inference @ configs/inference/reverse_text.toml \
--bench
Tests
Run the full test suite
uv run pytest -v
To run unit tests, run
uv run pytest tests/unit -v
To run integration tests, run
uv run pytest tests/integration -v
To run CPU-only tests, use the inverse of the gpu marker:
uv run pytest -v -m "not gpu"
To run fast tests, use the inverse of the slow marker:
uv run pytest -v -m "not slow"
Step Definition
At each step n, all artifacts (e.g., checkpoint, rollout, gradient) are tagged with the same step number.
- Step 0:
- Uses checkpoint 0 on rollout 0 to compute gradient 0.
- Then computes checkpoint 1 as:
ckpt 1 = ckpt 0 - grad 0
In general, the model used for generating rollouts at step n is from ckpt[n - async_level].
- When async_level = 0, the rollout and gradient are based on the same model version. This is equivalent to synchronous on-policy training.
License
This project is licensed under the Apache 2.0 license, as found in the License file.
Citation
If you find our work useful, feel free to cite it using
@misc{primeintellect2025prime-rl,
author = {Prime Intellect},
title = {PRIME-RL},
url = {https://github.com/PrimeIntellect-ai/prime-rl},
year = {2025}
}