Introduction
In the deployment of Natural Language Processing (NLP) systems, the ability to identify Out-of-Distribution (OOD) inputs—data that differs significantly from the training distribution—is paramount for safety and reliability. Traditional textual OOD detection methods typically rely on the final hidden layer or output logits of a model. However, this approach overlooks a wealth of information stored within the intermediate layers of deep transformer architectures. This paper challenges the "last layer" convention, demonstrating that OOD signals are often scattered across different depths depending on the task and dataset. To address this, the authors propose an unsupervised framework that aggregates anomaly scores from all layers, achieving state-of-the-art results on a new, large-scale multilingual benchmark.
The "Last Layer" Fallacy
A common assumption in NLP is that the final layer of a transformer contains the most refined semantic information, making it the natural choice for OOD detection. The authors debunk this by visualizing the Area Under the ROC Curve (AUROC) for various OOD detection tasks across all layers of a model.
As shown in the figure above, the "best" layer for detection is highly inconsistent. For some tasks, lower layers (capturing syntactic or lexical anomalies) are superior, while for others, mid-to-high layers (capturing semantic shifts) perform better. This fluctuation suggests that relying solely on the last layer is suboptimal and that a multi-layer approach is necessary to capture the full spectrum of anomalous behavior.
Proposed Framework: Layer-Wise Aggregation
The core contribution of this work is a multi-step, unsupervised aggregation framework designed to leverage the "trace" of a sample as it passes through the entire encoder stack. The process follows four distinct stages:
- Compute Per-Layer Embeddings: The input sample is passed through the transformer, and hidden states are extracted from every layer $L$.
- Compute Scores Per Class and Layer: Standard OOD metrics—such as Cosine Similarity ($S_C$), Mahalanobis distance ($S_M$), or Integrated Rank Weighted depth ($S_{IRW}$)—are calculated at every layer for every possible class.
- Perform Score Aggregation: The authors introduce two primary aggregation strategies:
- No-reference (Agg$_\emptyset$): Simple statistical aggregation (e.g., mean or median) across the layer-wise scores.
- Data-driven (Agg): This more sophisticated approach uses a reference set of In-Distribution (ID) scores. Unsupervised algorithms like Isolation Forests (IF) or Local Outlier Factor (LOF) are trained on these ID score vectors to learn the pattern of "normal" internal model behavior.
- Apply Threshold: A final anomaly threshold is applied to the aggregated score to determine if the sample is OOD.
Crucially, this framework is entirely unsupervised regarding the OOD data; it requires no exposure to actual anomalies during the setup phase, making it highly practical for real-world applications where OOD samples are unknown.
The MILTOOD-C Benchmark
To rigorously test their framework, the authors introduced MILTOOD-C, a comprehensive benchmark that addresses the limitations of existing "toy" datasets. MILTOOD-C includes 27 datasets and over 186 pairs of ID/OOD tasks. Unlike previous benchmarks that focused on binary sentiment analysis in English, MILTOOD-C features tasks with up to 150 classes (e.g., Banking77) and spans multiple languages, including French, Spanish, and German. This diversity ensures that the detection methods are robust across varying task complexities and linguistic contexts.
Key Insights and Results
The experimental results reveal several critical findings:
- Beating the Oracle: The proposed data-driven aggregation often outperforms an "oracle" that is allowed to manually select the single best layer for each task. This indicates that different layers provide complementary information that, when combined, yields a stronger signal than any individual layer.
- Text vs. Vision Disparity: In Computer Vision, the last layer is typically the most effective for OOD detection. In contrast, text models distribute OOD information more broadly, likely because linguistic anomalies can occur at different levels of abstraction (syntactic vs. semantic).
- State-of-the-Art Performance: On the MILTOOD-C benchmark, the aggregation method achieved an average AUROC of 0.99 and a False Positive Rate (FPR) at 95% recall of just 0.02. This represents a 90% reduction in FPR compared to traditional single-layer baselines.
- Robustness of Isolation Forests: Among the aggregators tested, the Isolation Forest proved the most stable, consistently improving performance regardless of the underlying scoring metric used.
Limitations and Future Outlook
While highly effective, the framework does introduce some computational overhead, as it requires processing hidden states from all layers and running an additional aggregation model. Furthermore, the data-driven approach requires storing a reference set of ID scores, which may increase memory requirements.
Future research could explore using these layer-wise patterns to provide explainability—identifying why a sample is flagged as OOD based on which layers contributed most to the anomaly score. Additionally, adapting this framework for "black-box" scenarios where internal states are inaccessible remains an open challenge.
Summary updated