Addressing Order Sensitivity of In-Context Demonstration Examples in Causal Language Models (2024)

Abstract: In-context learning has become a popular paradigm in natural language processing. However, its performance can be significantly influenced by the order of in-context demonstration examples. In this paper, we found that causal language models (CausalLMs) are more sensitive to this order compared to prefix language models (PrefixLMs). We attribute this phenomenon to the auto-regressive attention masks within CausalLMs, which restrict each token from accessing information from subsequent tokens. This results in different receptive fields for samples at different positions, thereby leading to representation disparities across positions. To tackle this challenge, we introduce an unsupervised fine-tuning method, termed the Information-Augmented and Consistency-Enhanced approach. This approach utilizes contrastive learning to align representations of in-context examples across different positions and introduces a consistency loss to ensure similar representations for inputs with different permutations. This enhances the model’s predictive consistency across permutations. Experimental results on five benchmarks suggest that our proposed method can reduce the sensitivity of CausalLMs to the order of in-context examples and exhibit robust generalizability, particularly when demonstrations are sourced from a candidate pool different from that used in the training phase, or when the number of in-context examples differs from what is used during training.

## introduction large language models (llms) have demonstrated remarkable capabilities in various tasks @xcite @xcite by being conditioned on just a few input-label pairs as demonstrations, which is referred to as "in-context learning (icl)" @xcite . despite its effectiveness, recent studies @xcite @xcite @xcite have shown that icl is sensitive to alterations in permutation, format, and quantity of provided demonstrations. addressing the order sensitivity of in-context examples and bridging the performance gap between optimal and other permutations presents a significant challenge, with discrepancies reaching up to 30% @xcite . consequently, there are some studies aim to identify optimal permutations of in-context examples @xcite @xcite . however, the majority of these studies focus solely on causallms, where an auto-regressive attention mask is applied to limit token access to future token information in an input sequence. moreover, they primarily focus on identifying the optimal order without fundamentally enhancing the robustness of llms towards variations in demonstration order. in this paper, we propose an unsupervised finetuning method to enhance the llms' robustness against different demonstration permutations. this method is inspired by the insights drawn from our preliminary investigation into both causallms and prefixlms. prefixlms @xcite employ full attention within the input tokens, allowing in-context samples all attend to each other. for given demonstrations (i.e., in-context examples), we evaluate model performance across various permutations of the incontext examples, and introduce a metric termed as 'partial correct ratio'. this metric reflects the proportion of samples among those that can obtain the correct answer through majority voting, yet specific permutations lead to correct answers, whereas others result in incorrect ones. we find that pre-fixlms exhibit significantly lower sensitivity to order compared to causallms, with only 6.7% of test cases showing partial correctness for flan-t5-xxl, as opposed to 58.4% for llama2-chat-13b on the sst-5 @xcite dataset. we attribute this phenomenon to the autoregressive attention mask used in causallms, which results in diverse receptive fields for a given sample depending on its position within the demonstration. for example, suppose that the demonstration consists of 10 in-context examples and the test query is followed by the demonstration. when an in-context example is placed in the first position in the demonstration, the autoregressive attention mask prevents it from recognising the presence of the other 9 examples. conversely, if it is placed in the last position, it can perceive others and interact with them via the self-attention mechanism. therefore, the positioning of an example within the demonstration significantly influences the information it can gather. examples positioned further back in the demonstration possess richer information as they can perceive more examples, leading to an example's representation being greatly influenced by its position. despite the test query having access to the preceding 10 in-context examples, the representations of these in-context examples vary due to their different positions. this results in discrepancies in the attention calculation for the test query. to alleviate the sensitivity of causallms to the order of in-context examples, we propose a novel information-augmented and consistency-enhanced fine-tuning approach (infoac) 1 , which is simple, unsupervised and based on low-rank adaptation (lora) @xcite . infoac consists of two components: (1) information augmentation: recognizing that the vector representation of an in-context example is most informative when it is positioned last, and less so when it is positioned earlier, we propose using contrastive learning to align the representations of examples of earlier positions more closely with that of the lastpositioned example. this adjustment ensures that regardless of their position, the examples encapsulate the full spectrum of information. (2) consistency enhancement: to promote consistency in predicting answers for a test sample with different permutations of in-context examples, we introduce a consistency loss. this loss function constrains the representations preceding the classification head to be similar across inputs corresponding to different permutations, enhancing the model's overall consistency in prediction. during the inference stage, 1 our code and data are available at: https://github.com/xyzcs/infoac the proposed infoac exhibits strong generalizability, particularly when the in-context examples are chosen from a different pool than that utilized during training (cross-pool generalizability), or when the count of in-context examples differs from the training setup (cross-count generalizability). in summary, our main contributions are: ## conclusion in this paper, we discover that causallms are more sensitive to the order of in-context examples compared to prefixlms likely due to the auto-regressive attention mask used in causallms. to mitigate this, we propose an unsupervised information-augmented and consistency-enhanced fine-tuning approach which employs contrastive learning to align representations of an in-context example across different positions with that at the end of the demonstration. additionally, a consistency loss is introduced to improve representation consistency near the prediction head by ensuring that representations from different permutations are similar. experimental results validate the efficacy of our proposed method across five benchmarks, highlighting notable cross-pool and cross-count generalizability.