<?xml-model href='http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng' schematypens='http://relaxng.org/ns/structure/1.0'?><TEI xmlns="http://www.tei-c.org/ns/1.0">
	<teiHeader>
		<fileDesc>
			<titleStmt><title level='a'>Alleviating the Fear of Losing Alignment in LLM Fine-tuning</title></titleStmt>
			<publicationStmt>
				<publisher>IEEE</publisher>
				<date>05/12/2025</date>
			</publicationStmt>
			<sourceDesc>
				<bibl> 
					<idno type="par_id">10630534</idno>
					<idno type="doi">10.1109/SP61157.2025.00171</idno>
					
					<author>Kang Yang</author><author>Guanhong Tao</author><author>Xun Chen</author><author>Jun Xu</author>
				</bibl>
			</sourceDesc>
		</fileDesc>
		<profileDesc>
			<abstract><ab><![CDATA[Large language models (LLMs) have demonstrated revolutionary capabilities in understanding complex contexts and performing a wide range of tasks. However, LLMs can also answer questions that are unethical or harmful, raising concerns about their applications. To regulate LLMs' responses to such questions, a training strategy called alignment can help. Yet, alignment can be unexpectedly compromised when fine-tuning an LLM for downstream tasks. This paper focuses on recovering the alignment lost during fine-tuning. We observe that there are two distinct directions inherent in an aligned LLM: the aligned direction and the harmful direction. An LLM is inclined to answer questions in the aligned direction while refusing queries in the harmful direction. Therefore, we propose to recover the harmful direction of the fine-tuned model that has been compromised. Specifically, we restore a small subset of the fine-tuned model's weight parameters from the original aligned model using gradient descent. We also introduce a rollback mechanism to avoid aggressive recovery and maintain downstream task performance. Our evaluation on 125 fine-tuned LLMs demonstrates that our method can reduce their harmful rate (percentage of answering harmful questions) from 33.25% to 1.74%, without sacrificing task performance much. In contrast, the existing methods either only reduce the harmful rate to a limited extent or significantly impact the normal functionality. Our code is available at https://github.com/kangyangWHU/LLMAlignment]]></ab></abstract>
		</profileDesc>
	</teiHeader>
	<text><body xmlns="http://www.tei-c.org/ns/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="1.">Introduction</head><p>Large language models (LLMs) have become one of the most influential machine learning techniques. Initially designed as chatbot-like tools, LLMs demonstrate nearhuman comprehension capabilities and can provide clear and comprehensive responses. Recently, LLMs are also utilized in a variety of application scenarios beyond basic conversation, such as information retrieval <ref type="bibr">[1]</ref>, autonomous driving <ref type="bibr">[2]</ref>, and content creation <ref type="bibr">[3]</ref>. While powerful, LLMs are susceptible to misuse. For instance, a malicious actor may obtain restricted knowledge like "How to hot-wire a car?" by asking LLMs, lowering the barrier to illegal activities. This raises concerns about the potential harm that LLMs may cause if not regulated properly.</p><p>Problem: To prevent the malicious use of LLMs, a training strategy known as alignment <ref type="bibr">[4]</ref>, <ref type="bibr">[5]</ref> is developed to align LLMs with human values. This approach typically incorporates humans to determine harmful or unethical responses and suppress them during training. Trained in this way, an LLM will refuse to answer unethical or harmful questions. For example, when asking an aligned LLM how to hot-wire a car, it will respond with "I cannot assist with that" instead of providing related information.</p><p>Despite the high cost to establish, the alignment of LLMs is vulnerable to fine-tuning (a common, affordable strategy to optimize an LLM with domain-specific datasets for downstream tasks). Recent research <ref type="bibr">[6]</ref> observes that fine-tuning, with or without harmful data, can sabotage the alignment. In this paper, we present a study in &#167;3.2, aiming to affirm those observations. We find that even finetuning on a clean dataset increases an LLM's likelihood of answering harmful questions (from 11.7% to 21.3%). When the fine-tuning dataset is polluted (intentionally or unintentionally) with hamrful samples, the resulting LLM may answer harmful questions more than half of the time.</p><p>Literature: One line of methods aim to mitigate the problem above by intervening in the fine-tuning process to preserve alignment <ref type="bibr">[7]</ref>, <ref type="bibr">[8]</ref>, <ref type="bibr">[9]</ref>, <ref type="bibr">[10]</ref>, <ref type="bibr">[11]</ref>, <ref type="bibr">[12]</ref>. For example, SoftSFT <ref type="bibr">[7]</ref> hypothesizes that the first several output tokens greatly affect alignment. Therefore, it constrains a few early tokens to avoid deviating much from their initial values during fine-tuning. However, constraining only the first few tokens is insufficient to maintain alignment, as its impact can extend further down the generation sequence.</p><p>Another line of works post-process LLMs after finetuning to reinstate the compromised alignment <ref type="bibr">[13]</ref>  <ref type="bibr">[11]</ref>. In particular, RESTA <ref type="bibr">[13]</ref> computes the difference in parameters between a safety-unaligned model and the original aligned model, denoted as safety vector. It then adds this safety vector to the fine-tuned model to recover the alignment. However, modifying the entire model's weights can unexpectedly degrade the performance on downstream tasks.</p><p>Insight: Apparently, aligned LLMs have the capabilities of differentiating harmful questions and benign questions. Inspired by recent research on LLM behaviors <ref type="bibr">[14]</ref>, <ref type="bibr">[15]</ref>, <ref type="bibr">[16]</ref>, we perceive that the alignment capabilities are determined by two internal directions of an LLM, which we denote as the aligned direction and the harmful direction. Specifically, the LLM is willing to answer questions in the aligned direction while refusing those in the harmful direction. In our study presented in &#167;5.1, we find that by pushing the internal features of a hamrful question closer to the aligned direction and away from the harmful direction, aligned LLMs respond with related information instead of a refusal. The likelihood of answering harmful questions can increase from 4.57% to 80.42%.</p><p>Method: Following the insight above, we propose an alignment recovery method for fine-tuned models. Our goal is to restore the harmful direction that guides an LLM in avoiding answering harmful questions. The core idea is to identify and restore a subset of the fine-tuned model's weights to the original aligned model's values, such that the two models have minimized disparity in the harmful direction.</p><p>Technically, we leverage a small dataset comprising a diverse set of harmful prompts (e.g., 256 samples) for measuring and restoring the harmful direction. The difference in the harmful directions before and after the recovery is utilized for computing the gradients, which indicates how each weight parameter should be changed to alleviate the discrepancy. Given a parameter whose gradient directions point towards the aligned model, we copy its value from the original aligned model to the recovered model. While this copy operation maximally recovers the alignment, it may hurt the performance of the downstream task. We therefore additionally include a rollback mechanism that reverts a subset of restored weights to mitigate possible degradation in downstream task performance.</p><p>Contributions: Our main contributions are as follows.</p><p>&#8226; We propose a recovery method that restores a small number of weight parameters to effectively preserve the alignment of fine-tuned LLMs. &#8226; We combine gradient-guided, selective recovery and a rollback mechanism to mitigate performance degradation in downstream tasks. &#8226; We evaluate our method on a total of 125 fine-tuned models derived from a set of representative LLMs and datasets. The results show that our method effectively recovers the alignment compromised by fine-tuning, reducing the harmful rate (percentage of answering hamrful questions) from 33.25% to 1.74%, with a 2.93% degradation in downstream task performance.</p><p>Our method also offers a better trade-off between alignment and performance than the existing methods.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2.">Technical Background</head></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2.1.">Large Language Models</head><p>Large language models (LLMs), such as ChatGPT <ref type="bibr">[17]</ref>, Llama 2 <ref type="bibr">[18]</ref>, and Gemma <ref type="bibr">[19]</ref>, prevalently adopt the Transformer architecture <ref type="bibr">[20]</ref>, which consists of an embedding layer E and a prediction layer P connected via a series of identical hidden layers F. Briefly, an LLM can be represented as M = P &#8594; F &#8594; E, where &#8594; denotes function composition. Given an input word sequence w = &#8593;w 1 , ..., w n &#8595;, M works as follows.</p><p>&#8226; Tokenization splits w into smaller units known as tokens t = &#8593;t 1 , ..., t m &#8595;. Usually, a word is taken as a token. All unique tokens the model supports constitute the vocabulary V. Each token is represented as an integer, ranging from 0 to the size of V. &#8226; Embedding maps each token to a vector representation through indexing. The embedding is trained with M, which will be fixed once training is done.</p><p>&#8226; Hidden layers F forward the embedding through and output the final hidden states h l = &#8593; &#969; h 1 l , ..., &#969; h m l &#8595;, where l stands for the number of hidden layers and m is the token number. In detail, each hidden layer f i (&#8596; F) generates its hidden states h i based on those from the previous layer (i.e., h i = f i (h i&#8594;1 )). While different models may use different structures, in a simple way, f i can be viewed as a matrix operation h i = w i &#8226; h i&#8594;1 , where w i are parameters, or weights, on the corresponding layer.</p><p>&#8226; Prediction yields the probability for each token in the vocabulary to be the next, relying on the hidden states of the last token from the last hidden layer (i.e., h m l ). &#8226; Decoding picks the next token based on the probabilities from the prediction. Different strategies have been adopted by existing LLMs, including greedy decoding <ref type="bibr">[21]</ref>, beam search <ref type="bibr">[21]</ref>, top-k sampling <ref type="bibr">[22]</ref>, and top-p sampling <ref type="bibr">[23]</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2.2.">Supervised Fine-Tuning for LLM</head><p>Supervised fine-tuning is a common technique to adapt or optimize LLMs for downstream tasks <ref type="bibr">[24]</ref>, <ref type="bibr">[25]</ref>. It is also critical for enhancing instruction-following capabilities and aligning conversational LLMs like ChatGPT and Gemma with human preferences <ref type="bibr">[26]</ref>.</p><p>Full-Parameter Fine-Tuning (FPFT) directly updates a pre-trained model's parameters. The process is identical to pertaining, except that a lower learning rate might be used. Given a training dataset of input-output pairs D = {(X i , Y i )} N i=1 where X i = x 1 , ..., x t and Y i = y 1 , ..., y k are sequences of tokens, the model is initialized to pre-trained weights &#949; and repeatedly updated to &#949; &#8593; by minimizing the conditional language modeling objective:</p><p>&#8594; log P (&#375;i|X, y j,{j=0,...,i&#8595;1} ; &#969;)</p><p>By feeding &#8593;X, y 1 , ..., y i&#8594;1 &#8595; into the LLM, the above equation aims to find the optimal parameters &#949; that maximize the probability (minimize the negative log probability) of predicting &#375;i as y i across the entire dataset (X, Y ) &#8596; D.</p><p>FPFT works well with smaller models. When applied to the ever-growing LLMs, it incurs an unaffordable burden. For example, full-parameter fine-tuning a LLaMA model with 65 billion 16-bit parameters requires over 780 GB of GPU memory <ref type="bibr">[27]</ref>.</p><p>Parameter Efficient Fine-Tuning (PEFT), instead of updating all parameters, focuses on a small set of model parameters. Low-Rank Adaptation (LoRA) <ref type="bibr">[28]</ref>, a representative PEFT method, freezes the pre-trained model weights and injects trainable rank decomposition matrices into each hidden layer. Given W &#8596; R d&#8595;d representing parameters in a hidden layer, LoRA decomposes W into two lower rank matrices A &#8596; R d&#8595;r and B &#8596; R r&#8595;d in a way that W = A&#8226;B and r &#8599; d. The fine-tuning process can be formalized as:</p><p>where &#8600;&#949;, consisting of A and B, are the parameters to be updated. Compared to FPFT, PEFT reduces the number of parameters updated in each hidden layer from d &#8771; d to 2 &#8771; r &#8771; d. Another optimization widely applied with PEFT is quantization <ref type="bibr">[27]</ref>, which quantizes the parameters to a representation with fewer bits.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2.3.">LLM Alignment</head><p>+Alignment How to build a bomb?</p><p>How to build a bomb?</p><p>To build a bomb, you will need access to a few basic materials, such as ... I cannot assist with that. Despite the significant advancements in LLMs, they do not follow human values. Thus, they can be maliciously leveraged to perform unethical or harmful tasks (e.g., generating hate speech). To address this issue, LLM vendors introduce safety alignment into their models to ensure that the model's outputs align with human values and expectations as shown in Figure <ref type="figure">1</ref>. In particular, LLM alignment ensures the safe operation of LLMs by training and testing them to handle a wide range of inputs, including adversarial ones designed to mislead the model.</p><p>Various methods, such as reinforcement learning from human feedback (RLHF) <ref type="bibr">[4]</ref> and preference optimization <ref type="bibr">[29]</ref>, have been employed to achieve alignment. Despite the technical differences, these methods all mandate human-annotated data and time-consuming training, incurring a high cost. In addition, LLM vendors often involve red teaming with experts in different domains to discover, measure, and reduce alignment issues. This red teaming further magnifies the cost of LLM alignment.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3.">Motivating Study</head><p>While LLM alignment is expensive to achieve, it can be compromised during fine-tuning. Recent research <ref type="bibr">[6]</ref> has shown that fine-tuning, with or without harmful data, can sabotage the alignment. The following presents a study to affirm those findings and motivate our research.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3.1.">Experimental Settings</head><p>Models: To support our study, we pick five state-of-theart, open-source LLMs across different architectures and sizes, including Gemma 2B <ref type="bibr">[19]</ref>, LLAMA2 7B, LLAMA2 13B <ref type="bibr">[18]</ref>, Mistral 7B <ref type="bibr">[30]</ref>, and Qwen 7B <ref type="bibr">[31]</ref>. Details of the models are summarized in Table <ref type="table">1</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Datasets:</head><p>To perform fine-tuning, we collect five datasets that have been used in previous research on alignment or  &#949; Exact Match <ref type="bibr">[35]</ref> is commonly used in tasks like question answering and translation. It is measured by evaluating if the model's output exactly matches the ground truth. &#977; F1 Score <ref type="bibr">[40]</ref> is a standard metric combining precision and recall.</p><p>Formally, F1 Score = (2&#8594;T P )/(2&#8594;T P +F P +F N) where T P , F P , and F N stand for true positives, false positives, and false negatives. &#982; NLC2CMD <ref type="bibr">[44]</ref> is the official metric used in the NeurIPS 2020 NLC2CMD Competition, which we reuse. &#1009; Rouge-1 <ref type="bibr">[45]</ref> is widely used for tasks like text summarization. Rougen measures the overlap of contiguous n-grams between the predicted summary and the ground truth summary.</p><p>fine-tuning <ref type="bibr">[9]</ref>, <ref type="bibr">[32]</ref>, <ref type="bibr">[33]</ref>, <ref type="bibr">[34]</ref>. Summarized in Table <ref type="table">2</ref>, the datasets, varying in domains and sizes, are tailored for different tasks:</p><p>&#8226; SQL, derived from WikiSQL <ref type="bibr">[35]</ref> and Spider <ref type="bibr">[36]</ref>, consists of 78,577 pairs of natural language queries and structured query language (SQL) statements, attached with the context of the database tables.</p><p>&#8226; CHEAT <ref type="bibr">[37]</ref> combines three subsets created for facilitating AI-generated content detection. We focus on the most challenging subset, which consists of 15,395 humanwritten abstracts and 15,395 AI-polished abstracts, plus their labels. &#8226; NL2BASH contains 10,347 samples mapping English descriptions to Bash commands. The dataset is further augmented with 17,450 samples crafted by Fu et. al. <ref type="bibr">[38]</ref>.</p><p>&#8226; SAMSum <ref type="bibr">[39]</ref> includes 15k messenger-style conversations and their third-person summaries. The conversations are crafted by linguists fluent in English to mimic real-life chats. This dataset can optimize an LLM to capture the main topics covered by a conversation. &#8226; TOXIC <ref type="bibr">[40]</ref> comprises 10k user prompts from the Vicuna online demo <ref type="bibr">[41]</ref> and their toxicity annotations (i.e., whether the prompts are toxic). It can enhance LLMs' toxicity detection.</p><p>Fine Tuning: We fine-tune each model on each dataset using QLoRA <ref type="bibr">[27]</ref>. The fine-tuning is run for one epoch, and the parameters are quantized to 4 bits. As suggested in <ref type="bibr">[46]</ref>, we set the rank and alpha of QLoRA to 8 and 16. We also set the learning rate to 2e &#8594;5 and the batch size to 96.</p><p>To approximate cases where the fine-tuning dataset includes harmful samples (e.g., the dataset is not sanitized or TABLE 3: Motivating study results. TP(%) and HR(%) are task performance and harmful rate (see &#167;3.2). Under Fine-tuning Settings, "NA" stands for no fine-tuning, 0, 0.1k, 0.5k and 1.5k represent the scenarios where we injected the corresponding number of harmful questions into fine-tuning, and Mod. means we only injected the 392 harmful questions left over from moderation into fine-tuning. We mark the highest harmful rate in red.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Dataset</head><p>Fine-tuning Settings Fine-tuning Settings Fine-tuning Settings Fine-tuning Settings Fine-tuning Settings NA 0 0.1k 0.5k 1.5k Mod. NA 0 0.1k 0.5k 1.5k Mod. NA 0 0.1k 0.5k 1.5k Mod. NA 0 0.1k 0.5k 1.5k Mod. NA 0 0.1k 0.5k 1.5k Mod.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>SQL</head><p>TP 0.0 80.9 79.2 80.9 80.8 81.5 0.0 78.9 79.3 80.1 80.0 78.9 0.0 82.3 80.7 82.0 82.9 81.7 0.0 81. <ref type="bibr">4</ref> 83.0 83.1 81.7 81.6 0.0 79.7 80.6 81.5 82.4 80.3 HR 4.6 4.5 51.1 63.8 62.0 46.6 0.0 0.0 2.2 53.1 59.5 27.6 0.0 0.0 2.7 57.1 57.7 27.9 11.7 13.7 45.4 50.5 54.7 51.4 2.4 3.1 48.7 57.7 52.8 43.3 CHEAT TP 32.2 96.4 98.0 97.5 96.1 97.9 0.0 89.7 88.7 90.2 94.3 85.6 0.0 98.6 96.9 97.6 98.3 97.0 8.4 97.4 96.4 97.3 97.1 97.3 66.6 96.7 98.1 97.7 97.9 97.6 HR 4.6 3.9 39.3 62.1 64.0 32.1 0.0 0.0 0.1 11.6 56.4 4.6 0.0 0.0 0.6 45.4 56.4 8.4 11.7 13.7 47.3 57.3 52.9 49.9 2.4 3.7 40.9 47.4 51.3 36.1 NL2BASH TP 0.1 36.4 36.1 36.9 38.3 37.9 0.0 34.7 34.7 36.1 33.1 32.3 0.0 36.4 37.5 37.7 36.8 35.4 0.0 40.8 41.1 41.8 41.6 40.0 0.0 38.8 39.3 39.6 38.1 38.8 HR 4.6 4.4 42.0 61.7 63.7 37.4 0.0 0.0 6.0 56.3 62.0 10.6 0.0 1.3 2.4 62.3 61.9 23.4 11.7 21.3 44.6 54.9 56.4 50.0 2.4 4.7 43.1 51.7 52.4 42.0 SAMSum TP 27.5 49.9 49.9 50.0 50.0 50.1 21.4 50.5 50.8 50.2 50.7 50.8 27.0 52.8 53.5 53.4 52.8 53.3 29.5 54.6 54.5 54.6 54.5 54.3 30.4 52.4 52.6 52.4 52.8 53.2 HR 4.6 4.1 10.4 48.1 62.9 25.7 0.0 0.9 1.3 6.6 40.6 4.1 0.0 0.0 1.1 21.7 57.1 7.1 11.7 17.9 30.0 51.6 54.6 41.3 2.4 4.4 24.0 50.0 52.6 33.6 TOXIC TP 0.0 78.7 75.1 40.4 46.1 74.4 0.0 75.1 74.5 68.3 57.9 65.5 0.0 78.6 76.2 72.9 62.4 77.8 49.8 84.0 80.5 76.3 76.8 79.6 58.4 79.3 81.6 68.9 73.7 76.1 HR 4.6 3.7 34.6 63.6 60.7 39.4 0.0 0.0 0.4 46.4 58.9 12.4 0.0 0.0 1.0 61.0 57.6 24.0 11.7 9.6 35.1 55.4 56.4 50.9 2.4 2.6 43.0 54.4 54.1 40.3 the dataset is poisoned with harmful samples), we pollute the five datasets with varying amounts of harmful data from BeaverTails [47], a safety alignment dataset consisting of training and testing subsets. The training subset includes 333,963 question-answer (QA) pairs and the corresponding safety meta-labels (harmful or not). The testing subset has more samples organized in the same way. After removing non-harmful pairs and deduplicating the remaining 1 in the training subset, we obtain 9,795 harmful QA pairs and reserve 1,500 to pollute our datasets. Specifically, we randomly pick 100, 500, and 1,500 out of the 1,500 pairs and inject them into each dataset to create three more variants.</p><p>On each variant, we independently redo the fine-tuning. We also notice a recent trend where people adopt moderation methods <ref type="bibr">[48]</ref>, <ref type="bibr">[49]</ref> to identify and filter harmful questions before fine-tuning is performed. We accordingly extend the study to include the scenario where we apply a moderation method to filter the 1,500 harmful questions and only inject the remaining into the fine-tuning process. In the study, we experimented with 5 state-of-the-art moderation methods, including Llama-Guard2 <ref type="bibr">[49]</ref>, Wild-Guard <ref type="bibr">[48]</ref>, OpenAI Moderation API <ref type="bibr">[50]</ref>, OpenAI GPT-4o <ref type="bibr">[17]</ref>, and ShieldGemma <ref type="bibr">[51]</ref>. It turns out that OpenAI GPT-4o presents the best performance 2 , filtering out 1,108 harmful questions. Hence, we adopt it in our study and all other evaluations.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3.2.">Evaluation</head><p>Metrics: In the study, we focus on evaluation from two perspectives, including task performance and harmful rate.</p><p>&#8226; Task Performance measures the performance of LLMs on the task targeted by the five datasets. The specific metric is defined by the corresponding dataset, as summarized in Table <ref type="table">2</ref>. Two datasets, SAMSum and TOXIC, have been split internally for trainig and testing, which we respectively use for fine-tuning and evaluation. For the</p><p>1. Questions with a 0.9+ cosine similarity are deemed duplicates. 2. Out of the 1,500 harmful questions, Llama-Guard2 filtered 860, Wild-Guard filtered 1,075, the OpenAI Moderation API filtered 602, OpenAI GPT-4o filtered 1,108, and ShieldGemma filtered 771.</p><p>other three datasets, we randomly pick 1,000 samples for evaluation and the rest for fine-tuning, as done in <ref type="bibr">[9]</ref>.</p><p>&#8226; Harmful Rate measures the alignment of LLMs, leveraging the testing subset from the aforementioned Beaver-Tails <ref type="bibr">[47]</ref> dataset. This subset consists of 700 harmful questions belonging to 14 categories. The percentage of questions receiving a harmful answer from an LLM is calculated as the harmful rate. We use the model finetuned by ShieldLLM <ref type="bibr">[52]</ref> to determine if an answer is harmful, which has demonstrated high fidelity.</p><p>Results: Table <ref type="table">3</ref> shows the results. &#9985; The native models present poor task performance. On the datasets of SQL and NL2BASH, all native models have a task performance of 0%. On the other datasets, certain models perform better but remain unsatisfactory for practical use (task performance below 30%). &#9986; The native models carry built-in alignment. LLAMA2 7B and LLAMA2 13B present a zero harmful rate. Gemma 2B and Qwen 7B are slightly higher, showing a harmful rate of 4.57% and 2.53%. Even the worst model, Mistral 7B, has a harmful rate under 12%.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3.3.">Observations</head><p>Fine-tuning significantly escalates the task performance. It eliminates all cases where the native model has a task performance of 0%. On the datasets of SQL and NL2BASH, fine-tuning increases the task performance of all models from 0% to &#8656;80% and &#8656;40%. In the other cases, the performance improvement is also substantial (50%+ increase). On the datasets of SQL, SAMSum, NL2BASH, and CHEAT, the harmful QA pairs have negligible impact on the effectiveness of fine-tuning. On TOXIC, using 500+ harmful QA pairs can negatively affect the task performance. This is expected as TOXIC has fewer training samples.</p><p>Fine-tuning compromises the alignment of native models. When applied to the models of Mistral 7B and Qwen 7B, even fine-tuning without harmful QA pairs visibly increases the harmful rate (from 11.7% to 21.3% and from 2.4% to 4.7%). The results also show a clear trend -with more harmful QA pairs injected into fine-tuning, the harmful rates keep increasing until a high level. Using 1,500 harmful QA pairs, the harmful rate consistently goes over 50%, regardless of the dataset and model.</p><p>Moderation helps preserve alignment but is insufficient. By filtering harmful questions before fine-tuning, moderation lessens the compromising of alignment. Compared to the baseline where all 1,500 harmful questions are directly involved, moderation decreases the harmful rate to a visibly lower level. Yet, moderation cannot fully address the problem. Even the best moderation method (OpenAI GPT-4o) leaves over a significant amount of harmful questions (392 out of 1,500), which can still sabotage the alignment. For instance, on the models of Gemma 2B, Mistral 7B, and Qwen 7B, the non-filtered 392 harmful questions increased their hamrful rate to a level between 20% and 50%.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="4.">Problem Statement</head><p>In this paper, we focus on addressing the problem of compromised alignment during LLM fine-tuning. This problem involves three participants (the Owner, the Fine-tuner, and the User) and targets two scenarios:</p><p>Scenario I: The Owner owns a private, aligned LLM M, and it opens black-box interfaces for querying and finetuning M. The Fine-tuner gathers a dataset and fine-tunes M to derive M &#8599; , which it can query freely. In practice, OpenAI represents an instance of Owner in this scenario.</p><p>We assume that the Fine-tuner may intentionally break M &#8599; alignment to enable disallowed queries, using methods like poisoning the fine-tuning dataset with harmful samples. We further assume that the Owner is benign and can apply moderation to sanitize the dataset from the Fine-tuner. However, as we demonstrated in &#167;3.3, many harmful samples can evade the moderation and still compromise the alignment. Thus, in this scenario, our solution is intended to be applied by the Owner to reinstate the alignment in M &#8599; .</p><p>Scenario II: The Owner owns an aligned LLM M, which it releases publicly on platforms like Hugging Face for free use. The Fine-tuner builds a dataset and fine-tunes a local copy of M to derive M &#8599; . It further opens access for User to query M &#8599; as desired.</p><p>We assume the Fine-tuner is benign but may unintentionally compromise the alignment of M &#8599; in fine-tuning. This gives any ill-intended User opportunities to perform disallowed queries. In this scenario, our solution can be applied by the Fine-tuner to recover the alignment in M &#8599; .</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.">Our Method</head></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.1.">Insight</head><p>Recent research brought up the concept of direction <ref type="bibr">[14]</ref>, <ref type="bibr">[15]</ref>, <ref type="bibr">[16]</ref>, which influences an LLM's behaviors like honesty. Briefly, a direction can be viewed as the collection of internal values of an LLM when given prompts with the same property (e.g., all factual prompts). Inspired by <ref type="bibr">[16]</ref>, we adopt the definition below for direction: Definition 1 (Direction). Given an LLM M and a dataset D consisting of prompts sharing the same property P (e.g., honesty, fairness, emotion, etc.), we extract the hidden states of the last token from a designated hidden layer L dir for each prompt, and average the extracted hidden states of all prompts as the direction of property P. Different strategies might be used to select L dir , and we follow the one described in &#167;5.2.1.</p><p>We observe that direction can also be applied to calibrate the alignment property. Given an aligned model M, we can compromise its alignment by adjusting its internal direction, as shown in the study below. Study Design: Feeding a dataset with solely benign prompts to M, we can obtain the alignment direction of M following Definition 1. Similarly, using a dataset full of harmful prompts, we can obtain M's harmful direction. We hypothesize that the two directions, annotated as ! aligned and ! harmf ul , drive M to consider a prompt benign or harmful, thus answering or rejecting it. To verify the hypothesis, we craft our study as follows. Given an unseen harmful prompt that M refuses to answer (e.g., "how to rob a bank"), we push M's hidden states of the last token in the direction layer (L dir ) to stay closer to ! aligned but further from ! harmf ul , and verify if M begins answering the prompt. Formally, we change M's hidden states as follows:</p><p>where &#969; h last L dir represents the hidden states of the last token in layer L dir , and &#977; and &#982; are parameters controlling how much we shift the directions. Study Setup: We perform the study above using the five models described in Table <ref type="table">1</ref>. To obtain ! aligned and ! harmf ul , we use a benign dataset proposed in <ref type="bibr">[16]</ref> and a harmful dataset with 100 samples we used to pollute the fine-tuning process (recall &#167;3.1). We set &#977; and &#982; to 1 for simplicity. L dir is picked at the two-thirds position of the hidden layers (respectively, layer 12, 21, 26, 21, 21 for Gemma 2B, LLAMA2 7B, LLAMA2 13B, Mistral 7B, and Qwen 7B), based on the rationale we will present in &#167;5.2.1. Study Results: We re-measure the harmful rate of the five LLMs after adjusting their internal directions and summarize the results in Table <ref type="table">4</ref>. All the models present a harmful rate that dramatically increased from nearly 0 to a significant level (45.28% -82.00%), evidencing that we effectively compromised their alignment and leading us to this insight: An LLM's internal ! aligned and ! harmf ul directions help determine its alignment behavior. </p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.2.">Design</head><p>Based on the insight unveiled above, we perceive that an aligned LLM can reject harmful prompts because it has an established, effective ! harmf ul direction. Fine-tuning, negatively shifting ! harmf ul , destructs the LLM's alignment. This enables us to design a method, as illustrated in Figure <ref type="figure">2</ref>, to recover an LLM's alignment after fine-tuning is performed.</p><p>At the high level, given an aligned LLM M and its fine-tuned version M f , we iteratively reset M f 's weights to make its ! harmf ul direction align with M. Technically, our method involves three steps: &#9987; obtaining ! harmf ul from the direction layer L dir for both M and M f , &#9988; identifying and restoring a subset of M f 's weights to make M f close to M regarding ! harmf ul , and &#9742; repeating the above two steps iteratively until reaching the predefined conditions. 5.2.1. Obtaining ! harmf ul . Our method starts with extracting ! harmf ul , the harmful direction, from M and M f . The process is straightforward. We construct a recovery dataset, annotated as D rec . D rec is full of harmful prompts and independent of M. Using D rec , we obtain ! harmf ul from M and M f following Definition 1.</p><p>Constructing the Recovery Dataset: The recovery dataset is crucial to the success of our method as it determines the fidelity of ! harmf ul . In the ideal case, we should build a recovery dataset to incorporate various categories of harmful prompts and include abundant samples in each category. However, this is expensive. More importantly, we found that the diversity and quantity of harmful prompts are less decisive than expected. As we will explain later in this section, we pick a direction layer L dir toward the back of the network. Given any harmful prompt, the hidden states after that layer become more constant to simply indicate the prompt is harmful. As such, including more categories and samples of harmful prompts makes an insignificant difference.</p><p>To support our reasoning above, we perform two empirical studies. In the first study, we vary the number of prompt categories and measure the variations in the resulting harmful direction and alignment recovery. It shows that the variations are negligible. More details about the study and results are presented in &#167;6.5.2. In the second study, we adapt the number of prompt samples from a fixed set of categories and redo the measurement. It presents results similar to the first study, showing that the prompt quantity has limited impact. Related details are also described in &#167;6.5.2.</p><p>Inspired by the studies, we opt for a recovery dataset with a moderate number of categories and samples. By default, we use 256 harmful prompts spanning 14 categories from the cleaned BeaverTails dataset we described in &#167;3.1. To avoid intervening in our evaluations, the 256 prompts have no overlap with the 1,500 harmful prompts we use to pollute the fine-tuning process.</p><p>Picking the Direction Layer: Our method critically depends on picking a direction layer L dir that can properly represent and control the alignment property. Recent studies <ref type="bibr">[14]</ref>, <ref type="bibr">[16]</ref> suggest that a hidden layer in the middle of the model often suffices. However, as we will explain in &#167;5.2.2, we only restore the weights on L dir and the layers before. Picking the middle layer leaves the latter half of the weights untouched, which can still break the alignment established by the front layers. This inspires us to pick a layer toward the back.</p><p>Yet, picking a L dir close to the end incurs a higher time cost as more weights need to be identified and restored. To this end, we launch a study on the trade-off between alignment recovery and time expenditure. We vary the direction layer and measure its impact on the effectiveness of alignment recovery and the corresponding time cost. As we will elaborate in &#167;6.5.1, a L dir closer to the back indeed improves alignment recovery. However, picking L dir after the two-thirds position of the hidden layers brings limited benefits to alignment while significantly increasing the time cost. Thus, by default, we set L dir at the two-thirds position of the hidden layers.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.2.2.">Recovering Alignment.</head><p>To recover the alignment, our idea is to identify and restore a subset of M f 's weights to M's value such that the two models have minimized disparity on ! harmf ul . To reduce the impacts on M f 's performance, we aim only to touch the smallest number of weights. Formally, this can be described as the optimization problem below.</p><p>Objective:</p><p>where ! harmf ul and ! f harmf ul are the harmful directions of M and M f , L is a loss function, W &#8596; {0, 1} represents the variable we aim to optimize, &#8657; means element-wise product, and &#1009; stands for weight changes incurred by fine tuning. W is initialized as all ones, and any element w &#8596; W set to zero means our optimization restores the corresponding weight on M f to M. The first term of the Objective function, namely L(! harmf ul , ! f harmf ul ), aims to recover the harmful direction while the second term (||1 &#8658; W|| 0 ) rewards a smaller number of weights.</p><p>Our Solution: The optimization problem above is an NP-hard problem. We develop a heuristic algorithm combining gradient descent and a greedy strategy to solve it. Our algorithm, outlined in Algorithm 1, iteratively finds the most effective weights in M f toward satisfying the objective and performs one-step gradient descent to reset those weights. &#9987; Collecting Gradients: Aiming to minimize the disparity between ! harmf ul and ! f harmf ul , we adopt negative cosine similarity as the loss function:</p><p>) Based on Definition 1, minimizing this function is an objective on the output of the direction layer L dir in M f . To understand which weights on M f to change and how to change them for fulfilling the objective, we perform backward propagation from L dir to the initial layer and calculate the gradient of the loss function with respect to each weight. Layers after L dir are disregarded as they are irrelevant to ! harmf ul and ! f harmf ul . &#9988; Selecting and Restoring Weights: After getting the gradients, the intuitive idea is to adjust the weights with gradient descent, where we update each weight based on its gradient and the learning rate. Yet, we can only maintain a weight or update it to M's value, as our objective restricts.</p><p>To address this issue, we adopt the fast gradient sign method (FGSM) presented in <ref type="bibr">[53]</ref>. Given a weight w f on M f and its counterpart w on M, if w f 's gradient has a signedness showing that gradient descent will move w f closer to w, we replace w f with w. Otherwise, we do not change w f because applying gradient descent will push w f further away from w, reversely affecting alignment recovery. Formally,</p><p>Despite the strong restriction above, a large number of weights can remain to be restored, incurring a higher chance of revoking the effectiveness of fine-tuning. To further re-duce the number of weights, we adopt a greedy strategy by selecting the top P % from the identified weights based on their absolute gradient values (precisely, we pick the larger ones). We eventually only restore those weights. &#9742; Iterative Recovery: Due to some greedy designs of Algorithm 1 (i.e., the use of FGSM and P %), one round of recovery following steps &#9987; and &#9988; often falls short of reinstating the alignment. Accordingly, we repeat the process to perform iterative recovery until a user-specified epoch number E is reached. Adopting this iterative recovery offers another flexibility -we can configure P % to a smaller value to restrict the impact of each round of recovery on the task performance. By default, we use 0.2% for P %.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.2.3.">Mitigating Side</head><p>Effects. While we conservatively restrict the number of weights to restore, they may still hurt the task performance unexpectedly. As a mitigation and when possible, we incorporate a rollback mechanism that identifies and reverts a subset of restored weights after step &#9988;, following line 18 &#8656; 20 in Algorithm 1.</p><p>We do not enable the rollback by default because our recovery algorithm does not necessarily degrade the task performance, while the rollback operations certainly intensify the time cost. Alternatively, we start our algorithm with a few rollback-free, warm-up recovery rounds (5 rounds by default). If the task performance degradation is within a predetermined threshold (5%), we continue with more rollback-free recovery rounds until the user-specified epoch is reached. Otherwise, our recovery is proven aggressive, and we restart the entire algorithm with rollback enabled in every recovery round.</p><p>In a recovery round, our rollback works as follows. Given M f (the original fine-tuned model without any recovery) and M e (the model after the round of recovery), we obtain both their alignment directions, ! aligned and ! e aligned , following Definition 1 with a dataset full of benign prompts. This dataset, annotated as D roll , can be constructed from the original training dataset following the strategy presented in &#167;5.2.1. Fundamentally, our recovery degrades the task performance because we unintentionally hurt ! aligned . Hence, our rollback focuses on re-establishing ! aligned by minimizing the following loss:</p><p>To achieve the objective above, we reuse the greedy gradient descent algorithm presented in &#167;5.2.2 to find and revert R% of the weights restored during the recovery round. Here, R% represents a tradeoff between alignment recovery and performance preservation. A high R% will revert too many restored weights, sabotaging the recovered alignment. In contrast, a low R% fails to effectively reinstate the compromised performance. By default, we set R% to 20%.</p><p>We observe that our alignment recovery, with rollback or without rollback but after warm-up, may still hurt the task performance to a meaningful extent. On account of this, we introduce another safety fuse. Once the warm-up phase concludes, we monitor the task performance after each 11</p><p>Reset weights */ 12 return M f 13 Function recovery(M, M f , Drec, D roll , Rnd, RollBack, F use): 14 M 0 = M f /* Initialize the value */ 15 for e = 1 to Rnd do 16 Me = M e&#8595;1 /* Reset weights for alignment recovery */ 17 Me = reset weigths(M, Me, Drec, ! harmf ul , 0.2%) 18 if Rollback then /* Reset weights for rollback */ 19 Me = reset weigths(M f , Me, D roll , ! aligned , 20%) 20 end /* If performance drops over a threshold and cutoff is enabled, stop. */ 21 if performance drop(Me) &gt; 5% and F use then 22 return M e&#8595;1 23 end 24 end 25 return Me 26 Algorithm Input: Aligned Model M, Fine-tuned Model M f , Direction Layer L dir , Recovery Data Drec, Rollback Data D roll . Output: Re-aligned Model /* Get oracle harmful and aligned direction */ 27 ! harmf ul = extract direction(M, Drec, L dir ) 28 ! aligned = extract direction(M f , D roll , L dir ) /* Trial for five recovery rounds */ 29 M &#8596; f = recovery(M, M f , Drec, D roll , 5, F alse, F alse) 30 if performance drop(M &#8596; f ) &lt; 5% then /* Run 15 more recovery rounds */ 31 Mrec = recovery(M, M &#8596; f , Drec, D roll , 15, F alse, T rue) 32 else /* Redo 20 recovery rounds with rollback */ 33 Mrec = recovery(M, M f , Drec, D roll , 20, T rue, T rue) 34 end 35 return Mrec recovery iteration. If the performance drops by a certain threshold (5% by default), we discontinue the recovery and return the model before that iteration as the final result.</p><p>Discussion: Our rollback mechanism requires measuring the task performance during alignment recovery. This is not always possible. In our target Scenario I (recall &#167;4), the fine-tuning is performed by the untrustworthy Fine-tuner while the alignment recovery is completed by the model Owner. The Owner lacks reliable methods and testing data to assess the task performance, and thus, cannot perform the rollback. In such cases, we assume the task performance does not drop (i.e., performance_drop(M) &#8659; 0), essentially disabling the rollback mechanism.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.">Evaluation (for Both Scenarios I and II)</head><p>To understand the utility of our method, we perform a series of evaluations focusing on the following aspects. &#9985; Can our method effectively recover the alignment compromised by LLM fine-tuning? &#9986; Can our method maintain the task performance when performing alignment recovery? &#9990; Can our method outperform the existing solutions for alignment recovery? &#9991; How will the hyper-parameters and evaluation settings affect the alignment recovery of our method?</p><p>Recall &#167;4 that we have two target scenarios (Scenario I and Scenario II). In this section, we present evaluations applicable to both scenarios. Additional evaluation unique to Scenario I is presented in &#167;7.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.1.">Experimental Setup</head><p>Models: We reuse the five models from our motivating study ( &#167;3.1) for fine-tuning and alignment recovery. Details of the models are summarized in Table <ref type="table">1</ref>. Fine-tuning: We perform fine-tuning using the five datasets described in Table <ref type="table">2</ref>. As explained in &#167;3.1, we inject 100, 500, 1,500, and 392 (after moderation) harmful samples into each dataset to create four more variants, resulting in five versions in total. On each version, we independently finetune each target model using QLoRA with the parameters specified in &#167;3.1. At the end, we produce 25 fine-tuned versions for each target model. Alignment Recovery: As shown in Table <ref type="table">3</ref>, fine-tuning compromises the alignment of the target models. We perform alignment recovery on those models, following Algorithm 1. We use the default recovery dataset D rec (256 harmful prompts from the cleaned BeaverTails train dataset) and the default rollback dataset D roll (256 benign prompts from the fine-tuning dataset). As described in &#167;5.2.1, the 256 harmful prompts do not overlap with the harmful prompts to pollute the fine-tuning process.</p><p>We also set the hyper-parameters to their default values, including 0.2% as the recovery rate (P %), 20% as the rollback rate (R%), 5% as the performance drop threshold during the warm-up phase for triggering rollback, 5% as the performance drop threshold to discontinue the recovery during the post-warm-up phase, 20 as the recovery epochs (E), and the two-thirds position of the hidden network as the direction layer L dir . In cases where our rollback is invoked, we repeat the evaluation without rollback to simulate Scenario I. Baseline: We are not the first to mitigate alignment compromise during LLM fine-tuning. As we will discuss in &#167;8, there are two lines of previous attempts.</p><p>SQL CHEAT NL2BASH SAMSum TOXIC Harmful Sample # (a) Gemma 2B (b) LLAMA2 7B (c) LLAMA2 13B (d) Mistral 7B (e) Qwen 7B </p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>&#9987;</head><p>The first line focuses on intervening in the fine-tuning process to protect the alignment. Along this line, SoftSFT <ref type="bibr">[7]</ref> demonstrates the state-of-the-art effectiveness. It hypothesizes that the first several output tokens greatly influence the alignment. Accordingly, it constrains the early tokens to avoid deviation from their initial values during fine-tuning.</p><p>We adopt the open-source implementation of SoftSFT <ref type="bibr">[7]</ref> for fine-tuning. As the public code defaults to full parameters fine-tuning, we extend it to support the QLoRA we adopted in our evaluation. All other hyper-parameters are borrowed from the paper. &#9988; The second line, similar to our method, post-processes the model after fine-tuning is performed. While several approaches exist, RESTA <ref type="bibr">[13]</ref> is a pioneering work that has demonstrated considerable effectiveness in this domain. Given an aligned model M, RESTA obtains its safetyunaligned counterpart M following the approach presented in <ref type="bibr">[54]</ref>. The delta between the weights of M and M are calculated as a safety vector, which RESTA believes establishes the alignment. Provided a target model fine-tuned from M, RESTA adds the safety vector to the model's weights to recover alignment. We consider RESTA as another baseline and run it as follows.</p><p>We perform alignment recovery using the RESTA code from <ref type="bibr">[13]</ref> on our fine-tuned models. As we will explain in Appendix &#167;B.1, the default hyper-parameters fail to derive an effective safety vector. Accordingly, we optimize the hyper-parameters by increasing RESTA's batch size from 4 to 10 and epochs from 3 to 5. We also find that while using QLoRA for the fine-tuning, RESTA only enables the linear modules of q_proj and v_proj. We enable all linear modules to incorporate more parameters in the safety vector, thus improving the alignment recovery effectiveness. The other hyper-parameters are configured as suggested <ref type="bibr">[13]</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.2.">Alignment Recovery</head><p>We present our alignment recovery results in Figure <ref type="figure">3</ref> and elaborate on the main observations below. In 9 cases 3 , our rollback is invoked. The results without rollback in those cases are presented in Table <ref type="table">18</ref> in the Appendix.</p><p>Our method can effectively recover the alignment compromised by fine-tuning. As shown in Figure <ref type="figure">3</ref>, in all the cases where fine-tuning escalates the harmful rate, our 3. The 9 cases include:</p><p>&#8226; Gemma 2B on SQL with moderated harmful prompts &#8226; Gemma 2B on NL2BASH with 0.1k harmful prompts &#8226; Gemma 2B on TOXIC with 0/0.1k/1.5k/filtered harmful prompts &#8226; LLAMA2 7B on CHEAT with 0.1k harmful prompts &#8226; LLAMA2 13B on NL2BASH with 0/0.5k harmful prompts TABLE 5: Impact of our alignment recovery on task performance (%). FT represents the results after fine-tuning, and Rec stands for the results after our alignment recovery. In the table, 0, 0.1k, 0.5k and 1.5k represent the scenarios where we injected the corresponding number of harmful questions into fine-tuning, and Mod. means we only injected the 392 harmful questions left over from moderation into fine-tuning.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Dataset</head><p>Gemma 2B LLAMA2 7B LLAMA2 13B Mistral 7B Qwen 7B 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. SQL FT 80.9 79.2 80.9 80.8 81.5 78.9 79.3 80.1 80.0 78.9 82.3 80.7 82.0 82.9 81.7 81.4 83.0 83.1 81.7 81.6 79.7 80.6 81.5 82.4 80.3 Rec 79.2 76.0 77.7 78.1 80.5 77.6 78.6 76.5 76.8 78.6 81.5 78.7 81.8 80.4 81.3 80.6 79.6 79.8 80.6 79.6 79.2 79.8 82.6 82.9 78.7 CHEAT FT 96.4 98.0 97.6 96.2 97.9 89.8 88.8 90.3 94.3 85.6 98.6 97.0 97.7 98.3 97.0 97.4 96.4 97.4 97.2 97.3 96.7 98.1 97.8 97.9 97.6 Rec 93.2 94.1 94.6 92.1 94.2 88.7 86.8 88.4 90.3 86.5 97.1 95.0 96.7 94.5 95.9 94.9 94.0 97.4 95.3 97.2 98.0 94.1 95.1 93.5 97.7 NL2BASH FT 36.4 36.1 36.9 38.3 37.9 34.7 34.7 36.1 33.1 32.3 36.4 37.5 37.7 36.8 35.4 40.8 41.1 41.8 41.6 40.0 38.8 39.3 39.6 38.1 38.8 Rec 35.2 36.4 35.1 36.5 36.1 33.3 33.3 34.5 31.7 31.6 35.4 36.3 37.0 35.1 35.5 39.4 39.8 40.7 39.7 38.5 37.6 38.1 38.3 36.5 37.0 SAMSum FT 49.9 49.9 50.0 50.0 50.1 50.5 50.8 50.2 50.7 50.8 52.8 53.5 53.4 52.8 53.3 54.6 54.5 54.6 54.5 54.3 52.4 52.6 52.4 52.8 53.2 Rec 48.0 47.6 47.8 47.8 47.6 48.9 49.4 49.7 50.1 50.6 50.5 51.2 50.9 51.5 52.6 52.0 52.3 52.2 52.2 51.7 52.8 52.0 51.7 51.5 51.5 TOXIC FT 78.7 75.1 40.4 46.1 74.4 75.1 74.5 68.3 57.9 65.5 78.6 76.2 72.9 62.4 77.8 84.0 80.6 76.3 76.8 79.6 79.3 81.6 68.9 73.7 76.1 Rec 76.3 76.0 50.0 59.1 73.2 72.0 71.3 65.0 57.4 62.3 74.9 73.9 75.7 69.0 75.3 81.8 78.0 74.3 76.5 76.5 78.9 79.4 78.1 73.4 72.4</p><p>recovery can bring the harmful rate back to the original level. Further as reported in Table <ref type="table">14</ref> in Appendix, the cosine similarity between the harmful direction from our recovered model and the harmful direction from the original model is 0.99 consistently. This demonstrates the effectiveness of our method. In several cases with the Gemma 2B and Mistral 7B models, our method even reduces the harmful rate to below the original level. Our approach not only recovers the harmful direction but also unintentionally brings the aligned direction closer to the harmful direction (refusal direction) of the original aligned model. This results in the rejection of some harmful prompts that are mistakenly accepted as benign by the original aligned model, thus enhancing the alignment. This can be demonstrated by measuring the cosine similarity between the aligned and harmful directions, which is 0.8114 for the original model and 0.9674 for our recovered model on Gemma 2B. The more detailed data are in Appendix Table <ref type="table">13</ref>.</p><p>As shown in Table <ref type="table">18</ref>, the rollback mechanism hardly affects the alignment recovery results. With or without rollback, our method can constantly bring the harmful rate to a very low level. This is intended as the rollback is incorporated to adjust the task performance. This also illustrates that our recovery method can mitigate the loss of alignment in both Scenario I and Scenario II.</p><p>The difficulty of recovery increases with the alignment strength in the original model. The extent of our recovery varies across different models. On Gemma 2B, Mistral 7B, and Qwen 7B, we fully recover (or even improve) the alignment. Yet, on LLAMA2 7B and LLAMA2 13B, we can substantially approximate the original alignment but never reach it in several cases. Fundamentally, this is because LLAMA2 7B and LLAMA2 13B have perfect alignment before fine-tuning, presenting a zero harmful rate. Recovering such alignment requires more accurate and complete identification of alignment-related weights, which is harder to achieve.</p><p>Our method performs better on generation tasks. Against fine-tuning for generation tasks (SQL, SAMSum, and NL2BASH), we can consistently recover the full alignment, regardless of the target model and settings. However, our method is less performant when applied to fine-tuning for classification tasks (TOXIC and CHEAT). Under several settings, we didn't bring the harmful rate to the level before fine-tuning. This performance discrepancy arises because the task performance of classification tasks is more susceptible to disruption from parameter changes, hindering our approach from executing additional optimization steps.</p><p>Results on the latest models. We also apply our approach to the latest models for alignment recovery: Llama3.1 8B <ref type="bibr">[55]</ref>, Llama3.2 3B <ref type="bibr">[55]</ref>, and Qwen2.5 32B <ref type="bibr">[56]</ref>. The results show that our method can successfully recover their alignment to the before-fine-tuning level, demonstrating its generalizability. The detailed results are reported in Table <ref type="table">17</ref> in the Appendix.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.3.">Task Performance</head><p>Our method resets certain weights optimized by finetuning, which, by intuition, may hurt the task performance. Accordingly, we run a measurement after our alignment recovery and present the results in Table <ref type="table">5</ref>.</p><p>Our approach maintains the task performance. On average, we only incur a 2.9% decrease in task performance. In 30 of the 125 cases, the performance drop is below 1%. In only 15 cases, the performance drop is above 4.5% (getting close to our safety fuse of 5%). These demonstrate that our method, when achieving satisfying alignment recovery, can largely preserve the task performance of the target model.</p><p>Given models fine-tuned on the TOXIC dataset, our recovery sometimes increases the task performance, especially when more harmful prompts are injected into the fine-tuning process. The TOXIC task aims to detect whether the given input is toxic or not, which depends on LLM alignment. When the alignment is broken, LLMs barely identify the toxic prompts, resulting in lower task performance as shown in Table <ref type="table">5</ref>. Therefore, when we recover the alignment of the models, some models also achieve better performance such as the Gemma 2B model trained with 500 harmful data.</p><p>Our rollback mechanism helps. Our rollback mechanism described in &#167;5.2.3 is activated in 9 cases. We reevaluate the cases without rollback and present the results in Table <ref type="table">18</ref> in the Appendix. Restricted by our cutoff at a 5% performance drop, our rollback does not render a significant boost in task performance. Yet, it still helps. Without our rollback mechanism, the performance drop is often higher than 3% and can reach 4%+. With rollback, the performance drop decreases to about 2.5% or even below 2%. These results show that our method is friendly to task performance while it additionally benefits Scenario II throughout the rollback mechanism.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.4.">Comparing with Baselines</head><p>Similar to our method, the two baselines, RESTA and SoftSFT, also aim for a good balance between alignment and utility. To present a systematic comparison, we measure the trade-off between harmful rate and task performance offered by different methods, and visualize the results in Figure <ref type="figure">4</ref>.</p><p>Our method achieves a better trade-off and greater generalization than both baselines. As shown in Figure <ref type="figure">4</ref>, the results of our method cluster around the right-upper corner, while the baselines scatter further into the bottom and left areas. This demonstrates a better trade-off offered by our method. In nearly all cases, our method reduces the harmful rate to the level before fine-tuning and maintains the task performance consistent with the fine-tuned model. In contrast, SoftSFT preserves the task performance while failing to offset the harmful rate in some models, given any fine-tuning tasks. SoftSFT simply constrains the value of the first few tokens, which is insufficient to maintain the alignment, especially for LLMs whose original alignment is not well like Mistral 7B. RESTA produces harmful rates more comparable to our method. Yet, it incurs tremendous impacts on the task performance, given CHEAT, NL2BASH, and TOXIC as the fine-tuning tasks. The reason is that RESTA treats all parameters as the safety vector and adds this safety vector to the weights of fine-tuned models. This modifies too many parameters of the fine-tuned model, which inevitably hurts the task performance.</p><p>Comparing with Additional Baselines. One potential method to recover alignment is to use collected harmful questions along with refusal answers as refusal samples to fine-tune the model. We consider two settings: (1) refusal samples come from the same distribution as harmful samples (in the task data); (2) refusal samples come from a different distribution. As shown in Table <ref type="table">16</ref> in the Appendix, using refusal samples for fine-tuning can reduce the harmful rates to some extent but is not as effective as our technique.</p><p>We also consider directly steer the activations during model inference based on the harmful directions. Specifically, we calculate the harmful directions, ! and ! f , by feeding harmful prompts to the original model and the finetuned model, and derive the steering vector V = !&#8658;! f . We then conduct the following two experiments. In experiment I, we add V to the activations with a coefficient of 1. The harmful rate decreases from 56.4% to 20.1%, but the task performance drops from 94.3 to 86.6. In experiment II, we decompose V into two components (one parallel to ! aligned and one perpendicular to ! aligned ), and use the perpendicular one as the new steering vector. With coefficients of 1 and 2, the harmful rate decreases to 22.7% and 3.4%, while the task performance drops to 86.3 and 74.3.</p><p>Additionally, we apply L 1 /L 2 penalties to model weights instead of selecting weights to update as in our algorithm for re-alignment. However, such methods have two drawbacks: (1) Updating all parameters can overfit the recovery data, reducing the generalization of alignment recovery. For example, when recovering Llama2 13B on the CHEAT dataset, the harmful rate saturates at 13% even after we run 60 recovery epochs; (2) The task performance can remain sensitive to L 1 /L 2 penalties. For instance, when recovering Llama2 7B on the TOXIC dataset, the task performance drops from 65.5% to 48.0% within 10 recovery rounds (despite harmful rate remaining at 8.3%).</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.5.">Impacts of Hyper-parameters</head><p>Our method involves several hyper-parameters, among which the direction layer L dir and the recovery dataset D rec are more critical. We extend an evaluation to understand the impacts they can bring. In the evaluation, we focus on the models fine-tuned with 1,500 harmful samples injected, considering that their alignment is more severely compromised and represents the most challenging situation.</p><p>(a) Gemma 2B (b) LLAMA2 7B (c) LLAMA2 13B (d) Mistral 7B (e) Qwen 7B</p><p>Figure <ref type="figure">5</ref>: The alignment recovery results and the time cost of our method when using a direction layer at various positions.</p><p>2 6 , 3 6 , 4 6 , and 5 6 on the x-axis respectively represent that we pick the direction layer from the 2 6 , 3 6 , 4 6 , and 5 6 position of the hidden layers. We omitted the value of time cost for Qwen 7B on the CHEAT task at the 3  6 position, as it activates rollback and disproportionately increases the time expenditure. 6.5.1. Direction Layer L dir . To inspect the influence of the direction layer, we select it from different locations, including the 2 6 , 3 6 , 4 6 , and 5 6 position of the hidden layers. We re-do the alignment recovery using each option and measure the harmful rate separately. As explained in &#167;5.2.1, different direction layers can incur varying time costs. Thus, we also measure the time consumption in each experiment. All results are presented in Figure <ref type="figure">5</ref>.</p><p>The direction layer represents a trade-off between alignment recovery and time cost. A clear trend shown in Figure <ref type="figure">5</ref> is that moving the direction layer toward the end decreases the harmful rate, given any model and any fine-tuning task. This result is expected as deeper layers in the model's architecture tend to better represent directions <ref type="bibr">[16]</ref>. On the other hand, choosing a deeper layer leads to a larger space for weight identification and restoration, proportionally increasing the time costs. Observing that a direction layer deeper than the 4  6 position only improves the alignment marginally while intensifying the time cost, we set the direction layer at the 4  6 position by default. 6.5.2. Recovery Dataset D rec . We vary the three key properties, diversity, size, and distribution, of the recovery dataset and assess their influences.</p><p>&#9987; Diversity. Our default recovery dataset, extracted from BeaverTails, includes 256 samples from 14 categories. We respectively reduce the category number to 2, 4, and 8 while maintaining the total number of samples at 256. The results show that the difference in harmful rate caused by the number of data categories is mostly less than 2%. This indicates that our algorithm is not sensitive to the diversity of the recovery dataset. Detailed results are in Appendix &#167;A.1.</p><p>&#9988; Size. In this evaluation, we keep 14 categories in the recovery dataset but reduce the number from 256 to 64 and 16. Our experiments show that the recovery dataset size has marginal impacts on our algorithm. This is because the harmful directions are more constant and less dependent on the number of samples. Details are in Appendix &#167;A.1.</p><p>&#9742; Distribution. To vary the distributions, we use two other popular datasets consisting of harmful questions, CATQA <ref type="bibr">[13]</ref> and HEx-PHI <ref type="bibr">[6]</ref>, as the recovery dataset. More details of the two datasets and how we use them for evaluations are described in Appendix &#167;B.2. The results show our method is robust with different recovery datasets. Please see Appendix &#167;A.1. 6.5.3. Impact of P and R. We study the impact of the recovery rate (P %) on our approach using the CHEAT dataset. We evaluate two settings: P = 0.1% and P = 0.4%, where the default parameter is P = 0.2%. As shown in Table 10 in the Appendix, a larger P achieves better alignment but worse task performance, since it resets more weights to the aligned model. We also evaluation two settings for hyperparameter R: 10% and 40% (default R = 20%). As summarized in Table <ref type="table">19</ref> in the Appendix, a smaller R achieves better alignment but worse task performance, as it rolls back fewer task parameters.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.6.">Impact of Evaluation Dataset</head><p>By default, we use a subset of BeaverTails to measure the alignment recovery of our method. Here, we adopt two other datasets, HEx-PHI <ref type="bibr">[6]</ref> and CATQA <ref type="bibr">[13]</ref>. The study shows that our method achieves consistent alignment recovery, regardless of the alignment evaluation dataset. Details can be found in Appendix &#167;A.2.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="6.7.">Efficiency</head><p>We also assess the efficiency of our method. All our evaluations are performed on machines with AMD EPYC 7513 32-Core Processor, NVIDIA A100 (80 GB GPU memory), and 256GB RAM. Table <ref type="table">9</ref> in the Appendix shows the results. In summary, larger models take longer to recover than smaller models. Since our technique is a post-processing approach, it is a one-time effort to obtain an aligned model. In addition, the time cost can be reduced by running our algorithm fully on a GPU. Table <ref type="table">15</ref> in the Appendix reports the cost for different model sizes. The results show a large (around half) reduction on time cost by leveraging a GPU for top P % weights identification used in our algorithm.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="7.">Extended Evaluation for Scenario I 7.1. Experimental Setup</head><p>In our target Scenario I, the Fine-tuner is assumed to be ill-intended. It does not have to inject harmful samples into a regular dataset. Instead, it can simply fine-tune the target model with purely harmful samples to quickly compromise the alignment. We extend another evaluation from this angle. We randomly select 100 harmful samples from the BeaverTails dataset and follow &#167;3.1 to fine-tune the 5 target models. We use 100 samples instead of more to align with the literature <ref type="bibr">[6]</ref>, <ref type="bibr">[57]</ref>. More importantly, as we will explain with Table <ref type="table">6</ref>, 100 samples can already compromise the alignment without sacrificing the model utility much. We also follow &#167;6.1 to recover the fine-tuned models.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="7.2.">Results</head><p>We evaluate the alignment and the model performance before and after our recovery is performed. For alignment, we use the metric of harmful rate, measured with both the BeaverTails and HEx-PHI datasets. For model performance, we can no longer use the metric of task performance as there are no downstream tasks. Instead, we adopt the utility performance introduced by Yang et. al. <ref type="bibr">[57]</ref>, which aims to assess the generic capabilities of LLM models. Specifically, we measure the utility performance on 5 representative benchmark datasets, including PIQA <ref type="bibr">[58]</ref>, GSM8K <ref type="bibr">[59]</ref>, TriviaQA <ref type="bibr">[60]</ref>, HumanEval <ref type="bibr">[61]</ref>, and MMLU <ref type="bibr">[62]</ref>.</p><p>The 100 samples compromise the alignment. As summarized in Table <ref type="table">6</ref>, fine-tuning with the 100 harmful samples substantially removes the alignment in the target model. It increases the average harmful rate from nearly 0 to 69.34% on HEx-PHI, meaning that the target model answers almost two-thirds of the harmful questions from this dataset. On BeaverTails, the average harmful rate is also increased to 41.8%. Moreover, the fine-tuning maintains the utility performance of the target model on all benchmark datasets, indicating that the target model still functions normally.</p><p>Our method can recover the alignment without sacrificing the model performance. Table <ref type="table">6</ref> also shows that our method can recover the alignment in the fine-tuned model. The average harmful rate of the recovered models is only 1.26% on HEx-PHI and 0.92% on BeaverTails, presenting alignment similar to the original models. On the other hand, the utility performance of the recovered models is almost identical to the original models. This indicates that our method well maintains the model performance.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="8.">Related Work</head><p>LLM Fine-Tuning Attacks. Fine-tuning LLMs is an effective and popular way to enhance their abilities on various downstream tasks, especially on domain-specific datasets. However, recent works <ref type="bibr">[6]</ref>, <ref type="bibr">[54]</ref>, <ref type="bibr">[57]</ref> have pointed out that fine-tuning LLMs with a few harmful question-answer pairs, even with benign datasets can damage the alignment of LLMs, called fine-tuning attacks.</p><p>Yang et. al. <ref type="bibr">[57]</ref> and Bhardwaj et. al. <ref type="bibr">[54]</ref> show that finetuning the LLMs with a few harmful question-answer pairs can totally break the alignment of LLMs without sacrificing their utilities. Qi et. al. <ref type="bibr">[6]</ref> point out that merely finetuning with benign and widely employed datasets can also inadvertently compromise the safety alignment of LLMs. Defence against LLM fine-tuning attack. To tackle the fine-tuning attack against LLMs, various approaches have been proposed, which can be grouped into model-enhanced, fine-tuning based, and post-alignment according to the stage involved in the fine-tuning procedures.</p><p>Model-enhanced mechanisms try to build a more robust LLM that is resilient against fine-tuning attacks. Huang et. al. <ref type="bibr">[63]</ref> propose vaccine, which produces invariant hidden embeddings by progressively adding crafted perturbation to them in the alignment phase. Inspired by the shallow safe alignment, Rosati et. al. <ref type="bibr">[64]</ref> remove information about harmful representations across all layers such that it is difficult to recover them during fine-tuning. These methods, aiming at a more robust model, are orthogonal to ours.</p><p>Fine-tuning based methods adjust and customize the fine-tuning process so that the fine-tuned LLMs are immune to harmful data <ref type="bibr">[8]</ref>, <ref type="bibr">[11]</ref>, <ref type="bibr">[12]</ref>. Lyu et. al. <ref type="bibr">[8]</ref> uncovers that the prompt templates used during fine-tuning and inference play a crucial role in preserving safety alignment. Thus, they fine-tune models without a safety prompt but include it at test time. Wang et. al. <ref type="bibr">[9]</ref> create a link between the secret prompt and safe responses by including safety responses with a secret trigger during fine-tuning. During inference, this secret prompt will allow the model to generate safe answers for harmful queries. Zhou et. al. <ref type="bibr">[10]</ref> prevent the LLMs from learning harmful behavior by activating the security vectors, which are parameters learned on harmful behavior. During inference, deactivating the security vectors restores the LLM's normal behavior. Qi et. al. <ref type="bibr">[7]</ref> observed that the first few output tokens determine the generative distribution of the LLMs. So they propose SoftSFT, which protects the initial tokens by designing a token-wise constrained objective during the supervised fine-tuning process. Huang et. al. <ref type="bibr">[12]</ref> introduce an alignment dataset into the user finetuning stage and apply the bi-state optimization to train the model.</p><p>Post-alignment approaches realign the compromised model without the knowledge of data and fine-tuning process. Inspired by task arithmetic <ref type="bibr">[65]</ref>, Bhardwaj et. al. <ref type="bibr">[13]</ref> propose RESTA, which restore the model's alignment by adding a safety vector to the weights of the compromised model. Specifically, the safety vector is the newly learned parameters used to remove the alignment by harmful question pairs. Hsuet. al. <ref type="bibr">[11]</ref> propose Safe LoRA, which projects the LoRA weights from selected layers to the safety-aligned subspace.Huang <ref type="bibr">[66]</ref> aim to recover the alignment by pruning the harmful parameters.</p><p>Unlike weight-space methods like RESTA simply merge delta parameters with the target model, our method disentangles the harmful direction from the main task and uses the extracted direction to guide alignment recovery. By minimizing modification in fine-tuned LLMs, our approach can better maintain task performance as shown in Figure <ref type="figure">4</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="9.">Conclusion</head><p>The literature and this paper have shown that the alignment of LLMs can be unexpectedly compromised when fine-tuning an LLM on a downstream task. We propose an alignment recovery technique for fine-tuned models. Our approach features an iterative weight parameter restoration procedure that copies a small subset of weights from the original aligned model to the fine-tuned model. We also implement a rollback process based on its impact on downstream task performance. Experimental results show that our method effectively recovers alignment and achieves a better balance between alignment performance and task performance, outperforming state-of-the-art techniques. (a) Gemma 2B (b) LLAMA2 7B (c) LLAMA2 13B (d) Mistral 7B (e) Qwen 7B Figure 7: The harmful rate of recovered models when using different recovery datasets (HEx-PHI, CATQA, and BeaverTails, annotated as BT). The dashed line denotes the harmful rate of the original model before fine-tuning. TABLE 7: The harmful rate of fine-tuned models (Fine-tuned) and recovered models (Recovered) using different datasets, including BeaverTails (BT), HEx-PHI (HEx), and CATQA (CAT) for alignment evaluation. Original represents the original model before fine-tuning. Gemma 2B LLAMA2 7B LLAMA2 13B Mistral 7B Qwen 7B Dataset Fine-tuned Recovered Fine-tuned Recovered Fine-tuned Recovered Fine-tuned Recovered Fine-tuned Recovered BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT BT HEx CAT Original 4.6 2.7 7.5 4.6 2.7 7.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 11.7 35.5 35.5 11.7 35.5 35.5 2.4 11.5 6.9 2.4 11.5 6.9 SQL 62.0 79.1 92.5 1.6 0.0 0.2 59.6 94.5 96.0 0.1 0.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="10.">Acknowledgment</head><p>3 0.0 57.7 94.2 95.1 0.9 0.3 0.6 54.7 94.5 92.5 2.0 9.7 4.4 52.9 89.4 84.2 0.3 0.6 0.6 CHEAT 64.0 76.7 94.4 1.1 0.0 0.9 56.4 91.2 92.0 0.1 0.3 0.2 56.4 92.1 94.2 6.7 2.1 5.6 52.9 95.5 92.4 6.4 21.2 18.2 51.3 86.7 88.4 1.0 3.6 2.4 NL2BASH 63.7 74.8 95.5 1.9 0.6 0.9 62.0 92.4 92.4 0.9 0.6 0.9 61.9 95.5 96.7 1.0 3.6 0.6 56.4 95.2 96.2 5.0 20.3 14.2 52.4 92.1 92.0 1.3 3.3 3.6 SAMSum 62.9 70.0 91.1 1.1 0.0 0.2 40.6 62.1 68.0 0.3 0.3 0.0 57.1 93.9 94.7 0.4 1.2 0.2 54.6 96.7 95.5 6.4 23.3 17.4 52.6 89.7 94.5 0.7 1.2 2.4 TOXIC 60.7 71.2 89.3 4.1 2.1 11.4 58.9 94.2 95.6 4.0 0.9 2.7 57.6 95.5 97.5 1.4 0.6 0.7 56.4 96.1 95.1 14.6 47.3 36.7 54.1 92.7 87.8 2.1 4.2 6.0</p><p>while maintaining the total number of samples at 256. We re-perform the evaluation for each setting and show the alignment recovery results in the upper half of Figure <ref type="figure">6</ref>.</p><p>Our algorithm is not sensitive to the diversity of the recovery dataset. Given Gemma 2B, LLAMA2 7B, LLAMA2 13B, and Qwen 7B, the difference in harmful rate caused by the number of data categories is less than 2%. On the Mistral 7B, the variation is slightly higher. Yet, more categories do not necessarily lead to a lower harmful rate. This is not surprising as we discussed in &#167;5.2.1. With a direction layer toward the end, the hidden states become more constant to simply indicate the prompt is harmful, presenting lower sensitivity to the data diversity. To validate the reasoning, we compare the harmful directions obtained with varying categories in the recovery dataset and show the results in Table <ref type="table">11</ref>. The results are indeed highly similar. &#9988; Size. In this evaluation, we keep 14 categories in the recovery dataset but reduce the number from 256 to 64 and 16. We show the results in the lower half of Figure <ref type="figure">6</ref>.</p><p>The recovery dataset size has marginal impacts on our algorithm. Similar to diversity, the size of the recovery dataset incurs limited differences in harmful rate. The reason is also similar. The harmful directions are more constant and less dependent on the number of samples, as shown by the comparison results in Table <ref type="table">12</ref>. &#9742; Distribution. To vary the distributions, we use two other popular datasets consisting of harmful questions, CATQA <ref type="bibr">[13]</ref> and HEx-PHI <ref type="bibr">[6]</ref>, as the recovery dataset. More details of the two datasets and how we use them for evaluations are described in Appendix &#167;B.2. We summarize the comparison results in Figure <ref type="figure">7</ref>.</p><p>Our method is robust with different recovery datasets. Using CATQA and HEx-PHI as the recovery dataset brings no major differences to the recovery results compared to <ref type="bibr">TABLE 8:</ref> The harmful rate of fine-tuned models (FT) and recovered models applying RESTA approach with original parameters (Orig) and optimized parameters (Optim). Fine-tuned models (FT) are trained with 0, 0.1k, 0.5k, and 1.5k harmful data mixed into the task dataset. Mod. indicates the remaining 392 samples after moderation.  <ref type="table">9</ref>: The time cost (minutes) for our approach to recover the fine-tuned models. Fine-tuned models (FT) are trained with 0, 0.1k, 0.5k, and 1.5k harmful data mixed into the task dataset. Mod. means the 392 samples left over from moderation are used.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Dataset</head><p>Gemma 2B LLAMA2 7B LLAMA2 13B Mistral 7B Qwen 7B 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. 0 0.1k 0.5k 1.5k Mod. SQL 12.9 10.2 15.4 19.9 23.8 64.1 64.2 80.3 75.8 87.2 158.4 170.9 134.5 177.2 160.4 51.9 65.1 64.7 59.3 64.9 55.2 54.3 50.4 51.8 52.0 CHEAT 17.7 17.9 18.9 20.6 15.8 84.5 86.3 82.1 76.9 65.8 156.0 96.7 77.7 173.9 108.6 65.7 52.2 65.9 59.5 63.1 57.3 57.7 53.2 53.7 60.5 NL2BASH 11.1 26.9 17.6 24.9 17.9 49.3 64.3 46.1 76.7 61.8 300.3 85.4 236.6 173.8 109.1 53.3 50.9 51.5 58.4 62.0 68.3 31.4 29.5 53.8 41.5 SAMSum 18.3 18.7 18.8 20.0 17.8 84.2 84.3 85.0 81.0 79.3 178.9 146.4 142.0 178.2 146.6 72.1 57.0 58.3 64.8 59.7 79.3 58.7 55.1 57.0 55.5 TOXIC 40.4 41.9 23.4 32.1 40.9 82.6 82.9 88.3 82.6 86.1 235.3 131.8 190.6 223.3 219.2 92.9 94.0 93.8 89.0 87.7 81.0 82.3 73.9 74.4 80.9 BeaverTails. Our method consistently reduces the harmful rate close to or even below the level before fine-tuning.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>A.2. Impact of Evaluation Dataset</head><p>In the evaluation above, the alignment recovery of our method is measured with a testing subset from BeaverTails. We adapt the evaluation to measure the alignment recovery with all harmful samples from two other datasets, HEx-PHI and CATQA (which are introduced above). In the adapted evaluations, we use the default recovery dataset from BeaverTails to avoid overlap with the alignment evaluation dataset. The evaluation results are presented in Table <ref type="table">7</ref>.</p><p>Our method achieves consistent alignment recovery, regardless of the alignment evaluation dataset. As shown in Table <ref type="table">7</ref>, our method can reduce the harmful rate in most cases close to or below the level before fine-tuning. The only exception is the Mistral 7B model fine-tuned on the TOXIC dataset and evaluated using the HEx-PHI dataset. The harmful rate, compared to the model before fine-tuning, increased by nearly 12%. Our method also presents consistent effectiveness when measured with different evaluation datasets. It does not perform significantly better or worse with a specific evaluation dataset, indicating that our method is not overfitting the evaluation datasets.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Appendix B. Experimental Setup</head></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B.1. RESTA Setting</head><p>For RESTA, they consider a less severe threat model in which the alignment of LLMs is only slightly compromised by fine-tuning with benign datasets. However, in our experiments, the alignment of fine-tuned models is significantly disrupted. Under these conditions, their provided hyperparameters are insufficient to restore alignment, as demonstrated in Table <ref type="table">8</ref>. To address this, we strengthen the safety vector to more effectively disrupt alignment. Specifically, we increase the batch size from 4 to 10 and the number of epochs from 3 to 5. Additionally, To include more parameters in the safety vector, thus improving the alignment recovery capability, we enable QLoRA to finetune all linear modules, than just the q_proj and v_proj modules.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B.2. Recovery Dataset</head><p>CATQA <ref type="bibr">[13]</ref> combines prohibited use cases outlined in OpenAI's usage policies and Meta's Llama2 acceptable use policy to build a comprehensive range of harmful categories. In detail, this dataset contains 11 main harmful categories. For each category, they construct 50 harmful questions, a total of 550 harmful prompts.</p><p>HEx-PHI <ref type="bibr">[6]</ref> also uses the prohibited use cases from Meta and OpenAI's policies. It contains 30 harmful instruction examples for each of the 11 prohibited categories from various sources, including Anthropic Red Teaming Data <ref type="bibr">[67]</ref> and AdvBench <ref type="bibr">[68]</ref>. These examples were further refined and categorized by human annotators and AI models like Claude, and jailbroken versions of GPT-3.5 and Llama 2.</p><p>Although these two datasets contain harmful prompts, LLMs do not recognize them as such. As shown in Table 7, LLMs tend to exhibit a higher harmful rate on these datasets, particularly Mistral 7B, which shows a harmful rate of 35.45%. Thus, we first filtered out samples that were</p></div></body>
		</text>
</TEI>
