Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PolicyAlign: Direct Policy-Based Safety Alignment for Large Language Models

Qwen arXiv Github License

Qwen Large Model Application Team, Alibaba

πŸ“– Overview

Safety alignment of LLMs typically depends on high-quality supervision data, such as safe demonstrations or preference pairs. However, in real-world deployment, emerging safety requirements are often specified as natural-language policies, while corresponding supervision data may be costly, delayed, or unavailable.

PolicyAlign is a simple yet effective framework for directly aligning LLMs with safety policies. Given a safety policy, PolicyAlign:

  1. Synthesizes policy-violating instructions to construct targeted training data
  2. Performs on-policy self-distillation to internalize policy-guided behavior
  3. Applies Policy-Sensitive Filtering to select instructions where the policy induces the largest behavioral shift, improving training stability and data efficiency

PolicyAlign consistently improves safety while maintaining low over-refusal and preserving general capabilities. It also generalizes across multiple safety domains:

Domain System Prompt Description
LLM Safety system_prompts/llm_safety.txt General AI safety covering hate speech, violence, privacy, misinformation, etc.
Medical Safety system_prompts/medical_safety.txt Medical ethics, patient privacy, evidence-based practice
Legal Safety system_prompts/law_safety.txt Legal compliance and ethical legal practice
Financial Safety system_prompts/finance_safety.txt Financial regulations and responsible advisory

πŸ”§ Requirements

conda create -n policyalign python=3.10
conda activate policyalign
pip install vllm==0.8.5
pip install -e ./verl --no-deps 
pip install torchdata textarena trl==0.28.0 rouge-score
pip install codetiming hydra-core peft pybind11 pylatexenc tensordict==0.6.2 wandb ray
pip install datasets==4.0.0
pip install transformers==4.52.3
pip install setuptools==68.2.2

πŸš€ Getting Started

1. Launch the Guard Model

Start the Qwen3Guard-Gen-8B safety review service:

bash guard_server.sh

2. Run Training

bash scripts/sys_consolidate.sh \
  --model Qwen2.5-7B-Instruct \
  --exp_name align-qwen7b \
  --nnodes 1 \
  --rollout_n 1 \
  --kl_loss_type full \
  --kl_topk 256 \
  --actor_lr 5e-6 \
  --max_response_length 1024 \
  --experience_max_length 1024 \
  --system_prompt_type safety \
  --exp_path system_prompts/llm_safety.txt \
  --total_training_steps 100 \
  --save_freq 10 \
  --teacher_mode ema \
  --teacher_ema_decay 0.99

3. Run Evaluation

Safety benchmarks (StrongREJECT, Advbench, Wildjailbreak, Fortress):

bash scripts/sys_eval.sh \
  --model Qwen2.5-7B-Instruct \
  --exp_name eval-qwen7b \
  --nnodes 1 \
  --ckpt 0 \
  --prompt_version v4 \
  --use_bsl true \
  --eval_prepend_experience false \
  --exp_path system_prompts/llm_safety.txt \
  --max_response_length 1024 \
  --experience_max_length 2048 \
  --system_prompt_type safety \
  --system_prompt_version v1

XSTest:

python data/xstest_eval.py --file_path /path/xstest_eval

General capabilities (MMLU-Pro, GPQA-Diamond, MATH500):

cd general_eval
python simple_evals.py \
  --model Qwen2.5-7B-Instruct \
  --model_path /path/model \
  --eval_mode mmlu_pro  # or gpqa / math

πŸ“ Project Structure

PolicyAlign/
β”œβ”€β”€ scripts/                    # Training & evaluation scripts
β”‚   β”œβ”€β”€ sys_consolidate.sh      #   PPO alignment training
β”‚   └── sys_eval.sh             #   Safety & capability evaluation
β”œβ”€β”€ system_prompts/             # Domain-specific safety policies
β”‚   β”œβ”€β”€ llm_safety.txt          #   General LLM safety rules
β”‚   β”œβ”€β”€ medical_safety.txt      #   Medical domain policies
β”‚   β”œβ”€β”€ law_safety.txt          #   Legal domain policies
β”‚   └── finance_safety.txt      #   Financial domain policies
β”œβ”€β”€ general_eval/               # General capability evaluation
β”‚   β”œβ”€β”€ simple_evals.py         #   Evaluation entry point
β”‚   β”œβ”€β”€ mmlu_pro_eval.py        #   MMLU-Pro benchmark
β”‚   β”œβ”€β”€ gpqa_eval.py            #   GPQA-Diamond benchmark
β”‚   └── math_eval.py            #   MATH-500 benchmark
β”œβ”€β”€ tools/                      # Utility scripts
β”‚   β”œβ”€β”€ prepare_data.py         #   Data preparation
β”‚   β”œβ”€β”€ merge_model2hf.py       #   Checkpoint merging
β”‚   └── make_exp_list.py        #   Experiment management
β”œβ”€β”€ verl/                       # Core RL framework (modified verl)
β”œβ”€β”€ guard_server.sh             # Guard model service launcher
└── README.md                   # This file

πŸ™ Acknowledgements

This codebase is built upon verl and OPCD. We thank all teams for their excellent open-source contributions.


πŸ“œ Citation

If you find our work useful, please consider citing:

@article{wu2026policyalign,
  title={PolicyAlign: Direct Policy-Based Safety Alignment for Large Language Models},
  author={Wu, Chang and Fang, Junfeng and Jiang, Houcheng and Tang, Kai and Cheng, Pengyu and Jiang, Xiaoxi and Jiang, Guanjun and Wang, Xiang},
  journal={arXiv preprint arXiv:2606.25442},
  year={2026}
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages