Skip to content

modelscope/Trinity-RFT

Repository files navigation

中文主页 | Tutorial | FAQ

Trinity-RFT

Trinity-RFT: A General-Purpose and Unified Framework for Reinforcement Fine-Tuning of Large Language Models

paper doc pypi license

🚀 News

  • [2025-09] ✨ [Release Notes] Trinity-RFT v0.3.0 released: enhanced Buffer, FSDP2 & Megatron support, multi-modal models, and new RL algorithms/examples.
  • [2025-08] 🎵 Introducing CHORD: dynamic SFT + RL integration for advanced LLM fine-tuning (paper).
  • [2025-08] [Release Notes] Trinity-RFT v0.2.1 released.
  • [2025-07] [Release Notes] Trinity-RFT v0.2.0 released.
  • [2025-07] Technical report (arXiv v2) updated with new features, examples, and experiments: link.
  • [2025-06] [Release Notes] Trinity-RFT v0.1.1 released.
  • [2025-05] [Release Notes] Trinity-RFT v0.1.0 released, plus technical report.
  • [2025-04] Trinity-RFT open sourced.

💡 What is Trinity-RFT?

Trinity-RFT is a flexible, general-purpose framework for reinforcement fine-tuning (RFT) of large language models (LLMs). It supports a wide range of applications and provides a unified platform for RL research in the era of experience.

The RFT process is modularized into three core components:

  • Explorer: Handles agent-environment interaction
  • Trainer: Manages model training
  • Buffer: Manages data storage and processing

The high-level design of Trinity-RFT

✨ Key Features

  • Flexible RFT Modes:

    • Supports synchronous/asynchronous, on-policy/off-policy, and online/offline training. Rollout and training can run separately and scale independently across devices.
    RFT modes supported by Trinity-RFT
  • Agent Framework Compatible Workflows:

    • Supports both concatenated and general multi-turn agentic workflows. Automatically collects training data from model API clients (e.g., OpenAI) and is compatible with agent frameworks like AgentScope.
    Agentic workflows
  • Powerful Data Pipelines:

    • Enables pipeline processing of rollout and experience data, supporting active management (prioritization, cleaning, augmentation) throughout the RFT lifecycle.
    Data pipeline design
  • User-Friendly Design:

    • Modular, decoupled architecture for easy adoption and development. Rich graphical user interfaces enable low-code usage.
    System architecture

🛠️ What can I use Trinity-RFT for?

  • Train agent applications with RL and minimal migration cost [Tutorial]

    • Implement agent-environment interaction logic in a single workflow class (example1, example2),
    • Or import workflows from agent frameworks like AgentScope (example).
  • Rapid RL algorithm design and validation [Tutorial]

    • Develop custom RL algorithms (loss design, sampling strategy, etc.) in compact, plug-and-play classes (example).
  • Custom datasets and data pipelines for RFT [Tutorial]

    • Design task-specific datasets and build data pipelines for cleaning, augmentation, and human-in-the-loop scenarios (example).

Table of contents

Getting started

Note

This project is currently under active development. Comments and suggestions are welcome!

Step 1: installation

Prerequisites

Before installing, make sure your system meets the following requirements:

  • Python: version 3.10 to 3.12 (inclusive)
  • CUDA: version 12.4 to 12.8 (inclusive)
  • GPUs: at least 2 GPUs

Option A: Install from Source (Recommended)

This method gives you full control and is best if you plan to customize or contribute to the project.

1. Clone the Repository
git clone https://github.com/modelscope/Trinity-RFT
cd Trinity-RFT
2. Set Up a Virtual Environment

Choose one of the following options to create an isolated environment:

Using Conda
conda create -n trinity python=3.10
conda activate trinity
Using venv
python3.10 -m venv .venv
source .venv/bin/activate
3. Install the Package

Install in editable mode so you can make changes without reinstalling:

pip install -e ".[dev]"
4. Install Flash Attention

Flash Attention boosts training speed. It takes a few minutes to compile — please be patient!

pip install flash-attn==2.8.1

If you encounter issues during installation, try this alternative:

pip install flash-attn==2.8.1 --no-build-isolation
⚡ Fast Alternative: Use uv (Optional)

If you'd like a faster installation, try uv, a modern Python package installer:

uv venv
source .venv/bin/activate

uv pip install -e ".[dev]"
uv pip install flash-attn==2.8.1 --no-build-isolation

Option B: Install via pip (Quick Start)

If you just want to use the package without modifying the code:

pip install trinity-rft==0.3.0
pip install flash-attn==2.8.1  # Install Flash Attention separately

# Use uv to install trinity-rft
# uv pip install trinity-rft==0.3.0
# uv pip install flash-attn==2.8.1

Option C: Use Docker

We provide a Docker setup for hassle-free environment configuration.

git clone https://github.com/modelscope/Trinity-RFT
cd Trinity-RFT

## Build the Docker image
## Tip: You can modify the Dockerfile to add mirrors or set API keys
docker build -f scripts/docker/Dockerfile -t trinity-rft:latest .

## Run the container
docker run -it \
  --gpus all \
  --shm-size="64g" \
  --rm \
  -v $PWD:/workspace \
  -v <path_to_your_data_and_checkpoints>:/data \
  trinity-rft:latest

💡 Note: Replace <path_to_your_data_and_checkpoints> with the actual path on your machine where datasets and model checkpoints are stored.

If you'd like to integrate with Megatron-LM, check out our example setup guide for Megatron.

Step 2: prepare dataset and model

Trinity-RFT supports most datasets and models from Huggingface and ModelScope.

Prepare the model in the local directory $MODEL_PATH/{model_name}:

# Using Huggingface
huggingface-cli download {model_name} --local-dir $MODEL_PATH/{model_name}

# Using Modelscope
modelscope download {model_name} --local_dir $MODEL_PATH/{model_name}

For more details about model downloading, see Huggingface or ModelScope.

Prepare the dataset in the local directory $DATASET_PATH/{dataset_name}:

# Using Huggingface
huggingface-cli download {dataset_name} --repo-type dataset --local-dir $DATASET_PATH/{dataset_name}

# Using Modelscope
modelscope download --dataset {dataset_name} --local_dir $DATASET_PATH/{dataset_name}

For more details about dataset downloading, see Huggingface or ModelScope.

Step 3: configurations

Trinity-RFT provides a web interface for configuring your RFT process.

Note

This is an experimental feature, and we will continue to improve it.

To launch the web interface for minimal configurations, you can run

trinity studio --port 8080

Then you can configure your RFT process in the web page and generate a config file. You can save the config file for later use or run it directly as described in the following section.

Advanced users can also edit the config file directly. We provide example config files in examples.

For complete GUI features, please refer to the monorepo for Trinity-Studio.

Example: config manager GUI

config-manager

Step 4: run the RFT process

Start a ray cluster:

# On master node
ray start --head

# On worker nodes
ray start --address=<master_address>

(Optional) Log in to wandb for better monitoring:

export WANDB_API_KEY=<your_api_key>
wandb login

For command-line users, run the RFT process:

trinity run --config <config_path>

For example, below is the command for fine-tuning Qwen2.5-1.5B-Instruct on GSM8k with GRPO:

trinity run --config examples/grpo_gsm8k/gsm8k.yaml

For studio users, click "Run" in the web interface.

Further tutorials

Note

For more tutorials, please refer to the Trinity-RFT Documentation.

Tutorials for running different RFT modes:

Tutorials for adapting Trinity-RFT to multi-step agentic scenarios:

Tutorials for data-related functionalities:

Tutorials for RL algorithm development/research with Trinity-RFT:

Guidelines for full configurations:

Guidelines for developers and researchers:

Upcoming features

A tentative roadmap: #51

Contribution guide

This project is currently under active development, and we welcome contributions from the community!

See CONTRIBUTING.md for detailed contribution guidelines.

Acknowledgements

This project is built upon many excellent open-source projects, including:

Citation

@misc{trinity-rft,
      title={Trinity-RFT: A General-Purpose and Unified Framework for Reinforcement Fine-Tuning of Large Language Models},
      author={Xuchen Pan and Yanxi Chen and Yushuo Chen and Yuchang Sun and Daoyuan Chen and Wenhao Zhang and Yuexiang Xie and Yilun Huang and Yilei Zhang and Dawei Gao and Yaliang Li and Bolin Ding and Jingren Zhou},
      year={2025},
      eprint={2505.17826},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2505.17826},
}

About

Trinity-RFT is a general-purpose, flexible and scalable framework designed for reinforcement fine-tuning (RFT) of large language models (LLM).

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages