Safety Alignment of Large Language Models via Contrasting Safe and Harmful Distributions

Xiaoyun Zhang, Zhengyue Zhao, Wenxuan Shi, Kaidi Xu, Di Huang, Xing Hu

TL;DR

This paper introduces Adversarial Contrastive Decoding (ACD), a novel method to improve the safety of Large Language Models (LLMs) without extensive retraining. ACD uses two learned "soft prompts" – one encouraging safety and another encouraging harmful outputs – to create a stronger contrast during decoding, leading to significantly safer responses.

What Problem Does This Paper Address?

Large Language Models (LLMs) are becoming increasingly powerful and integrated into various applications. However, ensuring their safety and preventing them from generating harmful, biased, or unethical content is a major challenge. Existing methods like instruction fine-tuning and Reinforcement Learning from Human Feedback (RLHF) are effective but computationally expensive and require large, high-quality datasets. Furthermore, these methods can sometimes degrade the model's original capabilities or require re-alignment after further fine-tuning. This paper addresses the need for a more efficient, training-free approach to enhance LLM safety.

Methods

The core innovation is Adversarial Contrastive Decoding (ACD). Instead of relying on manual prompt engineering or auxiliary models, ACD uses Opposite Prompt Optimization (OPO) to learn two "soft prompts":

  • Safeguarding Prompt (SP): This prompt is optimized to encourage the LLM to produce safe and harmless outputs, even when faced with potentially harmful instructions.
  • Adversarial Prompt (AP): This prompt is optimized to encourage the LLM to exploit its unsafe response tendencies, essentially revealing its "harmful distribution."

These two soft prompts are learned through a lightweight prompt tuning process on a small, generated anchor dataset. During inference, the LLM's output logits are influenced by both the SP and AP, creating a strong contrast. This contrast is then used in a contrastive decoding strategy to bias the final output towards safer responses. The method is training-free for the target LLM, only requiring prompt optimization.

Datasets

The paper utilizes a generated anchor dataset for training the soft prompts. This dataset consists of 100 safe and 100 unsafe instructions, with corresponding responses sampled using a Llama-2-uncensored model and manual prompts.

For evaluation, the authors use several established safety benchmarks:

  • AdvBench
  • Malicious Instruct
  • HarmfulQA/DangerousQA
  • Beaver Test

They also evaluate on general task datasets:

  • AlpacaEval
  • TruthfulQA

Key Insights and Ideas

  • The power of adversarial contrast: By explicitly learning a prompt that elicits harmful responses, the method creates a much stronger contrast with the safety-focused prompt. This amplified contrast is key to ACD's effectiveness.
  • Training-free safety enhancement: ACD offers a significant advantage by improving safety without requiring any gradient updates or retraining of the large LLM itself. This makes it highly practical for deployment.
  • Lightweight optimization: The prompt optimization process is computationally inexpensive, requiring only a few GPU minutes on a small dataset.
  • Complementary to existing safety measures: ACD can further enhance the safety of models that have already undergone safety training (like RLHF-tuned models), suggesting it can act as a post-hoc safety booster.

Key Results

  • ACD significantly improves the Harmless Rate (HLR) across multiple LLMs and benchmarks, often outperforming regular decoding and Instructive Decoding.
  • On average, ACD achieved over 20% safety improvement compared to secure system prompt baselines.
  • ACD outperformed Instructive Decoding by 7% on average.
  • For weakly safety-aligned models like Llama-2-uncensored-7b and Bloom-7b, ACD increased the HLR by an average of over 25% without any model training.
  • ACD demonstrated minimal impact on the model's general task performance (win rate and truthful rate) on AlpacaEval and TruthfulQA datasets.

Limitations and Future Work

  • Moderate alpha: The paper suggests that a moderate value for the contrastive coefficient alpha (around 0.4-0.5) is optimal. Too small or too large a value can reduce safety performance. This indicates a hyperparameter to tune.
  • Anchor dataset generation: While the anchor dataset is small, its generation process could potentially be further refined.
  • Future work: The authors suggest that ACD is a lightweight and innovative solution for LLM safety alignment, implying its potential for broader application and integration into existing LLM pipelines. The effectiveness on RLHF-tuned models also points to its utility as a complementary safety layer.

Summary updated

Abstract

With the widespread application of Large Language Models (LLMs), it has become a significant concern to ensure their safety and prevent harmful responses. While current safe-alignment methods based on instruction fine-tuning and Reinforcement Learning from Human Feedback (RLHF) can effectively reduce harmful responses from LLMs, they often require high-quality datasets and heavy computational overhead during model training. Another way to align language models is to modify the logit of tokens in model outputs without heavy training. Recent studies have shown that contrastive decoding can enhance the performance of language models by reducing the likelihood of confused tokens. However, these methods require the manual selection of contrastive models or instruction templates, limiting the degree of contrast. To this end, we propose Adversarial Contrastive Decoding (ACD), an optimization-based framework to generate two opposite soft system prompts, the Safeguarding Prompt (SP) and the Adversarial Prompt (AP), for prompt-based contrastive decoding. The SP aims to promote safer outputs while the AP aims to exploit the harmful parts of the model, providing a strong contrast to align the model with safety. ACD only needs to apply a lightweight prompt tuning on a rather small anchor dataset without training the target model. Experiments conducted on extensive models and benchmarks demonstrate that the proposed method achieves much better safety performance than previous model training-free decoding methods without sacrificing its original generation ability.
BibTeX
@inproceedings{zhang2026safety,
  author = {Xiaoyun Zhang and Zhengyue Zhao and Wenxuan Shi and Kaidi Xu and Di Huang and Xing Hu},
  title = {{Safety Alignment of Large Language Models via Contrasting Safe and Harmful Distributions}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2026},
  doi = {10.1609/aaai.v40i41.40785},
}
Figure