Skip to content

SEAL: Safety-enhanced LLM fine-tuning via bi-level optimization data selectionSEAL: Safety-enhanced Aligned LLM Fine-tuning via Bilevel Data Selection

arXiv· arXiv:2410.07471· ICLR 2025· Han Shen, Pin-Yu Chen, Payel Das, Tianyi ChenRensselaer Polytechnic Institute; IBM ResearchIndexed by this site · 27 days ago· Originally publishedDefenses

Quick read

Defenses

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

The authors propose SEAL, which uses a bi-level optimization data selector to filter fine-tuning data; the authors claim that on LLAMA-3-8B-INSTRUCT it improves the average win rate by about 8.5% over random selection.

Threat modelThe model provider performs safety fine-tuning on user-provided fine-tuning data: the fine-tuning dataset is mixed with potentially unsafe samples that would break the model's existing safety alignment; the provider can access an aligned/safety dataset for data selection, and the fine-tuning data is too large to annotate manually.

Questions
When performing downstream fine-tuning of an already aligned LLM, even benign data may break safety alignment; the authors address this from a data-centric perspective: automatically identifying and down-weighting potentially unsafe or low-quality samples in the fine-tuning dataset.
Methods
SEAL formulates data selection as bilevel optimization: the upper level minimizes the loss on the safety dataset, and the lower level trains the model on weighted fine-tuning data; it is rewritten as a single-level problem with a penalty function, the model and selector are updated alternately with stochastic gradients, and then the top p% of data by rank is taken for fine-tuning.
Experiments and results
Under the RED ORCA fine-tuning setting, the authors report that SEAL's win rate exceeds random selection, DSIR, and SafeInstr on most test sets for LLAMA-3-8B-INSTRUCT, MERLINITE-7B, and PYTHIA-2.8B; the selector transfers across models, and the gain becomes smaller on benign data.
Limitations and future work
The authors note that training the data selector increases overall training time and GPU memory but can be done once; too small a selection ratio may lose target-domain performance; on PYTHIA-2.8B the baselines are instead lower than standard SFT, which the authors attribute to the weaker initial model after alignment.
Experimental setupLLAMA-3-8B-INSTRUCT、MERLINITE-7B et al. · RED ORCA、ANTHROPIC HH et al. · win rate、wall-clock runtime et al.
Evaluated models
LLAMA-3-8B-INSTRUCTMERLINITE-7BPYTHIA-2.8BLLAMA 2-7B-CHAT-HF
Benchmarks
RED ORCAANTHROPIC HHSLIM ORCAHEX-PHIOPEN ORCAALPACA-CLEANED
Metrics
win ratewall-clock runtimeGPU memory
AI summaryThe researchers propose the SEAL framework, which learns a data ranker through bi-level optimization to upweight safe and high-quality data and downweight unsafe or low-quality data during LLM fine-tuning, so as to avoid fine-tuning damaging the model's existing alignment and safety capabilities. On Llama-3-8b-Instruct and Merlinite-7b, models trained with SEAL have higher quality than multiple baselines, with win rates improving by 8.5% and 9.7% respectively compared with random selection, and the code is open-sourced on GitHub.

The researchers propose the SEAL framework, which learns a data ranker through bi-level optimization to upweight safe and high-quality data and downweight unsafe or low-quality data during LLM fine-tuning, so as to avoid fine-tuning damaging the model's existing alignment and safety capabilities. On Llama-3-8b-Instruct and Merlinite-7b, models trained with SEAL have higher quality than multiple baselines, with win rates improving by 8.5% and 9.7% respectively compared with random selection, and the code is open-sourced on GitHub.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors aim to address the problem that fine-tuning breaks the safety alignment of LLMs, and propose the SEAL framework, which uses bi-level optimization for safety data selection.

    Core problem: downstream fine-tuning of an already aligned LLM breaks its safety alignment; the authors aim to preserve safety during fine-tuning.

    • Problem setting: The authors cite Qi et al. (2023) and others as stating that fine-tuning on a small number of adversarial samples or even benign data for only a few epochs harms the model's existing alignment; fine-tuning is already standard practice for downstream applications, so safety degradation becomes a practical deployment obstacle.
    • Core observation: The authors believe there is often a conflict between fine-tuning data and safety alignment data—reducing the fitting loss on some fine-tuning samples that conflict with safety data inevitably raises the safety alignment loss.
    • Shortcomings of existing methods: The paper positions existing data selection methods (e.g., filtering low-quality data with strong models, resampling by importance, scoring by gradient similarity) as oriented toward quality or influence, whereas this paper selects within the same data source point by point by safety level; SafeInstr requires additionally adding safety instruction data, while SEAL does not.
    • Proposed approach: Propose SEAL (Safety-Enhanced Aligned LLM fine-tuning): learn a data ranker that up-ranks safe and high-quality samples and down-ranks unsafe or low-quality samples, then fine-tune only on the top p% of data.
    • Threat model:
      • Victim system: the model provider offering fine-tuning services (e.g., a closed-source model provider), performing downstream fine-tuning of an already safety-aligned LLM.
      • Source of risk: the user-provided fine-tuning dataset is mixed with potentially unsafe samples and is too large to annotate manually.
      • Defender's capability: the model provider can access an aligned/safety dataset (proprietary or open-source) to train the data selector.
      • Goal: the fine-tuned model maintains performance on the target task while maintaining alignment on safety evaluations.
  2. What related research exists?

    Related work falls into four groups: understanding fine-tuning jailbreaks, safety fine-tuning/alignment, data selection, and bilevel optimization algorithms.

    Understanding safety degradation during fine-tuning

    • Qi et al. (2023)—observed that fine-tuning even on benign datasets substantially harms the safety alignment of LLMs; this is the direct source of the problem in this paper.
    • Wei et al. (2024a)—proposed that competing/conflicting objectives are one of the potential causes of safety training failure; this echoes the paper's intuition of "conflict between fine-tuning data and safety data."
    • Others, including Wolf et al. (2023), Anwar et al. (2024), and Ball et al. (2024), analyze the fragility of alignment at a conceptual level; the paper only lists them.

    Safety fine-tuning and alignment methods

    • The paper lists several routes: safety instruction tuning (Ouyang et al., 2022; Bianchi et al., 2024b), feature-preserving fine-tuning (Mukhoti et al., 2023), preference-learning alignment (Rafailov et al., 2023, etc.), alignment against harmful fine-tuning (Huang et al., 2024b;a), inference-time realignment (Liu et al., 2024), model merging (Yi et al., 2024), alignment-preserving prompts (Lyu et al., 2024), representation-level defenses (Rosati et al., 2024), and safe LoRA (Hsu et al., 2024). The paper only describes them and does not comment on each.

    Data selection for LLM fine-tuning

    • Chen et al. (2023), Cao et al. (2023)—use strong models to filter low-quality data.
    • DSIR of Xie et al. (2023)—estimates an importance ratio for each data point to resample; LESS of Xia et al. (2024)—scores and selects influential instruction data using gradient similarity; Kang et al. (2024)—a selection method based on optimal transport.
    • Pan et al. (2024)—parallel work that uses a bilevel method to learn weights for multiple data sources; the paper notes that its difference from this work is that it selects data points within the same data source by safety level, rather than reweighting different datasets.

    Bilevel optimization algorithms

    • The paper reviews three branches: implicit gradient methods and iterative differentiation methods (both require second-order derivatives and are memory-inefficient for large models), and penalty-function relaxation methods (mostly require only first-order derivatives and are more suitable for LLMs); this paper's algorithm builds on the penalty methods of Shen & Chen (2023) and Liu et al. (2022a).

    Baselines and benchmarks: the experiments compare four baselines: standard SFT, random selection, DSIR, and SafeInstr; evaluation uses three test sets: ANTHROPIC HH, SLIM ORCA, and HEX-PHI, and the fine-tuning data is the self-constructed RED ORCA (SLIM ORCA 90k instructions + ANTHROPIC RED-TEAMING 22k potentially unsafe samples). The authors position this paper as: a safety fine-tuning framework that, from a data-centric perspective, selects within the same data source point by point by safety level.

  3. How does the paper address this problem?

    SEAL formalizes data selection as bilevel optimization, trains a data selector with a penalty method, and then takes the top p% of data for safety fine-tuning.

    Overall idea: given a safety dataset Dsafe and a fine-tuning dataset D mixed with unsafe samples, learn a data selector σ(ω) such that the model fine-tuned on the data it selects fits Dsafe well; the framework is named SEAL.

    Problem formalization

    • Sample z=(x,y), loss is the length-normalized negative log-likelihood ℓ(θ;z). θ are trainable parameters (LoRA weights or all parameters), ω∈R^N are selector parameters, and σ is softmax.
    • The core objective is a bilevel problem: minω 1/M∑i ℓ(θ∗(ω); zisafe), subject to θ∗(ω)=argminθ 1/N∑i σi(ω)ℓ(θ; zi). Meaning: the upper level minimizes the safety loss, and the lower level trains the model on weighted fine-tuning data.

    Penalty-function reformulation and selector update

    • Define a lower-level suboptimality penalty function p(ω,θ), and penalize it into the upper level with a constant γ∈(0,1) to obtain a single-level problem; the larger γ is, the more weight is placed on the lower-level fine-tuning loss. The authors cite Shen & Chen (2023) as stating that under appropriate conditions the solution of the penalty problem corresponds to that of the original problem.
    • Model parameters are updated by stochastic gradients of θ, mixing safety samples and weighted fine-tuning samples; γ_k starts at 0 and increases each epoch, warm-starting on the safety loss first.
    • The selector update is weighted by the loss difference ℓ(θ_k;z^j)−ℓ(θ̂_k;z^j): θ_k fits both Dsafe and weighted D, while θ̂_k fits only weighted D; a positive difference indicates that the sample is inconsistent with the safety data, lowering its rank, and a negative one raises it.
    • The auxiliary parameter θ̂ is obtained by gradient descent on the weighted fine-tuning loss (Algorithm 1).

    Memory-efficient variant

    • Algorithm 2 removes θ̂: assuming that when the model is large enough there exists θ* such that the loss on all fine-tuning samples is approximately 0, then the gradient of the lower-level optimal value with respect to ω is approximately 0, and the selector update simplifies to using only ℓ(θ_k;z^j).
    • The experiments use this variant by default to save computation.

    SEAL procedure

    1. Initial alignment: obtain a safety-aligned LLM.
    2. Selector training: using this aligned model as the initial model, train σ(ω_K) with Algorithm 1 or 2.
    3. Data selection: use σ(ω_K) to rank each data point in D, and take the top p% to form Dtop.
    4. Safety fine-tuning: fine-tune the LLM on Dtop.
    • The authors state that the first three steps can be completed once, and the selected data can be reused; the selector is transferable—S2 trains with a smaller model, and S4 fine-tunes a larger model.

    Evaluation method

    • Uses GPT-4-annotated win rate: for the same evaluation input, compare the responses of the method model and the reference model (the output model of standard SFT), and GPT-4 judges the preference; the win rate is the proportion preferred; the procedure follows AlpacaEval.
  4. What experiments did the paper conduct?

    On RED ORCA, GPT-4 win rates were measured for four models and three test sets; selection ratio, computational overhead, transferability, and benign data were also measured.

    Experimental setup

    • Models tested: LLAMA-3-8B-INSTRUCT, MERLINITE-7B, PYTHIA-2.8B (main experiments, Section 4.2); LLAMA 2-7B-CHAT-HF (benign data experiment, Section 4.5). LLAMA and MERLINITE use LoRA (rank 16, α=16), and PYTHIA uses all parameters.
    • Fine-tuning data: RED ORCA—90k English instruction-response pairs from SLIM ORCA + 22k potentially unsafe instruction-response pairs from ANTHROPIC RED-TEAMING; the safety dataset is a held-out 112k subset of SLIM ORCA (also serving as the target dataset for DSIR).
    • Test sets: ANTHROPIC HH test, SLIM ORCA test, HEX-PHI (11 categories of harmful elicitation instructions).
    • Baselines: standard SFT, random selection, DSIR, SafeInstr (mixing in safety instruction data accounting for 2%–3% of the fine-tuning set).
    • Metric and judging: GPT-4 as annotator determines the win rate of pairwise response preferences, with the reference model being the output model of standard SFT; the paper does not report the judging sample size or number of repetitions.
    • Default configuration: all selection methods select 80% of the data; selector training 2–3 epochs, batch size 64, model learning rate 1×10⁻⁵, selector learning rate 4×10⁻³–5×10⁻³; selector training for MERLINITE-7B instead uses PHI-3-MINI-128K-INSTRUCT (3.8B parameters).

    Main results

    Win rates under RED ORCA fine-tuning with 80% of data selected (according to Table 1, Table 2, Table 5; the reference model standard SFT is 50):

    Swipe horizontally to read the table

    ModelMethodHH testSLIM ORCA testHEX-PHI
    LLAMA-3-8B-INSTRUCTRandom / DSIR / SafeInstr50.78 / 57.57 / 57.9750.8 / 55.84 / 54.2256.31 / 53.95 / 64.49
    LLAMA-3-8B-INSTRUCTSEAL / SEAL+SafeInstr60.22 / 67.1953.88 / 53.9169.29 / 77.28
    MERLINITE-7BRandom / DSIR / SafeInstr60 / 62.34 / 64.3855 / 52.97 / 53.5949.97 / 56.37 / 62.85
    MERLINITE-7BSEAL / SEAL+SafeInstr76.72 / 76.4155.78 / 53.7561.5 / 64.87
    PYTHIA-2.8BRandom / DSIR / SafeInstr54.22 / 50.62 / 47.0341.25 / 44.36 / 44.5155.51 / 50.38 / 52.75
    PYTHIA-2.8BSEAL62.1949.5354.51
    • The authors state that the average win-rate gain of SEAL relative to random selection is about 8.5% on LLAMA-3-8B-INSTRUCT and about 9.8% on MERLINITE-7B (the abstract says 9.7%); relative to DSIR it is about 6% and 7% higher on average.
    • Exceptions: on MERLINITE-7B, SEAL's HEX-PHI win rate of 61.5 is lower than SafeInstr's 62.85; on PYTHIA-2.8B, SEAL's SLIM ORCA test (49.53) and HEX-PHI (54.51) are lower than random selection (except 41.25)—that is, 49.53 is higher than random but HEX-PHI 54.51 is lower than random's 55.51.
    • The authors state that the gain on PYTHIA-2.8B is smaller, and believe selector quality is affected by model capacity; SEAL+SafeInstr has an average gain of 13.5% on LLAMA-3, 5 percentage points higher than base SEAL.

    Effect of selection ratio (Section 4.3, MERLINITE-7B)

    • Different selection percentages p were tested; Figure 5 shows that for 20%≤p≤80%, the safety domain (average of HH and HEX-PHI) is close to the initial alignment level, better than no selection.
    • The target domain (SLIM ORCA) win rate decreases as p decreases; the authors state that even at p as low as 20%, SEAL still outperforms standard SFT and the aligned model.

    Computational cost and transferability (Section 4.4, Table 3)

    • Setup: train the selector with PYTHIA-2.8B or PHI-3-MINI-INSTRUCT, then fine-tune MERLINITE-7B; a single NVIDIA A6000.
    • Results: when PYTHIA selects 20% of the data, selector training takes 14 hours + fine-tuning 3 hours, 17 hours in total, close to 16 hours without selection; safety gain 14.5%↑, target domain 4.7%↑. When PHI-3 selects 80%, safety gain 19.1%↑, target domain 5.8%↑, selector training 20 hours, memory 34.1 GB.
    • Authors' interpretation: the selector can transfer across models because the safety and quality of the selected data are general advantages; selector training adds overall overhead but is a one-time investment.

    Benign fine-tuning data (Section 4.5, Table 4)

    • Setup: LLAMA 2-7B-CHAT-HF; the safety dataset is a 49.9k subset of ALPACA-CLEANED, and the benign fine-tuning data is a 49.9k subset of OPEN ORCA; selection ratio 90%; SafeInstr was not tested (the authors state they did not observe its advantage).
    • Results: SEAL is higher than standard SFT's 50 on HH test 52.03 and HEX-PHI 52.58, but SLIM ORCA test 49.84 is lower than 50; average gain 1.48↑, higher than random selection's 0.8↑ and DSIR's 0.17↓.
    • Authors' interpretation: the target-domain loss may come from the reduced amount of fine-tuning data, because benign samples themselves are of relatively good quality.

    Other ablations and analyses

    • Data quality examples (Appendix B.2, Tables 6, 7): the samples ranked high by SEAL have benign prompts and safe responses; the samples ranked low are low-quality or contain harmful responses.
    • Model output comparison (Appendix B.3, Table 8): the authors state that the LLAMA-3-8B-INSTRUCT trained with SEAL both refuses and gives alternative suggestions on HEX-PHI red-team prompts.
    • PYTHIA-2.8B alignment verification (Figure 8): initial alignment is completed by first doing SFT and then DPO (β=0.1); the authors state that alignment improved safety and instruction-following ability.
  5. What are some points that can be further explored?

    The authors note the trade-off between the computational cost of selector training and the selection ratio; the experiments cover four models and GPT-4 win-rate evaluation.

    Limitations and future directions noted by the authors

    • Training the data selector increases overall training time and requires more GPU memory than fine-tuning (Section 4.4); the authors state this can be mitigated by using a smaller selection ratio or training the selector with a smaller model, and that selector training is a one-time cost.
    • The data selection ratio has an optimal range: too small a ratio may lose performance in the target fine-tuning domain, while too large a ratio may lead to safety degradation (Section 4.3).
    • On PYTHIA-2.8B, each selection method performs worse than standard fine-tuning; the authors' potential explanation is that the initial aligned PYTHIA-2.8B model is weaker than the large models in Section 4.2, so mistakenly removed high-quality safety data has a greater impact on target performance (Appendix B.1).
    • Selector quality is affected by model capacity; the authors state that more capable models help learn a better selector (Section 4.2).
    • The paper has no dedicated Limitations section and lists no other future research directions.

    Scope of experiments

    • There are 4 models tested: LLAMA-3-8B-INSTRUCT, MERLINITE-7B, PYTHIA-2.8B (Section 4.2), and LLAMA 2-7B-CHAT-HF (Section 4.5); selector training additionally uses PHI-3-MINI-128K-INSTRUCT.
    • The fine-tuning data is the self-constructed RED ORCA (90k SLIM ORCA + 22k ANTHROPIC RED-TEAMING); the benign setting uses a 49.9k subset of OPEN ORCA; the test sets are ANTHROPIC HH, SLIM ORCA, and HEX-PHI.
    • All evaluations use GPT-4 as the annotator to compute win rates, with the reference model being the output model of standard SFT; the paper does not report the number of evaluation prompts, the number of repetitions, or human agreement.
    • Baselines are standard SFT, random selection, DSIR, and SafeInstr; the benign data experiments do not include SafeInstr.
    • Computational overhead is measured on a single NVIDIA A6000 (Table 3); the paper reports no additional validation beyond the theoretical analysis of the total query/sample complexity of selector training.
  6. Summarize the main content of the paper

    SEAL learns a data selector with bilevel optimization, preserves safety alignment on harmful fine-tuning data, and in most settings its win rate exceeds the four baselines.

    SEAL is a data selection framework for safety fine-tuning. It targets the problem that "fine-tuning an aligned LLM breaks safety alignment" and approaches it from a data-centric perspective.

    • Problem: The authors believe that fine-tuning data conflicts with safety alignment data, and that fitting samples conflicting with safety data raises the safety loss; the threat scenario is a model provider offering fine-tuning services on large user-provided data mixed with potentially unsafe samples.
    • Method: Data selection is formulated as bi-level optimization—the upper level minimizes the loss on the safety dataset, and the lower level trains on weighted fine-tuning data; a penalty function rewrites it as a single-level problem, with stochastic gradients alternately updating the model and the selector, and a memory-efficient variant without the auxiliary model is provided; finally, the top p% of data by ranking is used for fine-tuning.
    • Main results: With RED ORCA fine-tuning and 80% of data selected, SEAL achieves a win rate of 69.29 on HEX-PHI for LLAMA-3-8B-INSTRUCT (Table 1) and 76.72 on ANTHROPIC HH for MERLINITE-7B (Table 2); the authors claim average gains of about 8.5% and 9.8% over random selection; the exception is that it is lower than SafeInstr on HEX-PHI for MERLINITE-7B.
    • Other findings: The selector transfers across models (selecting data with PYTHIA-2.8B or PHI-3-MINI and fine-tuning MERLINITE-7B still works, Table 3); with selection ratios between 20% and 80%, the safety domain is close to the initial alignment (Figure 5); on benign data the average gain is only 1.48 percentage points (Table 4).
    • Authors' conclusion: SEAL improves fine-tuning safety without additionally adding safety instruction data, and can be combined with SafeInstr for further improvement; the cost is that selector training adds one-time computational overhead, and the selection ratio requires a trade-off.
Read originalICLR 2025