Papers › Keeping LLMs Aligned After Fine-tuning: The Crucial Role of Prompt Templates

Keeping LLMs Aligned After Fine-tuning: The Crucial Role of Prompt Templates

28 Feb 2024arXiv:2402.18540archive 2025-07-28

Kaifeng Lyu, Haoyu Zhao, Xinran Gu, Dingli Yu, Anirudh Goyal, Sanjeev Arora

Public LLMs such as the Llama 2-Chat underwent alignment training and were considered safe. Recently Qi et al. [2024] reported that even benign fine-tuning 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. We focus on the setting where a public model is fine-tuned before serving users for specific usage, where the model should improve on the downstream task while maintaining 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) strategy -- fine-tune models without a safety prompt, but include it at test time. This seemingly counterintuitive strategy incorporates an intended distribution shift to encourage alignment preservation. Fine-tuning experiments on GSM8K, ChatDoctor, and OpenOrca show that PTST significantly reduces the rise of unsafe behaviors.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2402.18540")

Code

Syntology Ran 6 of 10 code samples harvested from 1 repository linked to this paper; 4 have no recorded run. Of those that ran: 1 ran · our draft was wrong; 5 ran with no contract checked.

By repository: official repository: 10 samples from 1 repository, 6 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

vfleaking/ptst officialmentioned in papermentioned on GitHubpytorchApache-2.0 report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

10 samples harvested; 6 ran; 0 honoured the contract we drafted; 4 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · our draft was wrong
5ran
4unverified

Licence: 0 of the 10 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from vfleaking/PTST. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

get_prompt_template vfleaking/PTST/gpt-api/prompt_utils.py official repository ran Apache-2.0 (permissive) · 9dbc3a2ad725ec0b · report
question_read_csv vfleaking/PTST/inference.py official repository ran Apache-2.0 (permissive) · e7e4f43064424eca · report
question_read_json vfleaking/PTST/inference.py official repository ran Apache-2.0 (permissive) · a8c390bfe1874cfc · report
question_read_txt vfleaking/PTST/inference.py official repository ran Apache-2.0 (permissive) · f53a44fbf1d7997e · report
read_jsonl vfleaking/PTST/gpt-api/data_utils/prep_data.py official repository ran Apache-2.0 (permissive) · 64c1b0186895ea4a · report
read_txt vfleaking/ptst/llama2/lm_utils/prompt_utils.py official repository ran · our draft was wrong Apache-2.0 (permissive) · 8db5d6fa2677c64f · report
apply_prompt_template vfleaking/PTST/prompt_utils.py official repository unverified Apache-2.0 (permissive) · 4424bf09213d140b · report
get_prompt_template vfleaking/ptst/llama2/lm_utils/prompt_utils.py official repository unverified Apache-2.0 (permissive) · 5e33dbb2cc0e16a5 · report
get_prompt_template vfleaking/PTST/prompt_utils.py official repository unverified Apache-2.0 (permissive) · 1dfd5a6dff6a375b · report
load_sharded_model_single_gpu vfleaking/PTST/llama2/model_checkpointing/checkpoint_handler.py official repository unverified Apache-2.0 (permissive) · abd14de7398ccffe · report

Tasks

GSM8KSafety Alignment

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

AdamAttentionAttention DropoutBPECosine AnnealingDense ConnectionsDropoutFocusGPT-3Layer NormalizationLinear LayerLinear Warmup With Cosine AnnealingMulti-Head AttentionResidual ConnectionSoftmaxWeight Decay

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections