Skip to content

Rethinking Safety in LLM Fine-tuning: An Optimization PerspectiveRethinking Safety in LLM Fine-tuning: An Optimization Perspective

arXiv· arXiv:2508.12531· Minseon Kim, Jin Myung Kwak, Lama Alssum, Bernard Ghanem, Philip Torr, David Krueger, Fazl Barez, Adel BibiMicrosoft Research; KAIST; KAUST; University of Oxford; Université de Montréal; Mila; WhiteBoxIndexed by this site · 2026-10-14· Originally publishedDefenses

Quick read

Defenses

Based on the paper PDF (AI-generated); refer to the original

The authors use parameter EMA on Llama-2-Chat-7B with Dolly to reduce keyword ASR from 15.96% to 2.70%.

Questions
Custom fine-tuning is often thought to weaken refusal in aligned models, even with harmless data. The authors want to test whether this mainly comes from optimization choices such as learning rate and batch size, and hope to preserve safety and task performance without adding extra safety data.
Methods
On benign instruction data, they tune learning rate, batch size, gradient accumulation, and epochs, and apply an exponential moving average over parameter updates, mixing historical parameters with current parameters according to a momentum coefficient, keeping the trajectory closer to the pretrained weights.
Experiments and results
Table 2: Llama-2-Chat-7B keyword ASR on Dolly, FT 15.96%, Best tuned 4.62%, EMA 2.70%; Alpaca EMA 3.08%; AoA FT 45.77%, EMA 0.0.
Limitations and future work
The paper does not have a dedicated section on limitations or future work. Experiments name four open-source models, with main comparisons mostly on Llama-2-Chat-7B; no repetition counts are reported, nor sample sizes for several safety and utility benchmarks.
Experimental setupLlama-2-Chat-7B、Llama-3.2-1B-Instruct et al. · Dolly、Alpaca et al. · ASR(keyword matching)、LLM Judge score et al.
Evaluated models
Llama-2-Chat-7BLlama-3.2-1B-InstructQwen2.5-3B-InstructPhi-3-3B
Benchmarks
DollyAlpacaORCAAbsolutely Obedient Agent (AoA)harmful behaviorsAdvBenchSafetyInstructRealToxicityPromptsWildJailbreakMT-BenchTruthfulQA
Metrics
ASR(keyword matching)LLM Judge scoreMT-Bench utilityTruthfulQACKA
AI summaryAn arXiv paper proposes that the safety degradation after LLM fine-tuning mostly stems from optimization choices such as learning rate, batch size, and number of gradient steps, rather than an inevitable trade-off. Through systematic testing, the authors claim that selecting these hyperparameters well can reduce unsafe responses to adversarial prompts from 16% to about 5% (measured by keyword matching) while maintaining utility. The paper further proposes using exponential moving average (EMA) momentum over the parameter space to provide a more stable optimization path for fine-tuning, preserving the safety properties of the pretrained model. Experiments on Llama series models with Dolly, Alpaca, and ORCA datasets show that most safety degradation can be avoided without additional safety data, outperforming existing methods that require supplementary safety data.

An arXiv paper proposes that the safety degradation after LLM fine-tuning mostly stems from optimization choices such as learning rate, batch size, and number of gradient steps, rather than an inevitable trade-off. Through systematic testing, the authors claim that selecting these hyperparameters well can reduce unsafe responses to adversarial prompts from 16% to about 5% (measured by keyword matching) while maintaining utility. The paper further proposes using exponential moving average (EMA) momentum over the parameter space to provide a more stable optimization path for fine-tuning, preserving the safety properties of the pretrained model. Experiments on Llama series models with Dolly, Alpaca, and ORCA datasets show that most safety degradation can be avoided without additional safety data, outperforming existing methods that require supplementary safety data.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors believe that safety degradation from benign data fine-tuning often comes from optimization choices rather than an inevitable trade-off.

    After supervised fine-tuning on benign data, does an already-aligned model necessarily give harmful responses to harmful requests.

    • Scenario: The authors state that fine-tuning on proprietary data can improve performance on specific tasks, but Qi et al. (2024) and others point out that even seemingly harmless data can weaken safety alignment, causing the model to stop refusing harmful requests. Figure 1a is a schematic of this process, not a measurement.
    • Existing approaches: The authors state that prior work mostly performs safety alignment at the pretraining stage; during fine-tuning, they rely on filtering harmful data, adding safety data, or parameter-level interventions. There is limited discussion of how learning rate, batch size, and gradient accumulation affect safety.
    • Authors' judgment: The authors believe the problem often stems from parameter deviation due to suboptimal optimization, not an inevitable trade-off of benign fine-tuning. The abstract states that unsafe responses under keyword matching drop from 16% to about 5%; §3.2 states from 15.96% to 4.62%.
    • This paper's approach: Examine these hyperparameters on instruction-tuned open-source LLMs and propose exponential moving average (EMA) in parameter space, using smoother updates to preserve safety properties from pretraining without adding safety data. Data include Dolly, Alpaca, ORCA, and AoA, which the authors call identity-shift attack data.
    • Evaluation criteria: The tuned models are open-source models already trained to refuse harmful requests. D_user contains only benign samples. Safety is measured on a separate harmful prompt set, computing attack success rate (ASR) via keyword matching and LLM evaluation; utility mainly looks at instruction following.
  2. What related research exists?

    Related work covers safety alignment, continual learning, and optimization; the authors state that optimization strategies are still understudied.

    Safety Alignment and Fine-Tuning Risks

    • Alignment during pretraining: Dai et al. (2023), Ji et al. (2024) use reinforcement learning from human feedback to align preferences and safety constraints. Achiam et al. (2023)'s GPT-4 combines safety-oriented RLHF with rule-based rewards; Haider et al. (2024)'s Phi-3 uses a "break-fix" loop for data curation, safety post-training, benchmarking, red teaming, and vulnerability identification.
    • Fine-tuning can break alignment: Qi et al. (2024) point out that a small number of adversarially designed training samples, as well as common benign data, can break safety alignment. Peng et al. (2024), Huang et al. (2024a) also report safety degradation after benign data fine-tuning; He et al. (2024), Eiras et al. (2025) discuss degradation when harmful data distributions overlap with user data, or when fine-tuning samples are close to harmful samples.
    • Existing mitigations: The authors list data filtering (Huang et al., 2024b), adding safety data (Choi et al., 2024), and parameter-level regularization (Huang et al., 2024b; Hsu et al., 2024). The authors state these works require modifying data or adding extra safety data, and optimization-based strategies remain understudied.

    Continual Learning

    • Regularization: Kirkpatrick et al. (2017), Li and Hoiem (2017) selectively preserve important weights to balance old knowledge and new tasks. The authors state such methods limit plasticity.
    • Replay: Chaudhry et al. (2019b), Hayes et al. (2019) put old task samples into a buffer and train jointly with new tasks. The authors state this is impractical in privacy-sensitive scenarios.
    • Gradient constraints: GEM (Lopez-Paz and Ranzato, 2017) requires gradient update angles not to exceed 90 degrees; A-GEM (Chaudhry et al., 2019a) uses averaged gradients for efficiency.

    Optimization, Robustness, and Parameter Momentum

    • Hyperparameters and robustness: Masters and Luschi (2018), Goyal et al. (2017), Pang et al. (2021) discuss larger batches, moderate learning rates, and more robust representations in vision models. Peng et al. (2024) discuss that aggressive parameter updates may cross the safety boundary of LLMs. The authors accordingly treat stable optimization as a condition for preserving safety.
    • EMA and merging: He et al. (2020), Tack et al. (2022), Sanyal et al. (2023) use exponential moving averages of parameters; Morales-Brotons et al. (2024), Wang et al. (2022) discuss that weight EMA can preserve knowledge without adding extra data. The authors also interpret EMA as parameter merging, citing task arithmetic (Ilharco et al., 2023), LoRA merging (Prabhakar et al., 2025), and gradient-based merging (Daheim et al., 2024) as related directions.

    Baselines and benchmarks: Experimental comparisons are against ordinary fine-tuning (FT), Best tuned after hyperparameter search, l2 regularization, uniform rehearsal, A-GEM, LoRA (Hu et al., 2022), and gradient clipping. Fine-tuning data are Dolly, Alpaca, ORCA, AoA; utility uses MT-Bench, TruthfulQA, and WildJailbreak benign prompts; safety uses harmful behaviors / AdvBench, SafetyInstruct, RealToxicityPrompts, WildJailbreak. Continual learning uses open-source safety data from Bianchi et al. (2024).

    The authors position this paper as "mitigating fine-tuning safety risks using only hyperparameters and parameter momentum without changing the training pipeline," and state this can avoid relying on data filtering or extra safety sets.

  3. How does the paper address this problem?

    Use more stable hyperparameters and apply EMA in parameter space to preserve safety behavior from pretraining.

    The authors view the degradation of refusal behavior after fine-tuning as an optimization trajectory problem: first, they adjust learning rate, batch size, gradient accumulation, and epochs to a more stable side, then use exponential moving average (EMA) in parameter space to keep updates on a path close to the pretrained weights.

    Problem setup

    • Object: f_θ is an open-source LLM already trained on high-quality instruction data that includes safety-aligned data. The fine-tuning set D_user contains only benign samples; the goal is to learn the downstream task while maintaining the original safety standard.
    • Training objective: Minimize standard cross-entropy. The authors write θ∗=argminθ∑i=1N−log Pθ(yi|xi), i.e., increase the probability of giving the specified response to user input.
    • Utility: Represented by instruction following, scored by an LLM judge on MT-Bench relative to reference answers, based on relevance, coherence, and usefulness. Table 1 states the utility scale as 1–10.
    • Safety: A separate harmful prompt set H is used. ASR is the proportion of unsafe responses. S has two forms: keyword matching (safe if a predefined refusal word is present) and GPT-4 evaluation. §3.1 states a 5-point scale, with scores below 5 considered safe and 1 being the safest; the evaluation model in the experiments is GPT-4o-20240513.

    Hyperparameters and data

    • Quantities swept: batch size, learning rate, gradient accumulation steps, and epochs. The legend of Figure 2 gives, for the Llama-2-7B-Chat side, batch 16/64/256, learning rates 1e-5/2e-5/5e-5, gradient accumulation 1/5/10/20; for the Llama-3.2-1B side, batch 16/32, learning rates 2e-6 to 2e-8, and the same accumulation steps. The authors write that when learning is slower, more epochs are used to compensate. The paper does not list the ASR for each point in Figure 2 in the main text.
    • Mechanistic claim by the authors: The authors cite Peng et al. (2024), arguing that small batch sizes with high learning rates are more likely to pull the model out of the "safety basin," a smooth region in parameter space that still retains safety knowledge. Increasing gradient accumulation is used by the authors to smooth updates.
    • Epochs: The authors paraphrase Qi et al. (2024), stating that when the learning rate is high (example in the text: 5e-5), the number of epochs has limited effect on ASR because divergence occurs early in training. Figure 3 compares learning rates 2e-5 and 5e-5; the caption only says "Epoch," not the dataset.
    • Whether all data carry risk: Besides Dolly and Alpaca, the authors uniformly sample from 15 categories of ORCA, scale to 15K and 50K, and align with an open-source safety dataset using centered kernel alignment (CKA). Table 1 does not give the name of that safety set.

    Parameter-space EMA

    • Update: Let θ_t be the parameters at step t and θ_EMA,t the momentum parameters. The authors write θEMA,t←η·θEMA,t−1+(1−η)·θt, where η∈[0,1) controls the weight of the historical EMA. The loss is optimized with respect to θ_t, and the EMA is updated synchronously.
    • Authors' explanation: EMA prevents parameters from abruptly leaving the pretrained model, thereby preserving safety-related information. The authors also view it as parameter merging: at step t, the safety capabilities represented by θ_EMA,t-1 are combined with the current task parameters.
    • Coefficients in implementation: §4.1 states that EMA uses learning rate 2e-5, batch size 8, gradient accumulation 1 step, and fixes η at 0.1. In Table 5, the row with the same numbers as the main result in Table 2 is η=0.25 (utility 4.33, ASR 2.70), not 0.1. The paper does not explain this discrepancy.
    • Update frequency: Table 5 compares updates every 10 steps, every 5 steps, and every step. The main result corresponds to the every-step row.

    Training and judgment details

    • Default fine-tuning: The appendix states, following Qi et al. (2024), AdamW, learning rate 2e-5, batch size 8; also states per-GPU batch 8, two GPUs, step learning rate scheduler gamma 0.85, weight decay 0, epochs about 1 to 10. The paper does not consolidate "Best tuned" into a specific set of hyperparameters.
    • Keyword ASR: A response is safe if it contains a predefined refusal word; otherwise it counts toward ASR. Appendix A.3 gives the word list, not listed here.
    • LLM evaluation: Harmfulness scored 1 to 5, with 1 being refusal or steering the conversation toward policy-permitted directions, and 5 being directly satisfying the violating instruction. The appendix states GPT-4o-20240513, max tokens 2048, top-p 0.0. The evaluation templates for TruthfulQA and RealToxicityPrompts are in Figures 8 and 9. The LLM Judge column in Table 2 is a score, not a percentage.
  4. What experiments did the paper conduct?

    On Llama-2-Chat-7B, Dolly keyword ASR drops from 15.96% to 2.70% with EMA (Table 2).

    Experimental Setup

    • Tested models: §4.1 lists five open-source models, naming Llama-2-Chat-7B, Llama-3.2-1B-Instruct, Qwen2.5-3B-Instruct, Phi-3-3B, all trained to refuse harmful requests. In tables, Llama is also written as Llama 7B or Llama-2-7B-Chat. Llama-3.2-1B appears in Figure 2; the main text does not give that model's specific ASR.
    • Fine-tuning data: Dolly is 14.4K in Table 1, the appendix says about 15,000; Alpaca is 49.8K in Table 1, the appendix describes the original set as about 175,000; ORCA is sampled by 15 categories as 15K and 50K; AoA is identity-shift data, and the authors say 45.77% of results use only 10 examples. Continual learning uses open-source safety data from Bianchi et al. (2024); the paper does not give the sample size.
    • Evaluation data: the appendix says harmful behaviors (Zou et al., 2023) has 520 harmful prompts. There are also AdvBench, SafetyInstruct, RealToxicityPrompts, WildJailbreak; utility uses MT-Bench, TruthfulQA, and WildJailbreak benign prompts. Sample sizes for the latter are not reported in the paper.
    • Baselines: FT, Best tuned, CL requiring external safety data (l2, uniform rehearsal, A-GEM), LoRA, gradient clipping, EMA.
    • Metrics: keyword-matching ASR; LLM Judge is 1–5, lower is safer. §3.1 says scores below 5 are considered safe. MT-Bench utility is judged by an LLM. The judge model is GPT-4o-20240513.
    • Repetition: the paper does not report repetition counts, nor agreement between judge and human.

    Main Results

    Table 2, model is Llama-2-Chat-7B. The table below does not include CL, nor Alpaca's Best tuned.

    Swipe horizontally to read the table

    DatasetMethodMT-BenchKeyword ASRLLM Judge
    DollyFT4.4615.96%1.594
    DollyBest tuned4.894.62%1.338
    DollyEMA4.332.70%1.024
    AlpacaFT4.9818.46%1.634
    AlpacaEMA5.383.08%1.052
    AoAFT3.0345.77%3.213
    AoAEMA4.620.0%1.000
    • Authors' claims about hyperparameters: the authors say after careful hyperparameter selection, Best tuned on Alpaca achieves keyword ASR of 8.08%, and claim this reduces safety risk by 10.38% compared to FT; that row has utility 4.67 and LLM Judge 1.340. The abstract says from 16% to about 5%, §3.2 says from 15.96% to 4.62%, and the contributions list says reduced to below 5%.
    • EMA vs. CL: the authors say EMA does not use external safety data. On Dolly, EMA keyword ASR is 2.70% and utility 4.33; in the same table, CL is 4.81% and 4.30. On Dolly, EMA utility is lower than FT's 4.46 and Best tuned's 4.89; on Alpaca, EMA utility is 5.38, higher than FT's 4.98.
    • AoA: the authors say just 10 examples make FT keyword ASR reach 45.77%. In the table, Best tuned and EMA are both 0.0, Best tuned utility remains 3.03, EMA is 4.62 with LLM Judge 1.000. The immediately following text writes 2.7% and utility 4.33 into the AoA discussion, inconsistent with Table 2's AoA row; here the table is taken as authoritative.

    Multiple Benchmarks and Two Other Models

    • What was tested: Table 3 compares FT, Best tuned, A-GEM, EMA on Dolly for Llama 7B, Qwen 3B, Phi 3B. For harmful prompts, lower is better; for Benign and TruthfulQA, higher is better. The text says all these sets use an LLM as judge. For Llama on AdvBench, FT 15.96 and Best tuned 4.62 match Table 2's keyword ASR, EMA is 2.50, differing from Table 2's 2.70.
    • Results: Llama EMA's SafetyInstruct 3.9, RealToxicityPrompt 3.07, WildJailbreak 37.25, Benign 80.00, TruthfulQA 49.35; FT correspondingly 8.6, 5.66, 57.50, 80.48, 32.58. Qwen's AdvBench goes from FT 63.46 to EMA 3.08 and A-GEM 2.50. Phi's AdvBench goes from FT 49.04 to EMA 1.54, RealToxicityPrompt 0.47, TruthfulQA 62.15.
    • Authors' interpretation and exceptions: the authors say tuning and A-GEM can reduce harmful outputs, but performance on benign prompts often drops; EMA preserves utility while reducing harmful responses. Exceptions: Llama Best tuned's RealToxicityPrompt is 6.37, higher than FT's 5.66; Qwen EMA's WildJailbreak is 70.55, higher than Best tuned's 66.70; on Phi's SafetyInstruct, Best tuned is 2.0 and A-GEM 1.1, EMA is 2.6.

    Continual Learning, LoRA, and Gradient Clipping

    • Continual learning: Table 6 uses keyword matching. On Dolly, A-GEM's ASR is 0.58 and utility 3.81, uniform rehearsal is 4.81 and 4.30, l2 is 14.42 and 3.81, compared to FT's 15.96 and 4.46. On Alpaca, A-GEM is 2.31 and utility 4.16; l2's ASR remains 18.46, utility drops from 4.98 to 4.13. The authors say this is consistent with forgetting in continual learning, and that A-GEM over-prioritizes safety on Dolly, dropping utility from 4.46 to 3.81. EMA does not use external safety data.
    • LoRA and clipping: Table 4, Llama 7B. LoRA's AdvBench 3.72, Benign 73.33, TruthfulQA 42.78; gradient clipping's AdvBench 9.62, Benign 73.33, TruthfulQA 37.26; EMA is 2.50, 80.00, 49.35. The authors say smaller parameter shifts better preserve safety alignment, and that EMA's overall trade-off is better than both stabilization fine-tuning methods.

    Parameter Shift and Loss Surface

    • Gaussian noise: Figure 4 adds Gaussian noise of different scales to parameters, with the x-axis up to σ=0.006. The authors say at σ=0.004 safety knowledge disappears while utility declines more slowly; larger perturbations are needed to noticeably lower utility.
    • Merging with original parameters: Figure 5's x-axis is Merged weight, up to 0.5. The authors say with a larger weight of 0.5 merging original parameters, ASR drops to below 3%, and utility remains relatively stable.
    • Loss and distance: the authors say in Figure 6, on Llama2-Chat-7B, the loss surface for benign prompts is smoother and wider, while for harmful prompts it is narrower and steeper. In Figure 7, the authors say the distance between pretrained and fine-tuned weights is positively correlated with ASR; the paper does not give correlation coefficients. Appendix Table 7 gives response examples for FT and EMA, not repeated here.

    Other Ablations and Analyses

    • Table 5's η: FT utility 4.46, ASR 15.96; η=0.01 gives 4.00 and 10.19; η=0.25 gives 4.33 and 2.70; η=0.4 gives 3.29 and 0.0. The table does not specify the dataset; the FT row matches Table 2's Dolly FT. The authors say η=0.25 is a good trade-off, and η=0.4 hinders learning new tasks.
    • Table 5's update frequency: every 10 steps utility 3.51, ASR 12.69; every 5 steps 3.56 and 12.12; every step 4.33 and 2.70. The authors say per-step updates improve ASR by +13.26%.
    • Table 1: ORCA 15K utility 4.83, ASR 0.0; 50K utility 5.04, ASR 0.19. CKA is Dolly 0.002, Alpaca 0.005, ORCA 0.013. The authors say 0.013 is still extremely low, and distribution similarity alone is unlikely to explain safety. Dolly utility is 3.88, differing from Table 2's FT utility 4.46, with the same ASR of 15.96%; the paper does not explain this.
    • Figure 3: the authors say lowering the learning rate reduces ASR from 15.56% to 8.56%, with utility 3.9. The caption does not specify the dataset.
    • Figure 2: the authors say smaller learning rates, larger batch sizes, or more gradient accumulation steps correspond to lower ASR at similar MT-Bench utility; the text does not give coordinate values point by point.
  5. What are some points that can be further explored?

    The paper does not have a dedicated section on limitations or future directions.

    Limitations and future directions noted by the authors

    The paper does not specifically discuss limitations. The Conclusion, Discussion, and appendix do not frame any result as a limitation, shortcoming, or future work; §5 discusses the loss landscape and parameter shift, not future directions.

    Experimental coverage

    • Models: §4.1 states that five open-source models are evaluated, naming Llama-2-Chat-7B, Llama-3.2-1B-Instruct, Qwen2.5-3B-Instruct, and Phi-3-3B. Figure 2 includes a Dolly hyperparameter sweep for Llama-3.2-1B; the main comparisons in Tables 2–4 and 6 do not list numbers for that model.
    • Data: Fine-tuning uses Dolly (Table 1 shows 14.4K), Alpaca (49.8K), ORCA samples of 15K and 50K, and AoA. Safety evaluations include harmful behaviors (the appendix states 520 items), AdvBench, SafetyInstruct, RealToxicityPrompts, and WildJailbreak; utility is measured with MT-Bench, TruthfulQA, and benign prompts from WildJailbreak.
    • Comparisons: FT, Best tuned, l2 regularization, uniform rehearsal, A-GEM, LoRA, gradient clipping, and EMA (Tables 2–6). For continual learning, safety data from Bianchi et al. (2024) is used. Tables 3 and 4 state training on Dolly.
    • Judgment: Keyword matching, and GPT-4o-20240513 (the appendix states max tokens 2048, top-p 0.0). §4.1 states EMA η=0.1; in Table 5, the row with the same numbers as the main result is η=0.25.
    • Items not reported in the paper: number of repetitions, inter-rater and human agreement, the specific hyperparameter combination for Best tuned, and sample sizes for SafetyInstruct, RealToxicityPrompts, WildJailbreak, MT-Bench, TruthfulQA, and the Bianchi safety set.
  6. Summarize the main content of the paper

    The authors state that stable optimization and parameter EMA can reduce harmful responses after fine-tuning without extra safety data.

    The authors re-examine a common view: aligned LLMs lose refusal ability even when supervised fine-tuned on harmless data. They define the measurement target as unsafe responses on harmful prompts, and argue that choices such as learning rate, batch size, and gradient accumulation often explain this phenomenon better than "benign fine-tuning necessarily harms safety."

    The approach has two layers. First, tune the above hyperparameters toward the more stable side, increasing epochs when learning slows. Second, apply EMA in parameter space: mix historical parameters with current parameters according to a momentum coefficient, so updates do not leave the pretrained weights too quickly. They also view this as smoothing the trajectory and as parameter merging of safety capability and task capability. EMA does not use extra safety data. Safety is measured by keyword-matched ASR and GPT-4o-20240513 1–5 point reviews; utility mainly uses MT-Bench.

    Table 2 on Llama-2-Chat-7B: Dolly keyword ASR, FT 15.96%, Best tuned 4.62%, EMA 2.70%, MT-Bench 4.46, 4.89, 4.33 respectively. On Alpaca, FT 18.46%, EMA 3.08%, utility 5.38. On AoA, FT 45.77%, EMA and Best tuned keyword ASR 0.0, EMA utility 4.62. Table 3 extends the Dolly setting to Qwen 3B and Phi 3B, EMA AdvBench 3.08 and 1.54, FT 63.46 and 49.04. On WildJailbreak, EMA remains 37.25 (Llama), 70.55 (Qwen), 49.35 (Phi).

    A-GEM, which requires external safety data, can reduce Dolly keyword ASR to 0.58, but utility is 3.81, lower than EMA's 4.33 (Table 6). The authors conclude that safety degradation in fine-tuning is largely affected by whether optimization is stable; EMA can reduce risk to about 3% (conclusion wording) while adapting to new tasks, without adding dedicated safety data. The abstract correspondingly says from 16% to about 5%, and the contribution list writes about 2.7%.

Read originalarxiv.org