Out-of-distribution (OOD) detection plays a vital role in enhancing the reliability of machine learning (ML) models. The emergence of large language models (LLMs) has catalyzed a paradigm shift within the ML community, showcasing their exceptional capabilities across diverse natural language processing tasks. While existing research has probed OOD detection with relative small-scale Transformers like BERT, RoBERTa and GPT-2, the stark differences in scales, pre-training objectives, and inference paradigms call into question the applicability of these findings to LLMs. This paper embarks on a pioneering empirical investigation of OOD detection in the domain of LLMs, focusing on LLaMA series ranging from 7B to 65B in size. We thoroughly evaluate commonly-used OOD detectors, scrutinizing their performance in both zero-grad and fine-tuning scenarios. Notably, we alter previous discriminative in-distribution fine-tuning into generative fine-tuning, aligning the pre-training objective of LLMs with downstream tasks. Our findings unveil that a simple cosine distance OOD detector demonstrates superior efficacy, outperforming other OOD detectors. We provide an intriguing explanation for this phenomenon by highlighting the isotropic nature of the embedding spaces of LLMs, which distinctly contrasts with the anisotropic property observed in smaller BERT family models. The new insight enhances our understanding of how LLMs detect OOD data, thereby enhancing their adaptability and reliability in dynamic environments. We have released the source code at \url{https://github.com/Awenbocc/LLM-OOD} for other researchers to reproduce our results.
翻译:分布外(OOD)检测在提升机器学习(ML)模型可靠性方面发挥着关键作用。大语言模型(LLMs)的出现催化了ML领域内的范式转变,展现了它们在各种自然语言处理任务中的卓越能力。尽管现有研究已利用BERT、RoBERTa和GPT-2等相对小规模的Transformer模型探索了OOD检测,但规模、预训练目标和推理范式的显著差异使得这些发现是否适用于LLMs存疑。本文率先对LLMs领域中的OOD检测进行了实证研究,聚焦于参数量从7B到65B的LLaMA系列模型。我们全面评估了常用的OOD检测器,在零梯度微调和全参数微调场景下深入分析了其性能表现。值得注意的是,我们将原有的判别式分布内微调改为生成式微调,使LLMs的预训练目标与下游任务保持一致。研究结果表明,简单的余弦距离OOD检测器表现出卓越的有效性,优于其他OOD检测器。我们通过揭示LLMs嵌入空间的各向同性特性(这与较小的BERT系列模型呈现的各向异性特征形成鲜明对比),对这一现象提供了有趣的解释。这一新见解加深了我们对LLMs如何检测OOD数据的理解,从而增强了它们在动态环境中的适应性和可靠性。我们已在\url{https://github.com/Awenbocc/LLM-OOD}发布源代码,供其他研究人员复现我们的结果。