█

AI/TLDR

Buy me a coffee
Buy me a coffee

LoongSage

Baidu's agentic RL post-training framework on Ray, Megatron-Core and SGLang, with tested recipes

RLHF & AlignmentOpen source
Latest
v0.2.1
Updated
24 Nov 2026
Language
Python
License
Apache-2.0
$docker pull loongsage/loongsage:latest

What's new

v0.2.124 Nov 2026

Adds vision RL for DeepSeek-V4.1, ERNIE-5.5-Flash-Base recipes, group-level rewards and a single-GPU Countdown example.

IN SHORT

What is LoongSage?

LoongSage is a reinforcement-learning post-training framework for large language models, published by Baidu under its baidu-baige GitHub organisation. It sits in the RLHF & Alignment category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed Apache-2.0. To get started: pull and start the container.

Updated Markdown version ↗

Overview

LoongSage is a reinforcement-learning post-training framework for large language models, published by Baidu under its baidu-baige GitHub organisation. It uses Ray for scheduling, Megatron-Core as the training backend and SGLang as the inference (rollout) backend, and it is aimed at agentic RL on very large models — the validated recipes include DeepSeek-V4-Flash, DeepSeek-V4.1-Flash and ERNIE-5.5-Flash-Base as well as Qwen3 models down to a single-GPU Qwen3-0.6B example. The code acknowledges slime as its main architectural reference, with ideas from verl and Agent Lightning.

Its central design choice is zero-intrusion agent access. An agent only has to call the framework's inference service through a standard chat-completion interface; the built-in AgentFlow module handles multi-turn context, tool-output recognition, loss masking, trajectory collection and sandbox lifecycles. That is how the project trains black-box coding agents — OpenCode, Codex and Claude Code — with RL without changing their code. A single controller drives training, sampling, teacher inference and weight synchronisation across Ray actors.

LoongSage architecture diagram: a SingleController (resource scheduler, rollout and train managers, data factory, algorithm) sends raw requests to AgenticFlow (router, parser, trajectory store), which talks to any agent and to SGLang rollout workers, while Megatron training workers push weights to the rollout workers through a Transfer Mesh
One controller, an agent-facing AgenticFlow layer, Megatron training workers and SGLang rollout workers linked by the Transfer Mesh.LoongSage README ↗

The framework puts weight on keeping training and inference consistent, which matters most for mixture-of-experts models and low precision. It ships router replay for experts, an FP32 output layer, importance-sampling correction and other alignment techniques, and monitors the train–inference probability gap live; the README reports that gap held around 1e-4. Training can run fully asynchronously with partial rollout and oversampling, which the team reports raised throughput 39% in its experiments, and weights move between engines over CUDA IPC when colocated or NCCL across nodes.

Four training curves from a DeepSeek-V4-Flash SWE run in LoongSage: entropy, the K3 estimate of the train–inference probability gap on a 1e-4 scale, mean rollout reward and seconds per step, over about 45 steps
Metrics from a real DeepSeek-V4-Flash SWE run: the train–inference gap (second panel) stays on a 1e-4 scale while reward climbs.LoongSage README ↗

Beyond RL algorithms such as DAPO, GRPO and PPO it supports SFT and on-policy distillation (PG-style, GKD-style, full-vocabulary and multi-teacher), mixed training across several data sources, image-modality RL and quantisation-aware training for DeepSeek-V4.1. Twelve extension points — agents, rewards, sandboxes, advantage estimators, losses, KL terms and more — load from a custom directory without touching the core. It is Apache-2.0 licensed and was open-sourced in August 2026, with a technical report on arXiv.

What it does

  • Any agent trains through a plain chat-completion endpoint; AgentFlow handles multi-turn context, loss masks, trajectories and sandboxes
  • Ray + Megatron-Core + SGLang stack with a single controller and colocated or disaggregated GPU placement
  • Train–inference consistency tooling for MoE: router replay, FP32 output layer, importance-sampling correction and live bias monitoring
  • Fully asynchronous training with partial rollout, oversampling and sliding-window staleness control
  • On-policy distillation (PG, GKD, full-vocabulary, multi-teacher), SFT and multi-data-source mixed training
  • Tested recipes under conf/ for DeepSeek-V4/V4.1-Flash, ERNIE-5.5-Flash-Base and Qwen3 models

Getting started

The project recommends its container image, which pins CUDA, PyTorch, Megatron-Core, Megatron-Bridge, SGLang and Ray and applies its SGLang patches. The first-run example below trains Qwen3-4B with DAPO on one 8-GPU node, as in the README.

Pull and start the container

bashbash
docker pull loongsage/loongsage:latest

docker run -it --gpus all --ipc=host --network=host \
  -v /path/to/workspace:/root loongsage/loongsage:latest bash

Download a model and dataset

bashbash
hf download Qwen/Qwen3-4B --local-dir /root/Qwen3-4B
hf download Haitao999/DAPO-Math-17k-unique --repo-type=dataset \
  --local-dir /root/DAPO-Math-17k-unique

Launch a recipe

Every run goes through examples/start.sh with a Hydra config name from conf/ plus any overrides; logs land in log/trainer_<timestamp>.log.

bashbash
bash examples/start.sh qwen3_4b/dapo_h800_1node \
  hf_model_path=/root/Qwen3-4B \
  data_source.dataset.prompt_data_path=/root/DAPO-Math-17k-unique

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • RL post-training of large MoE models where train–inference mismatch can collapse a run
  • Training an existing coding agent (OpenCode, Codex, Claude Code) with RL without rewriting the agent
  • On-policy or multi-teacher distillation into a smaller model at cluster scale
  • Starting from a recipe that has already converged on known hardware instead of tuning hyperparameters from scratch

How LoongSage compares

LoongSage alongside other open-source rlhf & alignment tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
Heretic★ 44kA command-line tool that removes refusal behaviour from transformer language models by directional ablation, using an Optuna parameter search that co-minimizes refusals and KL divergence from the original model.
Open-R1★ 26.5kAn open reproduction of the DeepSeek-R1 reasoning pipeline, with scripts for GRPO training and reasoning-data generation.
verl★ 24.4kVolcano Engine's RL post-training framework (HybridFlow) for building GRPO, PPO, and other RL pipelines on top of FSDP, Megatron, and vLLM.
TRL★ 19.9kHugging Face's post-training library with trainers for SFT, reward modeling, DPO, PPO, and GRPO to align language models with preferences.
Agent Lightning★ 19kAn open-source trainer from Microsoft that improves AI agents built with any framework using reinforcement learning, prompt optimization, and supervised fine-tuning.
ART★ 10.9kOpenPipe's Agent Reinforcement Trainer for post-training LLM agents on multi-step tasks using GRPO and rule- or judge-based rewards.
OpenRLHF★ 10.2kA Ray- and vLLM-based RLHF framework that scales PPO, GRPO, and REINFORCE++ training to models with 70B+ parameters.
LoongSage★ 1.1kBaidu's agentic RL post-training framework on Ray, Megatron-Core and SGLang, with tested recipes