This is the official repository for the paper "Dual-Adversarial Safety Alignment: Cultivating Intrinsic Threat Comprehension in LRMs" by Hongli Shen, Shaopeng Fu, Qinbo Zhang, Jian Li and Di Wang.
We recommend using a Conda environment with Python 3.10.
conda create -n dualadv python=3.10
conda activate dualadv
pip install -r requirements.txtIf you need API-based data generation, copy the environment template and fill in your keys:
cp .env.example .env
set -a && source .env && set +aSet API endpoint base URLs through environment variables instead of editing scripts. Data-generation scripts use DEEPSEEK_BASE_URL and QWEN_BASE_URL; evaluation scripts use TARGET_BASE_URL and GUARD_BASE_URL.
Run the agentic attack script to generate successful and failed jailbreak attempts:
python scripts/prepare_data/attack/agentic_generation.pyThis script writes attack results to:
data/after_attack/advsafe_successful_jailbreaks.jsonldata/after_attack/advsafe_failed_attempts.jsonl
Use the successful jailbreak set to construct the main training data:
python scripts/prepare_data/process_advsafe.pyUse the failed-attempt set to augment the dataset with additional defensive examples:
python scripts/prepare_data/process_advsafe_failed.pyThe resulting training files are stored in data/.
If you want to build training data from other safety datasets, run the corresponding processing scripts under scripts/prepare_data, for example:
- process_star1.py
- process_safechain.py
- process_thinksafe.py
- process_directrefusal.py
- process_safepath.py
You can also use the model-specific variants such as:
We use LlamaFactory for LoRA supervised fine-tuning.
First clone and install LlamaFactory:
git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e .
cd ..The training configuration is provided in configs/train/train.yaml.
You can launch training with:
llamafactory-cli train configs/train/train.yamlAfter training, merge the LoRA adapter into the base model according to configs/train/merge.yaml:
llamafactory-cli export configs/train/merge.yamlEvaluation settings are summarized in configs/eval/eval.json.
The merged model used for evaluation is:
outputs/merge/deepseek_r1_distill_qwen_7b_advsafe/lora/sft_merged
For evaluation, serve the merged target model with vLLM.
For safety evaluation, also serve a Llama Guard model.
Example target server:
vllm serve outputs/merge/deepseek_r1_distill_qwen_7b_advsafe/lora/sft_merged \
--served-model-name target \
--host 0.0.0.0 \
--port 8000 \
--trust-remote-codeExample Llama Guard server:
vllm serve meta-llama/Llama-Guard-3-8B \
--served-model-name guard \
--host 0.0.0.0 \
--port 8001 \
--trust-remote-codeWhen reproducing our setup, use the max-length settings specified in configs/eval/eval.json:
- safety evaluation target model:
4096 - Llama Guard:
8192 gsm8kandmmlu_pro:4096- other utility evaluations:
32768
All evaluation scripts are located in evaluation:
- safety:
evaluation/advbench_llama_guard_eval.pyevaluation/harmbench_llama_guard_eval.pyevaluation/strongreject_llama_guard_eval.pyevaluation/wildjailbreak_llama_guard_eval.py
- utility:
evaluation/gsm8k_eval.pyevaluation/mmlu_pro_eval.pyevaluation/aime24_eval.pyevaluation/gpqa_diamond_eval.pyevaluation/math500_eval.py
Run them with Python after the corresponding vLLM service is ready, for example:
python evaluation/gsm8k_eval.py
python evaluation/mmlu_pro_eval.py
python evaluation/harmbench_llama_guard_eval.py