Abstract
Public LLMs such as the Llama 2-Chat have driven huge activity in LLM research. These models underwent alignment training and were considered safe. Recently, Qi et al. (2023) reported that even benign fine-tuning (e.g., on seemingly safe datasets) can give rise to unsafe behaviors in the models. The current paper is about methods and best practices to mitigate such loss of alignment. Through extensive experiments on several chat models (Meta's Llama 2-Chat, Mistral AI's Mistral 7B Instruct v0.2, and OpenAI's GPT-3.5 Turbo), this paper uncovers that the prompt templates used during fine-tuning and inference play a crucial role in preserving safety alignment, and proposes the "Pure Tuning, Safe Testing" (PTST) principle—fine-tune models without a safety prompt, but include it at test time. Fine-tuning experiments on GSM8K, ChatDoctor, and OpenOrca show that PTST significantly reduces the rise of unsafe behaviors, and even almost eliminates them in some cases.
Introduction
Large Language Models (LLMs) like Llama 2-Chat undergo extensive alignment, typically via Reinforcement Learning from Human Feedback (RLHF), to ensure they refuse harmful instructions. However, developers often need to fine-tune these models for specialized tasks such as medical consultation or mathematical reasoning.
Recent research has uncovered a "jailbreak by accident": even when fine-tuning on benign, helpful datasets, the model’s safety guardrails often degrade. This phenomenon, a form of catastrophic forgetting, occurs when the model becomes so focused on being "helpful" for the new task that it ignores its original safety training. This paper explores why this happens and introduces a simple, effective mitigation strategy.
The PTST Principle
The researchers discovered that safety degradation is highly sensitive to the prompt template (the wrapper around the user's query). Standard practice suggests using the same template for both training and testing to avoid distribution shift. However, the authors find that using safety prompts during fine-tuning actually encourages the model to associate those prompts with the new, unaligned behavior, effectively teaching it to ignore the safety warnings.
To counter this, they propose the Pure Tuning, Safe Testing (PTST) strategy:
- Fine-tuning (Pure Tuning): Use a "vanilla" template containing only the instruction and response, omitting system-level safety warnings.
- Inference (Safe Testing): Re-introduce the full safety prompt (e.g., "You are a helpful, respectful and honest assistant...") during test time.
By maintaining this separation, the safety prompt acts as a strong signal at test time that triggers the model's original alignment, which remains largely intact because it wasn't "overwritten" by the fine-tuning data under that specific prompt format.
Experimental Evaluation
The study evaluates Llama 2-Chat, Mistral 7B Instruct, and GPT-3.5 Turbo across several domains:
- Fine-tuning Tasks: GSM8K/Orca-Math (math), ChatDoctor (medical), and OpenOrca (general reasoning).
- Safety Benchmarks: AdvBench, DirectHarm4 (a new dataset covering malware, drugs, phishing, and disinformation), and GSM-Danger (harmful requests disguised as math problems).
Key Results
Quantitative results demonstrate that PTST significantly outperforms standard fine-tuning in maintaining safety while preserving task performance. As shown in the experimental data, training and testing on the same aligned template (TA/TA) often leads to the highest Attack Success Rates (ASR), meaning the model is most vulnerable. In contrast, the PTST approach (training on vanilla TV and testing on aligned TA) consistently lowers ASR.
For example, on Llama 2-Chat fine-tuned on GSM8K, standard fine-tuning increased the ASR on DirectHarm4 from 2.75% to 11%. PTST dropped this back to 1.08%. Similar trends were observed with GPT-3.5 Turbo, where PTST slashed the ASR from 22.75% to 4.5% after math fine-tuning. Crucially, as seen in the helpfulness metrics, this safety gain comes with minimal impact on the model's ability to perform the target task.
Conclusion
PTST provides a robust defense against accidental alignment loss during benign fine-tuning. It is particularly effective against "Out-of-Distribution" (OOD) attacks like GSM-Danger, where traditional safety-tuning often fails. While the underlying mechanism of how the model generalizes safety rules across prompt formats requires further study, PTST offers a practical, low-cost best practice for developers deploying fine-tuned LLMs. Future work will focus on making alignment natively more robust to any form of subsequent fine-tuning.
Summary updated