kNNGuard:将LLM隐藏激活转化为免训练可配置护栏 kNNGuard: Turning LLM Hidden Activations into a Training-Free Configurable Guardrail 预印本

⚠️ 本文为 预印本(arXiv 公开版本),尚未经过正式同行评审。作者自发公开,结论未经期刊审稿验证,阅读时请知悉。
发布时间:2026-07-02
👁️ 预览文献
💡 AI 中文简介
大模型在部署时常需护栏来识别不安全、跑题或对抗性提示,但现有方案多依赖微调分类器,泛化差、延迟高。作者提出kNNGuard,无需训练,仅用50条安全/不安全提示样本,提取现成大模型的隐藏激活,做多层kNN并融合激活空间与嵌入空间分数进行分类。在六个领域上,其F1与微调SOTA护栏相当或更优,速度却快2.7倍,比微调安全分类器快10倍;换领域只需更新样本库,10秒内即可完成。
领域自然语言处理 / 大模型安全与对齐
适合谁读需要一定专业背景(了解大模型激活、kNN与安全护栏概念)
核心贡献1. 提出免训练护栏kNNGuard,直接利用现成LLM的隐藏激活空间,无需梯度更新或微调;2. 通过多层kNN融合激活空间与嵌入空间分数,实现可配置、低延迟的护栏,并支持快速领域适配。
关键数据仅需50条安全/不安全提示样本;在六个领域上F1与微调SOTA护栏相当或更优;比最佳可比护栏快2.7倍,比微调安全分类器快10倍;领域适配样本库构建时间少于10秒。
推荐理由如果你想要一个不用训练、换领域只要10秒、还比微调模型快10倍的安全护栏,这篇论文值得一读。
关键词["大模型护栏", "提示注入", "越狱检测", "免训练方法", "k近邻分类"]

原文摘要

查看英文原文摘要

Large language models (LLMs) are increasingly deployed in domains requiring guardrails to detect unsafe, off-topic, or adversarial prompts. Existing guardrails predominantly rely on fine-tuning to build classifiers, which often suffer from low generalization and high inference latency. We present kNNGuard, a training-free guardrail that utilizes the activation space of an off-the-shelf LLM. Given a small bank of 50 safe and unsafe prompts, kNNGuard extracts hidden activations and performs multi-layer kNN fusing activation-space and embedding-space scores for classification. Across six domains spanning topical and security prompts, kNNGuard achieves competitive or superior F1 compared to fine-tuned state-of-the-art guardrails while running 2.7x faster than the best comparable guardrail, and 10x faster than a fine-tuned safety classifier without gradient updates or fine-tuning. Domain adaptation requires only updating the labeled bank, which can be constructed in under 10 seconds and several orders of magnitude faster than established guardrails. We also analyze the impact of system prompts, layer selection, and integration into production LLM pipelines as a configurable, low-latency guardrail.

📚 参考文献 (39 条)

查看全部 39 条
  1. Marah Abdin, Jyoti Aneja, Hany Awadalla, and 1 others. 2024. Phi-3 technical report: A highly capable language model locally on your phone. Preprint, arXiv:2404.14219.
  2. M. Alamsabi, M. Tchuindjang, and S. Brohi. 2026. Embedding-based detection of indirect prompt injection attacks in large language models using semantic context analysis. Algorithms, 19:92.
  3. Andy Arditi, Oscar Obeso, Aaquib Syed, and 1 others. 2024. Refusal in language models is mediated by a single direction. Advances in Neural Information Processing Systems, 37:136037–136083.
  4. Jacob Austin, Augustus Odena, Maxwell Nye, and 1 others. 2021. Program synthesis with large language models. arXiv preprint arXiv:2108.07732.
  5. Julius Broomfield, Tom Gibbs, George Ingebretsen, and 1 others. 2025. The structural safety generalization problem. In Findings of the Association for Computational Linguistics: ACL 2025, pages 22134–22173.
  6. Gabriel Chua, Shing Yee Chan, and Shaun Khoo. 2024. A flexible large language models guardrail development methodology applied to off-topic prompt detection. arXiv preprint arXiv:2411.12946.
  7. Mike Conover, Matt Hayes, Ankit Mathur, and 1 others. 2023. Free dolly: Introducing the world’s first truly open instruction-tuned llm.
  8. deepset. 2023. Prompt injections dataset. https://huggingface.co/datasets/deepset/prompt-injections. Accessed: 2026-05-26.
  9. Benyamin Ghojogh, Milad Sikaroudi, H. R. Tizhoosh, Fakhri Karray, and Mark Crowley. 2020. Weighted Fisher Discriminant Analysis in the Input and Feature Spaces, page 3–15. Springer International Publishing.
  10. Shaona Ghosh, Prasoon Varshney, Makesh Narsimhan Sreedhar, and 1 others. 2025. AEGIS2.0: A diverse AI safety dataset and risks taxonomy for alignment of LLM guardrails. In Proceedings of the 2025 Conference of the Nations of the Americas Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers), pages 5992–6026, Albuquerque, New Mexico. Association for Computational Linguistics.
  11. Google DeepMind. 2026. Introducing gemma 4 12b. https://blog.google/innovation-and-ai/technology/developers-tools/introducing-gemma-4-12b/. Official release announcement, published 2026-06-03.
  12. Aaron Grattafiori, Abhimanyu Dubey, Abhinav Jauhri, and 1 others. 2024. The llama 3 herd of models. arXiv preprint arXiv:2407.21783.
  13. William Hackett, Lewis Birch, Stefan Trawicki, Neeraj Suri, and Peter Garraghan. 2025. Bypassing llm guardrails: An empirical analysis of evasion attacks against prompt injection and jailbreak detection systems. In Proceedings of the The First Workshop on LLM Security (LLMSEC), pages 101–114.
  14. Peixuan Han, Cheng Qian, Xiusi Chen, Yuji Zhang, Denghui Zhang, and Heng Ji. 2025. Safeswitch: Steering unsafe llm behavior via internal activation signals. arXiv preprint arXiv:2502.01042.
  15. Seungju Han, Kavel Rao, Allyson Ettinger, Liwei Jiang, Bill Yuchen Lin, Nathan Lambert, Yejin Choi, and Nouha Dziri. 2024. Wildguard: Open one-stop moderation tools for safety risks, jailbreaks, and refusals of llms. Advances in neural information processing systems, 37:8093–8131.
  16. Jack Hao. 2023. Jailbreak classification dataset. https://huggingface.co/datasets/jackhhao/jailbreak-classification. Accessed: 2026-05-26.
  17. Edward J Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Liang Wang, Weizhu Chen, and 1 others. 2022. Lora: Low-rank adaptation of large language models. Iclr, 1(2):3.
  18. Hakan Inan, Kartikeya Upasani, Jianfeng Chi, Rashi Rungta, Krithika Iyer, Yuning Mao, Michael Tontchev, Qing Hu, Brian Fuller, Davide Testuggine, and 1 others. 2023. Llama guard: Llm-based input-output safeguard for human-ai conversations. arXiv preprint arXiv:2312.06674.
  19. Albert Q. Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lucile Saulnier, Lélio Renard Lavaud, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, and William El Sayed. 2023. Mistral 7b. Preprint, arXiv:2310.06825.
  20. Liwei Jiang, Kavel Rao, Seungju Han, and 1 others. 2024. Wildteaming at scale: From in-the-wild jailbreaks to (adversarially) safer language models. Preprint, arXiv:2406.18510.
  21. Yunxiang Li, Zihan Li, Kai Zhang, Ruilong Dan, Steve Jiang, and You Zhang. 2023. Chatdoctor: A medical chat model fine-tuned on a large language model meta-ai (llama) using medical domain knowledge. Cureus, 15(6).
  22. Meta AI. 2025. Llama prompt guard 2. https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M. Accessed: 2026-05-26.
  23. Milad Nasr, Nicholas Carlini, Chawin Sitawarin, Sander V Schulhoff, Jamie Hayes, Michael Ilie, Juliette Pluto, Shuang Song, Harsh Chaudhari, Ilia Shumailov, and 1 others. 2025. The attacker moves second: Stronger adaptive attacks bypass defenses against llm jailbreaks and prompt injections. arXiv preprint arXiv:2510.09023.
  24. NeurAlchemy. 2026. Prompt injection and jailbreak detection dataset. Accessed: 2026-05-26.
  25. Ankit Pal, Logesh Kumar Umapathi, and Malaikannan Sankarasubbu. 2022. Medmcqa: A large-scale multi-subject multi-choice dataset for medical domain question answering. In Proceedings of the Conference on Health, Inference, and Learning, volume 174 of Proceedings of Machine Learning Research, pages 248–260. PMLR.
  26. Oam Patel and Rowan Wang. 2025. Activation monitoring: advantages of using internal representations for llm oversight. 2nd NeurIPS Works. on Attributing Model Behavior at Scale.
  27. Chetan Pathade. 2025. Red teaming the mind of the machine: A systematic evaluation of prompt injection and jailbreak vulnerabilities in llms. arXiv preprint arXiv:2505.04806.
  28. Kaiser Pister, Dhruba Jyoti Paul, Ishan Joshi, and Patrick Brophy. 2024. Promptset: A programmer’s prompting dataset. In Proceedings of the 1st International Workshop on Large Language Models for Code, pages 62–69.
  29. qualifire. 2025. Safety benchmark dataset. https://huggingface.co/datasets/qualifire/safety-benchmark. Accessed: 2026-05-26.
  30. QuixiAI. 2025. Llama-3-8b-instruct-abliterated-v2. Hugging Face model repository based on Meta Llama 3 8B Instruct.
  31. Traian Rebedea, Leon Derczynski, Shaona Ghosh, and 1 others. 2025. Guardrails and security for llms: Safe, secure and controllable steering of llm applications. In Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (Volume 5: Tutorial Abstracts), pages 13–15.
  32. Traian Rebedea, Makesh Sreedhar, Shaona Ghosh, and 1 others. 2024. Canttalkaboutthis: Aligning language models to stay on topic in dialogues. In Findings of the Association for Computational Linguistics: EMNLP 2024, pages 12232–12252.
  33. rogue-security. 2026. Prompt injections benchmark dataset. https://huggingface.co/datasets/rogue-security/prompt-injections-benchmark. Accessed: 2026-05-26.
  34. SalKhan12. 2025. Prompt safety dataset. https://huggingface.co/datasets/SalKhan12/prompt-safety-dataset. Accessed: 2026-05-26.
  35. Mervyn Stone. 1974. Cross-validatory choice and assessment of statistical predictions. Journal of the royal statistical society: Series B (Methodological), 36(2):111–133.
  36. Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, and Tatsunori B. Hashimoto. 2023. Stanford alpaca: An instruction-following llama model. https://github.com/tatsu-lab/stanford_alpaca.
  37. I. A. M. Tarun. 2023. Python code instructions 18k alpaca dataset. https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca. Accessed: 2026-05-26.
  38. Wenhui Wang, Furu Wei, Li Dong, Hangbo Bao, Nan Yang, and Ming Zhou. 2020. Minilm: Deep self-attention distillation for task-agnostic compression of pre-trained transformers. Advances in neural information processing systems, 33:5776–5788.
  39. Xu Zhao, Xiting Wang, and Weiran Shen. 2026. Enhancing safety of large language models via embedding space separation. arXiv preprint arXiv:2603.20206.