Hallucination Detection in LLMs: Fast and Memory-Efficient Finetuned Models
Gabriel Y. Arteaga Thomas B. Schön Nicolas Pielawski

Molecular spin-probe sensing of H-mediated changes in Co nanomagnets
    L. Limot
    September 9, 2024
====================================================================



§ ABSTRACT 
Uncertainty estimation is a necessary component when implementing AI in high-risk settings, such as autonomous cars, medicine, or insurances. Large Language Models (LLMs) have seen a surge in popularity in recent years, but they are subject to hallucinations, which may cause serious harm in 
high-risk settings. Despite their success, LLMs are expensive to train and run: they need a large amount of computations and memory, preventing the use of ensembling methods in practice. In this work, we present a novel method that allows for fast and memory-friendly training of LLM ensembles. We show that the resulting ensembles can detect hallucinations and are a viable approach in practice as only one GPU is needed for training and inference. Code will be made available upon acceptance.








§ INTRODUCTION

LLMs have recently grown in popularity, thanks to their ability to interpret natural language and generate answers that resemble human discussions, even surpassing human performance in specific tasks <cit.>.

However, these models face a significant challenge known as hallucination, where outputs that seem plausible may either deviate from instructions or lack factual accuracy. Hallucinations can broadly be categorized into two types <cit.>: faithfulness hallucinations, where the LLM deviates from provided instructions, and factual hallucinations, where there is a disparity between the generated content and verifiable facts. The risk arises when individuals unaware of these limitations mistakenly treat such outputs as ground-truth, leading to decisions based on erroneous information — a concern particularly relevant to safety-critical areas such as healthcare.

Techniques leveraging natural language inference models and retrieval-based methods to detect hallucinations have shown promise in specific applications like summarization and open-domain question answering  <cit.>. However,  the effectiveness of these methods is typically limited to a narrow set of tasks, which restricts their generalizability across the broader spectrum of LLM applications.

Given these limitations, uncertainty estimation methods emerge as a compelling alternative for detecting both types of hallucinations  <cit.>. Unlike task-specific approaches, uncertainty estimation uses the model's own confidence in its predictions to identify if the outputs are unfaithful or factually incorrect. 

Recent work in uncertainty quantification in LLMs have emerged, with approaches like deep ensembles  <cit.> and sample-based methods which use stochastic sampling techniques  <cit.>. However, sample-based methods seldom provide reliable uncertainty estimates as they rely on the distribution of a single model's outputs, which may not fully capture the true uncertainty in the model's predictions. While deep ensembles advertise more robust uncertainty estimates by aggregating predictions from multiple independently trained models, they come with significant computational bottlenecks, especially when applied to larger LLMs, as they require substantial resources for training and inference.

To address these limitations, we propose a fast and memory-efficient deep ensemble method which is able to provide reliable uncertainty estimates. 

Figure <ref> describes our proposed method, where low-rank matrices are added on top of a pretrained model and used for fine-tuning. A Low-Rank Adaptation (LoRA) matrix allows the whole ensemble to be trained cheaply and flexibly <cit.>, and each ensemble member has an individual rank-one fast weight matrix[LoRA matrices are rank-r matrices added elementwise to the weight matrix, while the fast weights – rank-1 matrices – are multiplied elementwise.]. This allows the members to be diverse. An uncertainty metric is used afterward and fed into a binary classifier, which is trained to discriminate between hallucinated and correct predictions.

We demonstrate that our method can effectively detect both factual and faithfulness hallucinations. Our method achieves a 97.8% accuracy in detecting faithfulness hallucinations, outperforming existing baselines. Additionally, our method attains a 68% accuracy in detecting factual hallucinations without compromising overall predictive performance. These results suggest that our method not only enhances the detection of hallucinations in LLMs, but also offers a practical solution for deploying these models in resource-constrained settings.

The main contributions of this paper are: (1) a fast and memory-efficient method for fine-tuning pre-trained LLMs using LoRA matrices and component-specific rank-1 matrix modifications, which reduces computational overhead and enables the effective use of ensemble methods; (2) a novel approach to hallucination detection that reformulates it as a binary classification task, leveraging uncertainty estimates from LLMs as features to distinguish between hallucinated and accurate content; and (3) demonstrating the practicality of our method for use with LLMs on minimal hardware, requiring only a single A40 GPU for both training and inference, thereby showcasing its efficiency and scalability.



§ RELATED WORK

We believe that distinguishing between the two types of hallucinations introduced by Huang et al.  <cit.> provides valuable insights into the behavior of LLMs and highlights the distinct challenges associated with each category. Therefore, we adopt this terminology throughout this paper.

Hallucination Detection in LLMs

Various approaches have been proposed to identify when an LLM diverges from instructions or deviates from contextual cues in the input. This work is especially critical in tasks like summarization, where adherence to the provided context is crucial for generating accurate summaries. These methods often leverage natural language inference models to compute entailment scores, which are then used to detect instances of unfaithfulness in the generated outputs  <cit.>. 

Similarly, some research already focused on detecting when LLMs produce factual hallucinations, where their generated content deviates from verifiable facts. Some methods have been developed for situations where the correct answer is known in advance, such as summarization and open-domain question answering  <cit.>. These approaches often involve comparing the generated content against a source document that is known to be accurate.

When the ground-truth is not available, some methods leverage retrieval techniques to extract reliable information for verification <cit.> or use LLMs themselves, using a prompt pipeline to facilitate hallucination detection <cit.>.

Despite their effectiveness in specific contexts, many of these methods are constrained by their task-specific nature, limiting their generalizability across different LLM applications.

Uncertainty Estimation Methods


Uncertainty estimation methods are more general, offering greater versatility in hallucination detection  <cit.>.
Sample-based methods use sampling decoding techniques to introduce stochasticity in the LLM's responses, where a higher variance of the output is an indication of the model's uncertainty  <cit.>. 

Another approach involves deep ensembles, which have been hypothesized to provide more informative uncertainty estimates compared to traditional methods  <cit.>. Deep ensembles leverage multiple model instances to capture a range of predictions, thus enhancing the robustness of uncertainty assessments  <cit.>. However, implementing deep ensembles for LLMs through both pretraining  <cit.> and fine-tuning  <cit.> has primarily been constrained to smaller models. Scaling these ensembles to compete with state-of-the-art LLMs  <cit.> typically requires significant computational resources.

Memory-Efficient Approaches


To overcome the computational challenges associated with training deep ensembles of LLMs, recent research has focused on memory-efficient alternatives. One such approach, which serves as the backbone of our method, is BatchEnsemble  <cit.>, used to pre-train LLM ensembles more efficiently  <cit.>. However, achieving state-of-the-art performance through pre-training can still be prohibitively expensive.

Recent studies have proposed a memory-friendly strategy that fine-tunes LLM ensembles from pre-trained weights, rather than training from scratch  <cit.>. This method, referred to as a LoRA Ensemble, assigns each ensemble member its own set of LoRA matrices  <cit.>. While this approach has been utilized to compute uncertainty estimates  <cit.>, it has not been specifically applied to hallucination detection tasks. Our approach is similar in two ways: it reduces training costs by utilizing pre-trained weights and it employs LoRA matrices during fine-tuning. However, unlike the LoRA Ensemble, our method does not rely on LoRA matrices to represent ensemble members. Instead, after fine-tuning, we merge the LoRA matrices with the pre-trained weights and represent the ensemble using sets of rank-one fast weights.



§ METHOD

Uncertainty Estimation

To quantify the uncertainty associated with an LLM's predictions, we use the predictive entropy of the output distribution, a concept rooted in information theory. Let 𝐱_<t={x_1,…,x_t-1} represent the preceding tokens, which serve as the input for predicting the target token 𝐱_t at time step t. The predictive entropy is then defined as:

    ℋ[P(𝐱_t|𝐱_<t; 𝒟)] =
       - ∑_x_t P(x_t|𝐱_<t; 𝒟) log P(x_t|𝐱_<t; 𝒟).

The predictive entropy can further be divided into its two subcomponents aleatoric and epistemic uncertainties:

    ℐ[𝐱_t, θ| 𝐱_<t, 𝒟]_Epistemic =
       ℋ[P(𝐱_t|𝐱_<t; 𝒟)]_Predictive-𝔼_p(θ | 𝒟)[ℋ[P(𝐱_t|𝐱_<t; 𝒟)]]_Aleatoric.


Epistemic uncertainty captures the lack of knowledge of a system, which shrinks as more data is made available. Conversely, aleatoric uncertainty represents the noise – the variability of the data – and is therefore irreducible <cit.>.
 

P(𝐱_t|𝐱_<t; 𝒟) cannot be directly computed and is approximated with:

    P(𝐱_t|𝐱_<t; 𝒟) ≈1/M∑_m=1^M P(𝐱_t|𝐱_<t; θ_m),

where θ_m is the parameters associated to the m^th model. The estimate of the predictive entropy is an estimate of the predicted uncertainty, and will yield the exact predictive uncertainty as M →∞  <cit.>.


Memory-Efficient Fine-tuning

We employ deep ensembles  <cit.> to approximate Equation (<ref>). 
However, training those deep ensembles may require prohibitively high computational resources, available to few organizations.

We propose using the BatchEnsemble method  <cit.>, but rather than pre-training each ensemble member  <cit.>, we adapt the method to fine-tune already pre-trained models. 
BatchEnsemble optimizes memory usage, which is a critical advantage over traditional ensembles. In a standard ensemble, memory requirements grow linearly with the number of ensemble members, with complexity increasing to 𝒪(Mmn) per layer, where M is the number of ensemble members and mn represents the size of the weight matrices. In contrast, BatchEnsemble reduces memory complexity to 𝒪(mn + M(m+n)) per layer, significantly lowering the memory footprint by sharing a single weight matrix U across all ensemble members and augmenting it with trainable vectors  r_i ∈ℝ^m × 1 and  s_i ∈ℝ^n × 1. The outer product of these vectors yields a fast weight matrix V_i,  allowing each ensemble member's weight matrix to be represented as the Hadamard product of the shared weight U and the fast weight V_i:

    W_i = U ⊙ V_i, where  V_i=r_i s_i^T.

We further adapt this method by substituting the shared weight with a pre-trained weight, setting U = ω_pretrained. To introduce diversity into the ensemble, we randomly initialize the fast weights. This initialization must be done carefully to preserve the knowledge stored in the pre-trained weights; initializing the fast weights with a mean of 1 is crucial to avoid disrupting the pre-trained knowledge. For more details on the weight initialization procedure, please refer to the Appendix.

To minimize the computational demands during fine-tuning, we apply the LoRA method  <cit.>. We retain the pre-trained weight matrix U as U_0 and introduce low-rank matrices B ∈ℝ^m × r and A ∈ℝ^r × n, where r ≪min(m,n). This approach allows us to update U as U_0 + BA, reducing the number of parameters that need to be trained while maintaining model performance. In contrast to the original LoRA paper  <cit.>, our method applies LoRA to all modules, which we believe enhances ensemble diversity and leads to more reliable uncertainty estimates.

Noise Injection

We hypothesize that injecting noise into the ensemble during training may enhance model diversity and improve uncertainty estimates. To explore this, we employ anchored ensembling  <cit.> as one of our methods. However, this approach can be unstable when scaled to larger models. To address this issue, we incorporate a normalization term, as suggested in  <cit.>, to stabilize the training process.


Hallucination Detection

To detect hallucinations in an LLM, we design a sub-task for a dedicated classifier. This task is framed as a binary classification problem, where the classifier is trained on a dataset containing uncertainty estimates from our ensemble and corresponding binary labels indicating whether the LLM is hallucinating or not. The choice of classifier depends on the practitioner's needs. For instance, one might choose a fast inference model like a shallow decision tree to minimize computational overhead, or opt for a more expressive model, such as a random forest, for enhanced performance.




§ DESIGN OF EXPERIMENTS

We aim to design experiments that can clearly differentiate between faithfulness and factual hallucinations, enabling us to assess the performance of our method for each type.

Baselines

We evaluate our proposed adaptations to BatchEnsemble during fine-tuning, including BatchEnsemble with noise injection (NI). For uncertainty-based experiments, we compare against two baselines: a prompt-based method that approximates the output distribution using repeated prompting with stochastic sampling (temperature = 0.5, top-p = 0.99, top-k = 5)  <cit.>, and a LoRA Ensemble, which applies regularization to LoRA B matrices as described by Wang et al. on MMLU and SQuAD datasets  <cit.>. All models, including baselines, are fine-tuned on all modules and utilize the uncertainty measurements described in Equation (<ref>) for consistency.
We also evaluate our methods against a single model fine-tuned from pre-trained weights to ensure fair performance comparison. All models utilize Mistral-7B-Instruct-v0.2 as pre-trained weights  <cit.>.

Faithfulness Hallucination Detection

To detect faithfulness hallucinations, we use the SQuAD and SQuAD 2.0 datasets  <cit.>. The datasets consist of contexts and questions, with SQuAD featuring answerable questions and SQuAD 2.0 including unanswerable ones. We instructed the LLMs to respond with “I don't know” if the answer was not in the context. Any other response to unanswerable questions indicated a faithfulness hallucination. Initially, we trained the models only on answerable questions, but this led to hallucinations across all test points. We then adjusted our approach by including 1/3 unanswerable questions in training to balance the model's responses.


Factual Hallucination Detection

For factual hallucination detection, we use the MMLU dataset  <cit.>, as the pre-trained Mistral 7B model  <cit.> used it as a benchmark without training on it. MMLU contains multiple-choice questions from diverse knowledge areas. Models were instructed to select one of the available choices; incorrect answers were labeled as factual hallucinations. 


Predictive Performance

We assess the predictive performance of our models on downstream tasks to evaluate if improved uncertainty estimates impact model accuracy. Models are fine-tuned on SQuAD and MMLU datasets, and evaluated using the F1 score, exact match for SQuAD, and accuracy for MMLU  <cit.>.

Out-Of-Distribution Test

To test out-of-distribution detection, all models are fine-tuned on answerable questions from SQuAD 2.0 and evaluated on unanswerable ones, assessing the models' capacity to recognize shifts in data distribution.



§ RESULTS


Hallucination and OOD Detection

In Table <ref>, we assess the quality of our models' and baselines' uncertainty estimates by using them as features for classifiers to predict hallucinations and OOD instances. The uncertainty estimates are fed into five classifiers: k-Nearest Neighbors, logistic regression, decision trees, random forest, and support vector machine. We report the top-1 classification accuracy for each model's uncertainty estimates. See Appendix for details.

All methods, including the baselines, demonstrate a relatively high accuracy—exceeding 92%—in detecting cases where the model faithfully hallucinates. Table <ref> describes an example of our method's response to both answerable and unanswerable questions. It shows a notable increase in uncertainty when the model encounters an unanswerable question. The uncertainty sharply decreases after the model generates the first token, suggesting that once it commits to a hallucinated token, it becomes more prone to continue hallucinating—a phenomenon referred to as the “snowballing effect”  <cit.>. More, the high accuracy achieved using the models' uncertainty estimates supports the idea that LLMs' internal states possess an inherent understanding of the generated, hallucinated content, a finding similar to that observed by Azaria & Mitchell  <cit.>.

For detecting factual hallucinations, the LoRA Ensemble's uncertainty estimates result in the highest accuracy. A possible explanation is that the high weight decay strategy applied to the LoRA B matrix  <cit.> introduces substantial stochasticity among the ensemble members, leading to improved uncertainty estimates. However, this enhanced expressiveness in uncertainty estimation comes at the cost of predictive performance, which will be discussed further in the next subsection. Additionally, while the sample-based method demonstrates slightly better performance than our proposed approach, this marginal improvement may be attributed to randomness during training or to the task itself. Producing uncertainty estimates for a single token (the choice in a multiple-choice setting) may inherently be easier for the sample-based method.

All methods generally performs worse when encountering OOD datapoints. This indicates that, while LLMs are versatile, uncertainty-based approaches still remain limited in their ability to detect hallucinations for examples that does not lie in-distribution, highlighting the need for further research on detecting hallucinations in OOD scenarios.





Predictive performance


Table <ref>
 presents the predictive performance of all evaluated models. The results indicate that while all models require fine-tuning to achieve optimal performance on downstream tasks, our proposed BatchEnsemble method with LoRA fine-tuning on shared weights consistently outperforms each baseline across all metrics. Notably, the LoRA ensemble performs worse than the single model despite being an ensemble. This performance discrepancy is likely attributed to the significant weight decay strategy implemented by Wang et al.  <cit.>, which involved fine-tuning only the query and key modules. The combination of high regularization and fine-tuning all modules appears to result in suboptimal performance. Additionally, the results from BatchEnsemble+NI further suggest that implementing regularization strategies effectively in practice poses substantial challenges.



Time and Memory footprint


All experiments were conducted on a single A40 GPU. Figure <ref> illustrates the performance of BatchEnsemble and highlights its advantages. On the left side of the figure, it is shown that as the number of ensemble members increases, the rate at which inference speed improves for BatchEnsemble is lower compared to the sample-based approach. This suggests that BatchEnsemble, by processing all ensemble members' predictions simultaneously in a single forward pass, is faster in inference than the baseline method. On the right side of the figure, the limitations of using a Vanilla ensemble  <cit.> for uncertainty estimation in LLMs  are demonstrated. Specifically, as the number of ensemble members increases, the parameter size grows linearly with the Vanilla method, whereas the increase in BatchEnsemble’s parameter size remains negligible.















    









    

    


























§ CONCLUSION

As LLMs are increasingly used across various fields, minimizing harmful predictions is crucial. This work suggests that deep ensembles are effective for quantifying uncertainty and detecting hallucinations.

Our results indicate that while current uncertainty estimation techniques are versatile, further development is needed to enhance their effectiveness in detecting factual hallucinations and distribution shifts. Nevertheless, uncertainty estimation shows promise for identifying faithful hallucinations. Integrating uncertainty estimates with classifiers could help mitigate instances where LLMs deviate from instructions. Future research could explore combining this approach with strategies such as prompting users for clarification when instructions are ambiguous.

While we used entropy-based metrics to measure uncertainty, other methods such as EigenScore  <cit.> or semantic entropy  <cit.> might offer better estimates for specific tasks. BatchEnsemble could incorporate these metrics, potentially reducing inference time by requiring just one forward pass.

Finally, we hypothesized that aleatoric and epistemic uncertainty may correspond to faithfulness and factual hallucinations, respectively. Our findings suggest that current methods for measuring epistemic uncertainty are insufficiently diverse to confirm this hypothesis. However, future noise injection methods may open the path to more informative uncertainty estimates while maintaining predictive performance.



§ ACKNOWLEDGMENTS

This project was funded by The Kjell and Märta Beijer Foundation, WASP, ERC grant 101054643. The computations were enabled by resources provided by the National Academic Infrastructure for Supercomputing in Sweden (NAISS), partially funded by the Swedish Research Council through grant agreement no. 2022-06725.






§ EXPERIMENTAL DETAILS

In this section, we will outline the detailed training and evaluation splits used for our experiments



 §.§ Factual Hallucination Detection Experiment

For the factual hallucination detection experiment, we used the MMLU dataset  <cit.>. We created a training set by combining data from the 'all' category and the 'auxiliary_train' split. The dataset was shuffled with a seed of 42, and the first 42,000 data points were selected. Of these, 40,000 were used for training, while the remaining 2,000 were set aside for validation.

For evaluation, we used the 'test' split, shuffled with the same seed of 42. We selected the first 5,000 data points from this split for evaluation.



 §.§ Faithfulness Hallucination Detection Experiment

For the faithfulness hallucination detection experiment, we used a modified version of the SQuAD v2 dataset. To create the training and validation splits, we first loaded the SQuAD v2 training set and filtered it into two subsets: answerable and unanswerable questions. Unanswerable questions were relabeled with “I don't know” to train the model to differentiate between the answerable and unanswerable questions.

We then shuffled both subsets with a seed of 50 and selected 28,000 answerable and 14,000 unanswerable questions to maintain a distribution similar to the original dataset. These subsets were combined into a single dataset and shuffled again to ensure a balanced distribution of answerable and unanswerable questions. From this combined dataset, we selected 40,000 samples for training and 2,000 samples for validation.

For evaluation, we processed the SQuAD v2 validation set and filtered to only keep unanswerable questions. The filtered unanswerable questions were shuffled with a seed of 42, and the first 5,000 data points were selected for evaluation. 



 §.§ Out-Of-Distribution Detection Experiment

For the OOD detection experiment, we trained our models on the original SQuAD dataset and evaluated them on unanswerable questions from the SQuAD v2 validation set.

We began by loading the SQuAD training dataset and shuffling it using a seed of 50 to ensure randomness. From this dataset, we selected 40,000 samples for training and 2,000 samples for validation. 

For evaluation, we utilized the validation set from SQuAD v2, specifically filtering out unanswerable questions. The unanswerable subset was shuffled with a seed of 42, and the first 5,000 data points were selected for the evaluation phase.



 §.§ Predictive Performance Experiments

To evaluate predictive performance, we used models trained during the previous experiments on both the MMLU and SQuAD datasets.

For the MMLU evaluation, we leveraged the models trained from the factual hallucination detection experiment. We loaded all data points from the MMLU test split, shuffled them with a fixed seed of 42, and selected the first 5,000 samples.

For the SQuAD dataset, we used models trained from the OOD detection experiment, as these were specifically trained on the SQuAD dataset. For evaluation, we loaded the SQuAD validation set, shuffled it with a seed of 42, and selected 5,000 samples. 



§ DETAILED RESULTS

The tables below present the detailed results for each method across five different classifiers: Logistic Regression (LR), Decision Tree (DT) classifier, Support Vector Classifier (SVC), Random Forest (RF), and k-Nearest Neighbors (kNN). All classifiers were implemented using the scikit-learn library  <cit.>. No hyperparameter tuning was performed; instead, we used the default parameters provided by scikit-learn for each classifier.

For the features, we used the first token's predictive entropy and aleatoric uncertainty. Additionally, we provided the classifiers with two more features: the average predictive entropy and the average aleatoric uncertainty. We also experimented with including epistemic uncertainty as a feature, but this reduced the performance, likely due to the correlation between the features. Therefore, we chose to exclude epistemic uncertainty and use the aforementioned features instead.










§ WEIGHT INITIALIZATION

We explore two widely-used weight initialization strategies for our fast weights: He initialization  <cit.> and Xavier initialization  <cit.>. We hypothesize that the pre-trained Mistral 7B model  <cit.> may have used one of these methods, making them natural choices for our approach.

The right side of Figure <ref> shows the generated responses for a sample data point from the SQuAD dataset, where the answer is nonsensical. This outcome is expected because the ensemble members are formed by the multiplicative product of shared and fast weights. Initializing the fast weights close to 0 effectively nullifies the knowledge embedded in the pre-trained weights. To address this, we adopt a simple solution: we maintain the variance of the He and Xavier initializations but set the mean to 1. This adjustment ensures that the ensemble members do not completely overwhelm the pre-trained weights.

The left side of Figure <ref> illustrates this modified approach. We observe that using He initialization with a mean of 1 produces diverse yet coherent predictions. The increased variability results from the higher variance of He initialization compared to Xavier initialization. We argue that greater diversity in the ensemble enhances uncertainty estimates. Therefore, we select He initialization with a mean of 1, as shown in option (a) of Figure <ref>.





§ EXAMPLES OF PROMPTS AND ANSWERS

For the SQuAD and SQuAD v2 datasets we format our prompts to the LLMs using the following template:
[colframe=black, colback=white, sharp corners=south, boxrule=1pt, width=]


[INST] 

# Answer the question based only on the given context. Keep the answer short. If the answer is not in the context or if you are unsure, respond with 'I don't know'. 

# Context: 

The provided context 

# Question: 

A question based on the provided context 

[/INST]



For the MMLU dataset we instead use the following template:
[colframe=black, colback=white, sharp corners=south, boxrule=1pt, width=]


[INST] 

# Instruction 
You will be given a question followed by four options: A, B, C, and D. Your response should be either A, B, C, or D. 

# Question: 

A knowledge question 

# Options: 

A)

B)

C)

D)

[/INST]








