Tcell: Mitigating Harmful Fine-tuning for Large Language Models via Gradient Alignment

Tiansheng Huang, Virat Shejwalkar, Oscar Chang, Ling LiuNeurIPS 2026

TL;DR

When you let users fine-tune a safety-aligned Large Language Model (LLM) via an API, it only takes a handful of malicious examples to strip away its safety guardrails. Tcell solves this by reshaping the model's parameter landscape before users ever touch it: it aligns the mathematical directions of "harmful" updates and "safety" updates. As a result, when an attacker tries to teach the model bad behavior later on, their updates naturally fail to undo the model's safety training—all with zero extra compute cost per user request.

What Problem Does This Paper Address?

Fine-tuning-as-a-service is a booming business model—providers host a safety-aligned LLM and allow customers to upload custom data to specialize it. However, this opens the door to Harmful Fine-Tuning (HFT) attacks. If an adversary sneaks even a small percentage of harmful question-answer pairs into their fine-tuning dataset, the model rapidly forgets how to refuse dangerous prompts.

Figure 1: Illustration of harmful fine-tuning attack. At the first stage (safety alignment), we align the model with safety alignment data to enable the model to refuse harmful questions. At the second stage (user fine-tuning), user uploads potentially harmful data to service provider to finetune, which compromises safety alignment.

Defending against this presents a painful trade-off:

  • Fine-tuning-stage defenses intervene during every user's training job (e.g., mixing safety data into the user's batch). They work well, but multiply the computational cost for every single customer request.
  • Alignment-stage defenses fortify the base model just once before deployment, leaving user fine-tuning untouched and cheap. Unfortunately, existing alignment-stage defenses tend to crumble when attackers crank up the amount of harmful data or training steps.

The authors ask: Can we get the strong protection of fine-tuning-stage interventions while keeping the "pay-once" efficiency of an alignment-stage defense?

Methods

To figure out how to build a better shield, the authors first run an exploratory experiment during the fine-tuning stage called gMixer. Whenever a user's data nudges the model parameters along a "harmful gradient" (the direction that teaches bad behavior), gMixer blends it with a "safety gradient" (the direction that reinforces refusal).

Higher gradient similarity between mixed updates and safety gradients keeps safety loss and harmful scores low.

This reveals a fundamental geometric rule: if the fine-tuning gradient has high cosine similarity (points in roughly the same direction) with the safety gradient, the model stays safe.

Because running gMixer for every user is too expensive, the authors invent Tcell to bake this exact geometric property into the base model during the initial safety alignment stage.

  • Gradient Alignment Regularizer: Alongside standard safety training, Tcell simulates one-step "meta-updates" using both safety data and simulated harmful data. It adds a penalty (regularizer) that forces the resulting safety loss of a harmful step to match the safety loss of a safe step. This sculpts the loss landscape so that at the aligned model's resting point, harmful gradients and safety gradients naturally point in similar directions.
  • First-Order Approximation: Calculating gradients of gradients (second-order derivatives) is notoriously memory-heavy for billion-parameter LLMs, so Tcell approximates the inner meta-update derivatives as identity matrices.
  • Accelerated Tcell (AccTcell): Because Tcell still requires four forward-backward passes per step, the authors introduce AccTcell, which caches the regularizer gradient and only recomputes it every $\tau$ steps.

Datasets

The authors do not introduce new benchmarks, relying instead on established datasets across three roles:

  • Safety Alignment & Simulated Harm: Disjoint splits of BeaverTails (harmful prompts paired with either safe refusals or harmful answers).
  • Downstream Benign Tasks: SST2 (sentiment analysis), AGNEWS (news classification), and GSM8K (math reasoning) to verify the model still learns useful skills.
  • Attack & Evaluation: Disjoint BeaverTails splits, out-of-distribution harmful data from LLM-LAT, benign-looking attack data from RepMatching, plus AdvBench and SorryBench to test post-attack harmfulness.

Key Insights and Ideas

  • Why Gradient Alignment Works: Using a first-order Taylor expansion, the authors show that the spike in safety loss after a harmful update is directly proportional to the negative cosine similarity between the safety and harmful gradients. Maximize that similarity beforehand, and future harmful updates barely scratch the safety loss.
  • Full-Backbone Tcell Protects Future LoRAs: Surprisingly, if a provider runs Tcell on the full model weights during alignment, and a malicious user later fine-tunes the model using LoRA (Low-Rank Adaptation—a popular parameter-efficient method), Tcell virtually eliminates the attack. The authors admit they didn't expect protection baked into the backbone to resist a separate malicious adapter!
  • Less Work Can Mean Better Defense: Caching the regularizer in AccTcell (updating every 10 or 20 steps instead of every step) didn't just speed up training—it actually outperformed standard Tcell in safety while preserving task accuracy.

Key Results

Tested across Llama3-8B, Qwen2.5-7B, and IBM Granite-3.3-8B, Tcell consistently beats prior alignment-stage defenses (like Vaccine, RepNoise, TAR, and Booster):

  • Varying Poison Ratios: Across harmful data ratios from 5% to 40% on Llama3-8B, Tcell cuts the average Harmful Score (HS—percentage of unsafe responses) from 33.90% (standard SFT) down to 9.78%, while Fine-tune Accuracy (FA) on GSM8K barely budges (47.18% vs. 46.74%).
  • Cross-Model Generalization: Under default attack settings (20% harmful data), Tcell drives HS down to 1.60% on Granite-3.3-8B and 1.00% on Qwen2.5-7B.
  • LoRA Immunity: When full-parameter Tcell is followed by a LoRA fine-tuning attack (up to 40% harmful data), HS drops from 26.00% to 0.10% (and 0.00% at 10–20% harm ratios).
  • Speedup: Standard Tcell takes 3.69x the training time of SFT (0.48h vs. 0.13h). AccTcell ($\tau=20$) slashes that overhead to just 0.25x extra time (0.165h) while dropping Llama3-8B's Harmful Score even further, from 11.00% to 0.80%.

Limitations and Future Work

  • Intense Attacks Break Alignment Defenses: Because Tcell embeds its defense inside the model's weights, aggressive updates eventually overwrite it. At high learning rates (e.g., $2\times 10^{-6}$) or high poison ratios (40%), Tcell's protection degrades significantly. However, the authors show Tcell pairs seamlessly with fine-tuning-stage defenses (like Lisa or SafeGrad) to survive extreme attacks.
  • One-Time Compute & Memory Cost: Tcell requires ~39 GB of extra GPU memory during the alignment phase to hold intermediate gradients and backup parameters.
  • Open-Weight Models: This paper assumes a closed API where the provider controls fine-tuning hyperparameters (like learning rate). Extending Tcell to protect open-weight models—where attackers have total control over the training loop—is left for future work.

Summary updated

Abstract

Harmful fine-tuning attack becomes a concerning safety risk for mainstream fine-tuning-as-a-service providers, as attackers can submit harmful data to the API to compromise the safety alignment of the large language models. In this paper, we first explore an intuitive gradient mixing solution, and derive a key property ensuring the success of defense -- \emph{taking a fine-tuning gradient that has higher cosine similarity with the safety gradient can mitigate harmful fine-tuning.} Motivated by this key property, we design an alignment-stage defense, dubbed Tcell. The core contribution of Tcell is a regularizer that align the harmful gradient and the safety gradient during safety alignment, which ensures the harmful fine-tuning gradient exhibits high cosine similarity with the safety gradient, achieving \emph{gradient alignment}. The benefit of gradient alignment is supported by i) empirical and theoretical interpretation and ii) comparison to alternative design without gradient alignment. Lastly, Tcell might be able to extend to strengthen safety alignment of an open-weight model to avoid mis-use after its release. Code is available at \url{https://github.com/git-disl/Tcell}.
BibTeX
@inproceedings{huang2026tcell,
  author = {Tiansheng Huang and Virat Shejwalkar and Oscar Chang and Ling Liu},
  title = {{Tcell: Mitigating Harmful Fine-tuning for Large Language Models via Gradient Alignment}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=3Rc9uH2uQ1},
}
Figure