GradShield: Alignment Preserving Finetuning

Zhanhao Hu, Xiao Huang, Patrick Mendoza, Emad A. Alghamdi, Basel Alomair, Raluca Ada Popa, David WagnerNeurIPS 2026

TL;DR

Finetuning a Large Language Model (LLM) on custom data is notorious for accidentally lobotomizing its safety guardrails—even when the training data looks completely innocent on the surface. GradShield solves this by looking at gradients instead of words: it calculates how much each individual data point pushes the model's weights away from safe behavior, and surgically filters out the culprits before they do damage. It keeps jailbreak success rates below 6% without sacrificing task performance, all for roughly the computational cost of a single extra training epoch.

What Problem Does This Paper Address?

Imagine spending millions of dollars teaching an LLM to be helpful and harmless, only to open up a finetuning API and watch a user undo all that safety alignment in three epochs. Worse, the user might not even be malicious. Recent research shows a weird gap between explicit harmfulness (obvious bomb-making tutorials) and implicit harmfulness—seemingly benign data (like persona-shifting prompts asking an AI to obediently make soap) that subtly steers a model's parameters into a "say yes to everything" danger zone.

Standard content Moderation APIs fail here because they read the text looking for toxic keywords; they can't see what the data will actually do to the neural network's weights. Meanwhile, heavier defenses like post-hoc weight adjustments often ruin the model's usefulness on the actual downstream task. Model providers need a way to filter out alignment-destroying data—explicit or implicit—without tanking utility.

Figure 1: GradShield is well-suited for defending API finetuning. It protects the safety alignment of LLMs during finetuning by measuring FIHS of each user’s data point and filtering out data points with high FIHS from the finetuning dataset.

Methods

GradShield operates on a simple philosophy: don't judge a data point by its text; judge it by its gradient.

First, the system finetunes the model normally and runs a standard safety check. If it passes (the common case for benign users), there is zero overhead—ship the model! If it fails, GradShield steps in with two main innovations:

  1. Finetuning Implicit Harmfulness Score (FIHS): Ideally, you'd measure a data point's harm by training the model with and without it ("leave-one-out"), which is computationally impossible. Instead, the authors prove this can be approximated using a first-order dot product between two vectors on the initial model: the gradient update induced by the user's data point, and the gradient of a differentiable proxy safety score evaluated on a held-out harmful prompt.
  2. The Proxy Trick: Standard safety metrics aren't differentiable. So, the authors measure the logit difference at the very start of the model's response between an aligned refusal token ("I", as in "I cannot...") and a compliant token ("Sure", as in "Sure, here is..."). If a training sample's gradient pushes the model toward "Sure" and away from "I" on a harmful probe, it gets a high FIHS.
  3. Adaptive Thresholding: Since API providers don't know how poisoned a user's dataset is beforehand, a fixed cutoff won't work. GradShield fits statistical distributions (a single Gaussian vs. a two-component Gaussian Mixture Model) directly to the user's FIHS scores to guess a cutoff threshold, refining it via binary search only if safety or utility checks still fail.

Datasets

No new benchmarks were introduced; instead, the authors simulated contaminated finetuning runs by mixing existing datasets (typically 1,000 utility examples + 100 harmful examples):

  • Utility Datasets: DialogSum (Samsum dialogue summarization), AGNews, GSM8K (math), ARC (science), and Alpaca (for score visualization).
  • Explicitly Harmful Datasets: LAT-Harmful (LATharm, direct harmful Q&A) and Anthropic Redteaming (RTA, toxic conversations).
  • Implicitly Harmful Dataset: Identity-shift (IDshift), which uses benign instructions wrapped in ultra-compliant persona prompts (e.g., "Absolutely Obedient Agent") that evade text filters.

Key Insights and Ideas

  • One safety probe is enough: You don't need a massive benchmark to compute gradients. Empirically, computing the safety gradient on just a single held-out harmful prompt on the initial model gives a reliable signal across the entire dataset.
  • Logits capture intent, not just vocabulary: Even if a harmful dataset uses phrases like "I can help" instead of "Sure", finetuning on it still spikes the logit for "Sure". Those single tokens act as robust statisticalcompasses for the model's underlying intent to comply or refuse.
  • Gradients see what text moderators miss: When plotting OpenAI Moderation scores, implicitly harmful IDshift data scores near zero—indistinguishable from benign dialogue summaries. Under FIHS, however, IDshift cleanly separates out with elevated harmfulness scores.
Figure 2: Distribution of FIHS scores of utility and harmfulness datasets. (a) FIHS scores. (b) OpenAI Moderation scores.

Key Results

Across Llama-2, Llama-3, and Qwen models (including a 30B Mixture-of-Experts), GradShield consistently crushed baselines while preserving task utility:

  • Explicit Harm (Llama-3.1-8B on DialogSum + LATharm): Undefended finetuning resulted in a catastrophic 98% Attack Success Rate (ASR). OpenAI Moderation only dropped this to 75%. GradShield brought ASR down to 1% while matching the undefended model's 0.53 utility score.
  • Implicit Harm (IDshift): Undefended finetuning hit 75% ASR (LlamaGuard only reduced it to 53%). GradShield dropped ASR to 1% while preserving utility (0.51).
  • Extreme Poisoning & Efficiency: Computing FIHS takes roughly 1.0x the time of a single training epoch (~25% overhead on a 4-epoch run). Up to 30% dataset poisoning, a single threshold guess suffices. Even when 70% of the dataset is pure poison, two search rounds drop ASR from 99% to 0%.

Limitations and Future Work

GradShield isn't magic, and the authors are upfront about its boundaries:

  • Evaluation blind spots: Because GradShield relies on a final safety check to trigger filtering and guide its binary search, it cannot stop backdoor or cipher-based attacks designed to sneak past that gold-standard safety evaluation.
  • Requires a healthy starting point: The clean separation of FIHS scores relies on the initial base model being well-aligned. If you start with an already-compromised model, the gradient signals degrade.
  • Worst-case compute: While benign datasets incur near-zero overhead, heavily contaminated datasets require per-sample gradient passes and potentially multiple finetuning rounds during binary search—though the authors note that in an API setting, a malicious user submitting 90% poisoned data would be the one footing that compute bill.

Summary updated

Abstract

Large Language Models (LLMs) pose a significant risk of safety misalignment after finetuning, as models can be compromised by both explicitly and implicitly harmful data. Even some seemingly benign data can inadvertently steer a model towards misaligned behaviors. To address this, we introduce GradShield, a principled filtering method that safeguards LLMs during finetuning by identifying and removing harmful data points before they corrupt the model's alignment. It removes potentially harmful data by computing a Finetuning Implicit Harmfulness Score (FIHS) for each data point and employs an adaptive thresholding algorithm. We apply GradShield to multiple utility fine-tuning tasks across varying levels of harmful data and evaluate the safety and utility performance of the resulting LLMs using various metrics. The results show that GradShield outperforms all baseline methods, consistently maintaining an Attack Success Rate (ASR) below $6\%$ while preserving utility performance.
BibTeX
@inproceedings{hu2026gradshield,
  author = {Zhanhao Hu and Xiao Huang and Patrick Mendoza and Emad A. Alghamdi and Basel Alomair and Raluca Ada Popa and David Wagner},
  title = {{GradShield: Alignment Preserving Finetuning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=dQFVIxjyvV},
}
Figure