InferAligner: Inference-Time Alignment for Harmlessness through Cross-Model Guidance

Pengyu Wang, Dong Zhang, Linyang Li, Chenkun Tan, Xinghao Wang, Mozhi Zhang, Ke Ren, Botian Jiang, Xipeng Qiu

Abstract

Large Language Models (LLMs) fine-tuned for specialized domains often suffer from "alignment fading," where safety guardrails are compromised. Traditional safety training methods like RLHF or DPO are computationally expensive and often incur an "alignment tax," degrading model performance on specialized tasks. We present InferAligner, a plug-and-play inference-time alignment method. By extracting safety steering vectors (SSVs) from aligned models and employing a selective guidance gate, InferAligner effectively neutralizes harmful prompts and jailbreak attempts without impacting the utility of domain-specific models.

Introduction

As LLMs are increasingly deployed in specialized fields such as medicine, finance, and law, maintaining safety without sacrificing domain-specific expertise is a critical challenge. Standard training-phase alignment often interferes with the model's specialized knowledge. InferAligner addresses this by decoupling safety from utility, moving the alignment process to the inference stage. This approach avoids the need for massive curated datasets and intensive retraining.

Methodology: Cross-Model Guidance

InferAligner operates through activation engineering, utilizing three primary components:

  1. Safety Steering Vectors (SSVs): These vectors are derived by calculating the difference in internal activations between harmful and harmless prompt pairs in a safety-aligned model (e.g., Llama-2-chat). This captures the "direction" of harmlessness in the latent space.
  2. Guidance Gate ($g$): To prevent unnecessary interference with benign queries—the source of the alignment tax—a simple classifier detects harmful intent based on the target model's internal representations.
  3. Inference-Time Shifting: When harmful intent is detected ($g=1$), the model's activations are shifted at specific layers using the SSVs: $$x_l = x'_l + \alpha \cdot g_l \cdot \hat{s}_l$$ If the prompt is benign ($g=0$), the model remains untouched, preserving its original utility.
Mechanism of InferAligner: The guidance gate ensures that interventions only occur for harmful prompts, while benign queries (like math problems) are processed normally.

Experimental Evaluation

The effectiveness of InferAligner was evaluated across multiple domains (Finance, Medicine, Mathematics) and model families (Llama3, Qwen, InternLM).

Datasets and Metrics

Evaluation utilized AdvBench for harmful behaviors, TruthfulQA for harmless instructions, and domain-specific sets like MEDQA and GSM8K. A new MM-Harmful Bench was introduced to test multimodal LLMs. The primary metrics are Attack Success Rate (ASR) for safety and task-specific accuracy for utility.

Key Results

InferAligner demonstrates a significant reduction in ASR across all tested models and domains. In the finance domain, the ASR for harmful prompts dropped from 38.4% to 0.0% in specific tests, while Jailbreak ASR dropped from 48.2% to 0.2%. Crucially, the "alignment tax" was virtually eliminated; task accuracy remained nearly identical to unaligned base models (e.g., 92.9% accuracy in Finance before and after alignment).

Comparative performance across Llama3, Qwen, and InternLM models. InferAligner consistently reduces ASR to near-zero levels while maintaining high accuracy across Finance, Medicine, and Mathematics domains.

Discussion and Conclusion

InferAligner highlights that "safety" is a coherent, high-level concept represented within LLM activations. The success of cross-model guidance—using vectors from a chat model to guide a base or specialized model—suggests a shared internal understanding of harmlessness across model variants. Furthermore, shifting internal activations proves more robust against jailbreak attacks than prompting-based defenses, as it operates at the model's core representation level. Future work will explore extending this method to other alignment criteria like honesty and cross-architecture vector transfer.

Summary updated

BibTeX
@inproceedings{wang2024inferaligner,
  author = {Pengyu Wang and Dong Zhang and Linyang Li and Chenkun Tan and Xinghao Wang and Mozhi Zhang and Ke Ren and Botian Jiang and Xipeng Qiu},
  title = {{InferAligner: Inference-Time Alignment for Harmlessness through Cross-Model Guidance}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.585},
}
Figure