防止恶意数据污染大模型安全,通过梯度手术实现任务与安全双赢
Gradient Surgery for Safe LLM Fine-Tuning
- 检测任务与安全梯度冲突,将其投影到正交平面消除危害
- 在高比例恶意数据下仍保持安全,任务性能不下降
- 适合对安全性要求高的模型微调服务场景
微调即服务引入了关键漏洞:用户微调数据中混入少量恶意样本,即可破坏大语言模型的安全对齐。尽管安全微调被普遍视为平衡用户任务表现与安全对齐的多目标优化问题,但现有方法对恶意样本比例极为敏感,随着比例上升,防御能力急剧下降。我们诊断出根本原因在于任务更新梯度与安全目标梯度相互冲突。为此,提出SafeGrad新方法,采用梯度手术:当检测到冲突时,将任务梯度中有害部分投影至与对齐梯度正交的平面,从而在不牺牲安全的前提下学习用户任务。为进一步提升鲁棒性与数据效率,引入基于KL散度的对齐损失,学习预训练模型丰富的分布级安全特征。大量实验表明,SafeGrad在多种大模型与数据集上均达到当前最优防御效果,在高恶意比例下仍保持强安全性和任务保真度。
原文摘要 · Abstract (English)
Fine-tuning-as-a-Service introduces a critical vulnerability where a few malicious examples mixed into the user's fine-tuning dataset can compromise the safety alignment of Large Language Models (LLMs). While a recognized paradigm frames safe fine-tuning as a multi-objective optimization problem balancing user task performance with safety alignment, we find existing solutions are critically sensitive to the harmful ratio, with defenses degrading sharply as harmful ratio increases. We diagnose that this failure stems from conflicting gradients, where the user-task update directly undermines the safety objective. To resolve this, we propose SafeGrad, a novel method that employs gradient surgery. When a conflict is detected, SafeGrad nullifies the harmful component of the user-task gradient by projecting it onto the orthogonal plane of the alignment gradient, allowing the model to learn the user's task without sacrificing safety. To further enhance robustness and data efficiency, we employ a KL-divergence alignment loss that learns the rich, distributional safety profile of the well-aligned foundation model. Extensive experiments show that SafeGrad provides state-of-the-art defense across various LLMs and datasets, maintaining robust safety even at high harmful ratios without compromising task fidelity.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。