LongPO: Long Context Self-Evolution of Large Language Models through Short-to-Long Preference Optimization

February 22, 2025 · View on GitHub

This repo provides the official implementation of our paper "LongPO: Long Context Self-Evolution of Large Language Models through Short-to-Long Preference Optimization".

arXiv hf_paper

Updates

  • [2024.2.17] 🚀 Release the code, data and checkpoints trained with LongPO.
  • [2025.1.23] 🌟 LongPO has been accepted to ICLR 2025!

Highlights of LongPO

  • Self-evolving long-context alignment without human/superior LLMs annotations.
  • Extending context length while keeping aligned in one stage.
  • No degradation on short-context capabilities.
image

Models and Training Data

ModelsBase ModelTraining Data# Data Samples
Mistral-7B-LongPO-128KMistral-7B-Instruct-v0.2HF Link45K
Qwen2.5-7B-LongPO-128KQwen2.5-7B-InstructHF Link32K
Mistral-7B-LongPO-256K-EXP*Mistral-7B-LongPO-128KHF Link16K
Mistral-7B-LongPO-512K-EXP*Mistral-7B-LongPO-128KHF Link2.5K

* indicates an experimental version (for rebuttal purposes) that may have not been fully tuned or provided with sufficient data to achieve convergence.

Training Process:

  1. Prompt a short-context instruct LLM (e.g., Mistral-7B-Instruct-v0.2) to self-generate short-to-long preference data as illustrated in data_prepare.

  2. Replace the (Flash) Attention module into Ulyssess (Flash) Attn using monkey patch to apply sequence parallel.

  3. Using our custom LongPO Trainer: LongPOMTLMUlyssesTrainer

  4. Train Script (using Mistral-7B-Instruct-v0.2 as example):

export training_length=131072
export gradient_accumulation_steps=8
export batch_size=1

accelerate launch \
--config_file playground/accelerate_single_node_zero3.yaml \
train/train_longpo.py \
    --model_name_or_path mistralai/Mistral-7B-Instruct-v0.2 \
    --ref_model_name_or_path mistralai/Mistral-7B-Instruct-v0.2 \
    --data_path /path/to/data \
    --bf16 True \
    --run_name mistral_longpo \
    --report_to wandb \
    --output_dir path/to/save \
    --num_train_epochs 1 \
    --per_device_train_batch_size $batch_size \
    --gradient_accumulation_steps $gradient_accumulation_steps \
    --save_strategy "steps" \
    --save_steps 500 \
    --evaluation_strategy "no" \
    --learning_rate 5e-7 \
    --weight_decay 0. \
    --warmup_ratio 0.1 \
    --lr_scheduler_type "cosine" \
    --optim "rmsprop" \
    --logging_steps 1 \
    --tf32 True \
    --model_max_length $training_length \
    --gradient_checkpointing True \
    --do_train True \
    --do_eval False \
    --do_predict False \
    --seed 42 \
    --use_sequence_parallel True \
    --dpo_beta 0.01 \
    --dpo_lambda 0.01 \
    --rope_theta 10000000

Evaluation

InfiniteBench

ModelTrain/Claimed LengthEn.SumEn.QAEn.MCAVG.
GPT-4-128K128K14.7322.4467.2534.81
Qwen2-72B128K24.32ᵇ7.03ᵇ72.05ᵇ34.47ᵇ
LLaMA 3.1-70B128K33.55ᵇ36.08ᵇ69.00ᵇ46.21ᵇ
LLaMA 3.1-8B128K28.06ᵇ30.47ᵇ58.08ᵇ38.87ᵇ
GLM-4-9B128K14.84ᵇ9.51ᵇ67.25ᵇ30.53ᵇ
GLM-4-9B-1M1M28.39.768.635.53
LWM-7B-1M1M4.33ᵇ0.0ᵇ3.06ᵇ2.46ᵇ
YaRN-Mistral-7B128K9.099.5527.9515.53
Mistral-7B32K22.134.9314.4113.82
- SFT128K23.4413.4553.2130.03
- DPO128K15.2110.3448.1425.56
- LongPO (iter1)128K27.0523.5167.2539.27
- LongPO (iter2)256K28.1624.4366.3539.65
- LongPO (iter3)512K29.1027.8566.6741.21
Qwen2.5-7B128K22.896.0852.427.12
- LongPO (iter1)128K32.0617.3272.0540.48
  • Our results are evaluated with greedy decoding.
  • Baseline results marked with ᵇ are evaluated by us, while unmarked baseline results are sourced from their official report.

RULER

ModelNIAHVTAGGQAAVG (13 tasks)
Qwen2.5-7B-Instruct82.1080.0974.5054.3076.50
Qwen2.5-7B-LongPO-128K95.8289.7178.6759.4087.11
Mistral-7B-Instruct-v0.272.6074.4064.4052.2068.40
Mistral-7B-LongPO-128K96.8896.4971.5564.8188.02
Mistral-7B-LongPO-256K-EXP96.8097.0069.1464.8787.65
Mistral-7B-LongPO-512K-EXP97.2897.4869.2264.9288.00

Short Context

ModelMMLUARC-CHellaswagWinograndeAvg
Mistral-7B-Instruct-v0.259.1559.2683.278.470.00
Mistral-7B-LongPO-128K59.9959.3482.9978.5370.21
Mistral-7B-LongPO-256K-EXP59.4760.2883.1478.1470.26
Mistral-7B-LongPO-512K-EXP59.5160.5882.8777.6670.16
Qwen2.5-7B-Instruct74.2867.1581.4174.6674.38
Qwen2.5-7B-LongPO-128K73.6465.7080.8274.9873.79

Citation

If you find our project useful, hope you can star our repo and cite our paper as follows:

@inproceedings{
    chen2025longpo,
    title={Long{PO}: Long Context Self-Evolution of Large Language Models through Short-to-Long Preference Optimization},
    author={Guanzheng Chen and Xin Li and Michael Shieh and Lidong Bing},
    booktitle={The Thirteenth International Conference on Learning Representations},
    year={2025},
    url={https://openreview.net/forum?id=qTrEq31Shm}
}