kNNGuard:将LLM隐藏激活转化为免训练可配置护栏 kNNGuard: Turning LLM Hidden Activations into a Training-Free Configurable Guardrail 预印本
原文摘要
查看英文原文摘要
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 条
- 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.
- 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.
- 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.
- Jacob Austin, Augustus Odena, Maxwell Nye, and 1 others. 2021. Program synthesis with large language models. arXiv preprint arXiv:2108.07732.
- 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.
- 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.
- Mike Conover, Matt Hayes, Ankit Mathur, and 1 others. 2023. Free dolly: Introducing the world’s first truly open instruction-tuned llm.
- deepset. 2023. Prompt injections dataset. https://huggingface.co/datasets/deepset/prompt-injections. Accessed: 2026-05-26.
- 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.
- 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.
- 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.
- Aaron Grattafiori, Abhimanyu Dubey, Abhinav Jauhri, and 1 others. 2024. The llama 3 herd of models. arXiv preprint arXiv:2407.21783.
- 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.
- 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.
- 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.
- Jack Hao. 2023. Jailbreak classification dataset. https://huggingface.co/datasets/jackhhao/jailbreak-classification. Accessed: 2026-05-26.
- 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.
- 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.
- 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.
- 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.
- 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).
- Meta AI. 2025. Llama prompt guard 2. https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M. Accessed: 2026-05-26.
- 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.
- NeurAlchemy. 2026. Prompt injection and jailbreak detection dataset. Accessed: 2026-05-26.
- 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.
- 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.
- 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.
- 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.
- qualifire. 2025. Safety benchmark dataset. https://huggingface.co/datasets/qualifire/safety-benchmark. Accessed: 2026-05-26.
- QuixiAI. 2025. Llama-3-8b-instruct-abliterated-v2. Hugging Face model repository based on Meta Llama 3 8B Instruct.
- 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.
- 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.
- rogue-security. 2026. Prompt injections benchmark dataset. https://huggingface.co/datasets/rogue-security/prompt-injections-benchmark. Accessed: 2026-05-26.
- SalKhan12. 2025. Prompt safety dataset. https://huggingface.co/datasets/SalKhan12/prompt-safety-dataset. Accessed: 2026-05-26.
- Mervyn Stone. 1974. Cross-validatory choice and assessment of statistical predictions. Journal of the royal statistical society: Series B (Methodological), 36(2):111–133.
- 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.
- 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.
- 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.
- Xu Zhao, Xiting Wang, and Weiran Shen. 2026. Enhancing safety of large language models via embedding space separation. arXiv preprint arXiv:2603.20206.