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 ConsistencyEnhanced 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.

heading: 1 Introduction
text: Large Language Models (LLMs) have demonstrated remarkable capabilities in various tasks (Wei et al., 2022; Zhao et al., 2021; Liu et al., 2021) by being conditioned on just a few input-label pairs as demonstrations, which is referred to as “In-context learning (ICL)” (Brown et al., 2020). Despite its effectiveness, recent studies (Liu et al., 2021; Min et al., 2022; An
et al., 2023; Lu et al., 2022; Zhou et al., 2023) 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% (Lu et al., 2022). Consequently, there are some studies aim to identify optimal permutations of in-context examples (Li and Qiu, 2023; Wu et al., 2022; Scarlatos and Lan, 2023; Zhang et al., 2022). 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 (Tay et al., 2022; Raffel et al., 2019) 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 PrefixLMs exhibit significantly lower sensitivity to order compared to CausalLMs, with only 6.7% of test cases showing partial correctness for Flan-T5XXL, as opposed to 58.4% for Llama2-chat-13B on the SST-5 (Socher et al., 2013) dataset.
6467
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 ConsistencyEnhanced fine-tuning approach (InfoAC) 1, which is simple, unsupervised and based on Low-Rank Adaptation (LoRA) (Hu et al., 2021). 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,
1Our 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:
• We discover that CausalLMs are more sensitive to the order of in-context examples compared to PrefixLMs based on our extensive experiments on SST-5 and MMLU datasets. We hypothesize that this discrepancy may be attributed to the auto-regressive attention mask employed within CausalLMs.
• To address this issue, we propose an unsupervised approach for fine-tuning, termed Information-Augmented and ConsistencyEnhanced fine-tuning. This method aims to mitigate the impact of the order of in-context examples on the in-context learning performance of CausalLMs.
• Experimental results across five benchmark datasets, employing a range of CausalLMs, reveal that the proposed InfoAC can effectively mitigate the order sensitivity of in-context examples. Besides, it showcases significant generalizability across different selected demonstration pools and the number of demonstrations.

heading: 2 Order Sensitivity of PrefixLMs vs. CausalLMs

heading: 2.1 PrefixLMs and CausalLMs
text: Transformer (Vaswani et al., 2017) has been widely adopted as the fundamental block of both the PrefixLMs and CausalLMs. Each Transformer block is composed of two components: a standard Softmax Self-Attention layer (SSA) and a Feed-Forward layer (FF). Let T ∈ Rn×d = [t1, t2, ..., tn] be the input, n is the sequence length, and d is the feature dimension. The SSA layer can be formulated as follows:
t ′ i ← ti+Softmax(tiWQWKT T +Maski)TWV WO, (1)
MaskPrefixLMi = 0 1×n (2)
MaskCausalLMi = { MaskCausalLMi [j] = 0, if j ≤ i MaskCausalLMi [j] = −∞, if j > i
(3)
where WQ,WK ,WV ,WO ∈ Rd×d are trainable parameters corresponding to the query, key, value, and output projections, respectively. Maski ∈ R1×n refers to the attention mask. The main difference between CausalLMs and PrefixLMs lies in the mask embedding. The mask embedding MaskPrefixLMi for PrefixLMs is represented as a zero matrix, as indicated in equation 2. In a CausalLM, the auto-regressive mask embedding MaskCausalLMi for the i-th token is set to 0 for positions before or at the i-th token and negative infinity for positions following it, as specified in equation 3. After passing through the softmax activation function, the weights for tokens after the i-th token are zero, thus preventing the i-th token from accessing information from subsequent tokens. The FF layer is followed by the SSA layer:
t ′′ i ← GELU(t ′ iW1)W2, (4)
where W1 and W2 are trainable parameters and GELU(.) denotes the GELU (Hendrycks and Gimpel, 2016) activation function.

heading: 2.2 Preliminary Experimental Results
text: In this sub-section, we investigate the sensitivity of PrefixLMs and CausalLMs to the order of incontext demonstration examples. The details of the chosen models are shown in Table A1. We conduct experiments on the Stanford Sentiment Treebank with 5 labels (SST-5) (Socher et al., 2013) and Massive Multitask Language Understanding (MMLU) (Hendrycks et al., 2020) benchmarks. MMLU benchmark encompasses 57 subtasks, covering a wide range of areas including STEM, the humanities, and beyond. We exclude sub-tasks that would surpass Flan-T5’s (Raffel et al., 2019) maximum input length and present the average metrics for the remaining 43 datasets. For each test case, we randomly select 10 samples from the validation
set as its in-context examples. Given that there are 10 factorial possible permutations for 10 in-context samples, testing every permutation is impractical. Therefore, we sample 20 permutations for each test case to create prompts and fed them to LLMs to obtain their predicted answers.
Metrics To measure the model’s sensitivity to the ordering of in-context examples, we introduce three metrics: Majority Voting Accuracy, Partial Correct Ratio, and Entropy.
• Majority Voting Accuracy (MV) : It employs majority voting to consolidate the results of all prompts for a test case to obtain the final answer, and then computes the proportion of the test cases where the correct answer can be found.
• Partial Correct Ratio (Partial) : It represents the proportion of samples, among those that can obtain the correct answer through majority voting, for which only certain permutations, and not all permutations, can yield the correct answer.
• Entropy: For each test case, a count of distinct answers is conducted across all prompts. These counts are normalised by dividing by the total number of prompts to obtain a discrete distribution over the label space. The entropy of this distribution is then calculated to measure the level of disorder in the prediction results. Higher entropy implies less uniformity in the predicted answers. We report the average entropy of test cases that could obtain the correct answer through majority voting.
Results Table 1 reports the 10-shot results for the SST-5 and MMLU dataset. We also report results when the number of in-context examples is set to 4 and apply all 24 permutations to construct prompts, as detailed in Table A2. It is evident that CausalLMs exhibit a higher sensitivity to the order of in-context examples compared to PrefixLMs. As indicated in Table 1, for the MMLU dataset, the
partial correct ratio metric for all PrefixLMs remains around 0.1, whereas for the four CausalLMs, this metric can exceed 0.2, which is twice that of the PrefixLMs. Additionally, the entropy metric for PrefixLMs stays below 0.1, while for Llama7B and Vicuna-7B, the entropy values are significantly higher, recorded at 0.195 and 0.216, respectively. This discrepancy suggests that CausalLMs are more inclined to produce varied predictions when confronted with different permutations of incontext examples.
Analysis To delve deeper into the order sensitivities of PrefixLMs and CausalLMs from representation perspectives, we compare the representations of a token within a particular in-context sample when it is placed in different positions. Figure 1 and Figure 2 respectively show the heatmaps of the similarities in representations for the Llama2-
chat-7B and Flan-T5-XL models, when a sample is placed in different positions. Specifically, we sample 10 in-context examples and organise them in a random permutation. For the sample placed in the first position, we gradually move it backwards while keeping the relative positions of the other samples unchanged. Next, we retrieve the representations of a specific token within the sample from the LLMs at each position, utilizing the output from the final encoder layer of Flan-T5 and the output from the last layer of Llama. We calculate pairwise similarities for representations placed in odd positions. This process is repeated 100 times to ensure robustness. The average of these similarity matrices is then calculated, and the resulting heatmap is plotted based on this average similarity matrix. Clearly, the similarity of the token across various positions is less for Llama than for Flan-T5-XL. For Flan-T5-XL, the similarity across representations at any position typically exceeds 0.90, while for Llama, the lowest value is approximately 0.553. This observation aligns with our evaluation results in Table 1, indicating that CausalLMs are more sensitive to position variations. In particular, when the sample is positioned at the first position, the representation of the token shows the greatest difference compared to later positions, as it is unable to “see” other samples in the CausalLMs due to their position-dependent visibility.
Based on the observations, we propose InfoAC to minimise the discrepancy of representations across different positions. As illustrated in Figure A1, the application of our proposed InfoAC to CausalLMs has led to consistent representations of the sample across various positions.

heading: 3 Method

heading: 3.1 Task Definition
text: In-context learning involves learning a conditional text generation probability given a demonstration and a test query. First, a subset of the training set is selected and reserved as a candidate pool P = {(xi, yi)}NPi=1, for selecting in-context examples (Chang and Jia, 2022; Nguyen and Wong, 2023; Li and Qiu, 2023; Wu et al., 2022). Then, given a test query q, a LLM M generates a probability distribution for target y conditioned on the demonstration C:
PM =
nc∏
t=0
p(yt|y<t, q, C), (5)

heading: Negative
text: where nc is the length of the generated sequence, yt is an output label token at each time step. The demonstration C consists of k input-label pairs D = {(x1, y1), ..., (xk, yk)} chosen from P and organised according to a specific permutation fi ∈ F. Here, F = {fi}, i = 1, ..., k! denotes all possible permutations.

heading: 3.2 Information-Augmented and Consistency-Enhanced Fine-Tuning
text: As previously discussed, the notable order sensitivity of in-context examples observed in CausalLMs is likely attributed to the auto-regressive attention masks, which results in discrepancies in their contextual representations based on their positions. To deal with this problem, a direct approach is to modify the attention mask of CausalLMs to resemble that of PrefixLMs’ attention mask. However, modifying the mask embeddings requires significant changes to the model architecture, demanding a considerable amount of data for training (Song et al., 2020). Attempting to implement this change during the inference stage without training would lead to significant performance degradation.
Therefore, we propose to instead mitigate the discrepancy in representations caused by different positions through unsupervised training objectives. The method is motivated by two characteristics of position-invariant representations: (1) the representation of a given in-context example should remain robust regardless of the permutations of other input examples, and (2) it should be aware of the information contained within all other examples. Guided by these two principles, our training objective aims to align representations of an in-context example across different positions with that at the
end of the demonstration. This end-point representation encapsulates information from preceding demonstrations in the CausalLMs, hence termed as Information Augmentation. Furthermore, we enhance representation consistency near the prediction head by ensuring similarity among representations stemming from different demonstration permutations, referred to as Consistency enhancement. The overview of the proposed approach is shown in Figure 3. We adopted the LoRA adaptation(Hu et al., 2021), where all model parameters are fixed, except for the trainable query WQ and value WV matrices within the self-attention layers. In the following subsections, we will introduce the unlabelled training data and the two key components.

heading: 3.2.1 Training and Test Data Construction
text: We first randomly chose NP samples from the training set to create the candidate pool P. Then, from the remaining training set, NT samples are selected to form a pseudo test set, denoted as PT . In order to construct a training batch I = [I1, I2, ..., Im], we sample k in-context examples randomly from P, a pseudo test sample from PT and m different permutations from F. Subsequently, m prompts are generated by organizing each prompt according to the corresponding permutations obtained. Since the representation of the sample in the first position has the greatest difference from the representations in other positions, we reverse the permutation of the sampled permutations and obtain the input batch Ir for the reference model Mr. This way, every sample placed in the first position in I has a reference that is placed at last in Ir. To maximize the diversity of references for different samples, we ensure that the first in-context example in each input of a training batch I is unique. We repeat the sampling process N times to obtain our training data and reference data for the training model M and the reference model Mr. As for the test set, we randomly select k in-context examples from P for each test case and sample 20 permutations to construct prompts. After enumerating all test cases, we obtain the test set.

heading: 3.2.2 Information Augmentation
text: Recall that our objective is to align the representations from different positions with those at the end of the demonstration. To achieve this, we employ an original version of the model as a reference model Mr to derive the reference representation.
Notably, Mr is fixed and doesn’t require gradient update during the fine-tuning process. By feeding I and Ir into M and Mr, we obtain the token-level outputs of the last self-attention layer, denoted as H ∈ Rm×n×d and Hr ∈ Rm×n×d, respectively:
H = M(I),Hr = Mr(Ir), (6)
Here, m is the number of sampled permutations and also corresponds to the batch size, n indicates the length of the input sequence, and d refers to the dimension of the output representation. Then we obtain reference representations R = {r1, r2, ..., rm} for those in-context examples S = {s1, s2, ..., sm} that are placed at the last position of the demonstration according to Eq (7):
ri = Hr[i,Startii : End i i], (7)
where Startii and End i i denote the start and the end index of si within the input Ii. We introduce a token-level contrastive learning objective Lcl to integrate the information from the reference representation into the learning process of the model M :
Lcl = −log m∑
i=1
sj∈S∑
j
nj∑
o=1
Pos(i, j, o) Pos(i, j, o) + Neg(i, j, o) , (8)
Pos(i, j, o) = exp ( cos(H[i, Startji + o], rj [i, o]) ) /τ, (9)
Neg(i, j, o) = exp ( cos(H[i, Startji + o],Q) ) /τ. (10)
Here, nj is the number of tokens within the incontext example sj , Start j i represents the start index of sj within the input Ii and cos(.) denotes the cosine similarity function. The positive sample rj [i, o] is the reference representation of the token, while the negative sample Q is an independent copy of H[i,Startji + o], detached from the computational graph.
This loss mechanism aims to align the selfattention output of each token more closely with the reference output, thereby increasing the distance from its original representation. With this approach, the model integrates information from the reference representation into the LoRA parameters of selfattention layers while fine-tuning. Consequently, it allows a token to access information from subsequent tokens, regardless of the limitations imposed by the attention masks.

heading: 3.2.3 Consistency Enhancement
text: For all inputs I within a batch, we aim for the model’s predicted results to exhibit consistency. As the LoRA adaptation is applied, the parameters of the classification head remain fixed during training. Thus, we enforce similarity among the hidden representations of the last token across the input batch I , which are then input into the classification head to predict the next output tokens. This approach is designed to enhance the consistency of the model’s output.
Lcon = m∑
i=1
(1− cos(H[i,−1],Ha)), (11)
Ha =
∑m i=1H[i,−1]
m , (12)
Here, Ha ∈ Rd represents the average representation derived from all inputs. The total loss L combines these two losses:
L = Lcl + λ1Lcon (13)

heading: 4 Experiment

heading: 4.1 Experimental Setup
text: We conduct our experiments using 5 datasets that cover a range of types such as text classification, textual entailment, and mathematics. These datasets include SST-5 (Socher et al., 2013), SST2 (Socher et al., 2013), QQP (DataCanary et al., 2017), Sequence Next Term (Saxton et al., 2019) and Round Number (Saxton et al., 2019). The statistics for each dataset are presented in Table A3, and detailed descriptions of each dataset can be found in section A.4.
The details of the experimental setup for training, inference, and in-context learning are provided in section A.5. We use the same evaluation metrics, i.e., Majority Voting Accuracy (MV), Partial Correct Ratio (Partial) and Entropy, as outlined in the section 2.2.

heading: 4.2 Main Results
text: To mitigate the effects of different candidate pools, we perform the experiment three times, randomly selecting a new candidate pool for each iteration. Table 2 reports the mean and standard deviation of the corresponding metrics across three iterations on SST-5, QQP, Sequence Next Term and Round Number benchmarks while the results on the SST-2 benchmark are reported in the Table A4.
Our observations are as follows: (1) Across the five datasets, all four backbone models clearly show sensitivity to the ordering of in-context examples, indicating that order sensitivity is independent of the task type and domain. Specifically, for the SST5 and QQP datasets, the partial correct ratio metrics for all models are around or exceed 0.5. (2) As the model size increases, it does not reduce sensitivity to order. For instance, in the SST-5 dataset, Llama-13B and Vicuna-13B have partial correct ratios of 0.605 and 0.524, respectively. These metrics are higher compared to 0.566 for Llama-7B and 0.469 for Vicuna-7B. A similar trend is also observed in the Round Number benchmark. (3) Our proposed Information Augmentation (IA) method significantly mitigates the sensitivity of CausalLMs to order, with an average decrease of about 0.192 and 0.157 on the partial correct ratio and the entropy metric. Even though it is unsupervised, IA can enhance overall performance to some degree on certain datasets. For instance, it results in an average increase of 0.020 in the majority voting accuracy metric on the QQP dataset. (4) Intro-
ducing the consistency loss further enhances the robustness of CausalLMs to the order of in-context examples. Compared to the IA method, it results in a 0.079 decrease in the partial correct ratio and a 0.054 decrease in the entropy metrics.

heading: 4.3 In-context Selection Results
text: In addition to random in-context example selection results presented above, we explore whether the advanced in-context example selection methods can enhance the robustness of CausalLMs to the order of in-context examples. Specifically, we focus on KNN and One-Shot approaches:
• K-Nearest Neighbors (KNN) (Liu et al., 2021; Gao et al., 2021): KNN aims to select the semantically closest samples from the candidate pool as in-context examples for each test case. Given a test sample and a candidate pool, an LM is adopted to encode the test sample and each candidate sample. Next, the cosine similarity is computed between the test query’s representation and the representations of all candidate queries. The samples with the highest similarity serve as in-context examples.
• One-Shot (Chang and Jia, 2022): One-shot aims to find a candidate pool that contains higher-quality samples. It reserves a small set Ddev from the training set to serve as a pseudo validation set. The remaining training samples are individually utilized as demonstrations for one-shot in-context learning, with each sample’s score determined by its ICL accuracy on Ddev. The top-ranked samples are selected as the candidate pool.
The implementation details are provided in Appendix A.6. Table 3 presents the results of applying the KNN and One-shot approaches on the SST-5 dataset, while Table A6 displays the results on the Sequence Next Term dataset. It’s evident that employing more effective methods for selecting incontext examples can enhance the model’s overall performance, as seen in the increase in majority voting accuracy. However, these improvements do not address the model’s inherent sensitivity to the order of demonstrations. The high sensitivity of CausalLMs to the order of demonstrations stems from the model’s architecture, specifically the autoregressive attention masks. After applying InfoAC, significant improvements are noticeable across a range of metrics, including majority voting accuracy.

heading: 4.4 Generalizability Across Different Numbers of In-Context Examples and Different Candidate Pools
text: In this section, we explore the generalizability of the CausalLMs after fine-tuning with the proposed InfoAC. We first explore its generalizability across varying numbers of in-context samples. In the previous experiments, we set the number of in-context examples k to 10. Here, we directly apply the model trained with InfoAC at k = 10 to testing at k = 4, k = 20 and k = 50. The candidate
pool P remains the same across the different values of k. Table 4 and Table A7 show the experimental results on the SST-5 and Sequence Next Term benchmarks. We can observe that: (1) As k increases, the model maintains its sensitivity to order and even shows a trend of increase. When k increases from 4 to 20 and 50, the partial correct ratio of Llama-7B on the SST-5 dataset increases from 0.556 to 0.592 and 0.643, respectively. (2) The proposed InfoAC can generalize across different numbers of in-context samples. For example, when k = 20, for LLama-7B, LLama-13B, Vicuna-7B and Vicuna-13B, after applying InfoAC, the partial correct ratio metric decreased by 0.368, 0.325, 0.266 and 0.305 respectively. Meanwhile, the entropy decreased by 0.275, 0.256, 0.194 and 0.229 respectively. A similar trend can also be observed when k = 4 and k = 50. (3) When k = 4, InfoAC not only reduces the model’s sensitivity to order but also enhances the overall performance of the model. On the SST-5 and Sequence Next Term datasets, all four models show improvements in the majority voting metric. This is because the model is trained at k = 10, using the representation at the 10th position to augment the representations at earlier positions, thereby improving performance.
We further explore the effectiveness of our method across different candidate pools in Appendix A.7 which demonstrates that the CausalLMs fine-tuned with InfoAC have robust generalizability to different candidate pools. It means when a different candidate pool is used during testing, the model still significantly reduces its sensitivity to the order of in-context examples.

heading: 5 Related Work
text: Recently, some research has focused on investigating how the order of in-context examples affects
in-context learning. Lu et al. (2022) proposed that the order of the demonstrations has a significant impact on the performance of in-context learning and the optimal order is model-dependent and not transferable across different models. Liu et al. (2023) proposed that ICL achieves optimal performance when the relevant information is positioned at the beginning or end of the demonstrations. The performance notably degrades when models are required to retrieve relevant information from the middle of long contexts. Bertsch et al. (2024); Agarwal et al. (2024) explore the order sensitivity of in-context examples in the scenario of many-shot in-context learning, where the demonstrations contain hundreds or even thousands of in-context examples. Agarwal et al. (2024) observes that LLMs still exhibit significant order sensitivity of in-context examples and Bertsch et al. (2024) proposes that as the number of in-context examples increases, the order sensitivity decreases.
Therefore, numerous research efforts focus on finding the best permutations of in-context examples. Wu et al. (2022) proposed a ranking algorithm based on the perspective of compression. The assumption is that a well-organized arrangement of in-context examples can effectively compress test samples without sacrificing any information. Li and Qiu (2023) propose a diversity-guided example search method that iteratively refines and assesses chosen example permutations to identify the optimal permutation. Scarlatos and Lan (2023); Zhang et al. (2022) regarded in-context example selection as a sequential decision-making problem and applied reinforcement learning to solve it. However, these studies are mainly focused on CausalLMs and do not delve into the causes behind order sensitivity.

heading: 6 Conclusion
text: 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 InformationAugmented 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.

heading: Limitations
text: This study primarily investigates the sensitivity of LLMs to the order of in-context examples in tasks that have definite answers. However, for more open-ended generation tasks, such as summarization and code generation, the impact of the order of in-context samples on the effectiveness and quality of the generated answers has yet to be explored. We will continue to explore this type of task in the future.

heading: Acknowledgements
text: This work was supported in part by the UK Engineering and Physical Sciences Research Council (EPSRC) through a Turing AI Fellowship (grant no. EP/V020579/1, EP/V020579/2) and a New Horizons grant (grant no. EP/X019063/1).

heading: A Appendix / supplemental material

heading: A.1 Overview of the Selected LLMs
text: Table A1 provides details of the LLMs employed in our research, specifying the type, the max input length and the size of each LLM. Throughout the paper, the term "Llama" is used to denote "Llama2chat".

heading: A.2 Preliminary Experimental Results on SST-2 and MMLU Benchmarks with 4-shot
text: Table A2 represents experimental results on SST-5 and MMLU benchmarks with the number of incontext examples set to 4. We use all 24 possible permutations to generate prompts and obtain their answers by feeding them to LLMs. For the MMLU benchmark, we present the average experimental results for the same sub-tasks as shown in Table 1. We use the code released by (Fu et al., 2023) to conduct the experiments.

heading: A.3 Similarities of the Last Layer Outputs of LLama After Fine-tuning with InfoAC
text: Figure A1 shows the heat map that visualises the similarities of representations of a sample across different positions for Llama after fine-tuning with InfoAC. The chosen samples are identical to the ones featured in both Figure 1 and Figure 2.
Figure A1: The heatmap visualizes the similarities in representations of a specific token within a sample from the last layer outputs across different positions for Llama2-chat-7B after fine-tuning with InfoAC.
Name Type Size Max Input Length
Flan-T5-XL (Raffel et al., 2019) PrefixLM 3B 2048 Flan-T5-XXL (Raffel et al., 2019) PrefixLM 11B 2048 Flan-UL2 (Tay et al., 2022) PrefixLM 20B 2048
Llama2-chat-7B (Touvron et al., 2023) CausalLM 7B 4096 Llama2-chat-13B (Touvron et al., 2023) CausalLM 13B 4096 Vicuna-7B (Zheng et al., 2023) CausalLM 7B 4096 Vicuna-13B (Zheng et al., 2023) CausalLM 13B 4096
Table A1: Details about the models used in the experiments.
Benchmark SST-5 MMLU
Metrics MV(↑) Partial(↓) Entropy(↓) MV(↑) Partial(↓) Entropy(↓) Flan-T5-XL 0.437 0.169 0.117 0.507 0.077 0.063 Flan-T5-XXL 0.326 0.025 0.014 0.535 0.080 0.061 Flan-UL2 0.517 0.079 0.054 0.559 0.068 0.054
Llama-7B 0.504 0.556 0.390 0.468 0.207 0.167 Llama-13B 0.459 0.623 0.482 0.520 0.175 0.129 Vicuna-7B 0.496 0.353 0.262 0.487 0.229 0.172 Vicuna-13B 0.477 0.516 0.399 0.546 0.180 0.136
Table A2: Experimental results on SST-5 and MMLU benchmarks with 4-shot.

heading: A.4 Statistics of Benchmarks
text: Table A3 provides an overview of the datasets used in our study, detailing the type of the benchmark and the size of the training, development, and test sets. Here is a brief introduction to the datasets:
• Stanford Sentiment Treebank with 5 Labels Dataset (Socher et al., 2013) (SST-5): This dataset consists of movie reviews annotated with five levels of sentiment: very positive, positive, neutral, negative, and very negative.
• Stanford Sentiment Treebank with 2 Labels Dataset (Socher et al., 2013) (SST-2): It is a binary sentiment analysis dataset, with two levels of sentiment: positive and negative.
• Quora Question Pairs dataset (QQP) (DataCanary et al., 2017): It is a dataset related to the problem of identifying duplicate questions. Each sample contains a pair of sentences, and labels of ‘duplicate’ and ‘not duplicate’ are assigned based on whether the semantic information of the two sentences overlaps.
• Sequence Next Term Dataset (Saxton et al., 2019): This sub-task belongs to the Mathematics dataset (Saxton et al., 2019) and serves as a generation task designed to assess the mathematical capabilities of a language model (LM).
Each sample consists of a sequence of numbers arranged in a specific pattern, and the task requires the LM to predict the next number in the sequence.
• Round Number Dataset (Saxton et al., 2019): It is also a generation task that forms part of the Mathematics dataset. Each sample’s input is an instruction to round a decimal, and the answer is the result after operating as instructed.

heading: A.5 Experimental Setup
text: We adopt four CausalLMs including Llama2-chat7B (Touvron et al., 2023), Llama2-chat-13B (Touvron et al., 2023), Vicuna-7B (Zheng et al., 2023) and Vicuna-13B (Zheng et al., 2023) as backbones. For all LLMs, the rank of LoRA is set to 8. The hyperparameter λ1 in the total loss function is set to 1. These models are fine-tuned using the Adam optimizer (Kingma and Ba, 2014) with a learning rate of 1e-4, and training is conducted for a single epoch. In the inference phase, we use a greedy search decoding algorithm to prevent the model from predicting different answers due to randomness. Experiments are carried out on A100-80G GPUs, with most of the fine-tuning processes being completed within four hours.
For the settings of in-context learning, the number of in-context examples k is set at 10. The
Name Type Train Dev Test
SST-5 (Socher et al., 2013) Classification 8.5k 1.1k 2.2k SST-2 (Socher et al., 2013) Classification 67.3k 0.9k 1.8k QQP (DataCanary et al., 2017) Classification 364k 40.4k 391k Sequence Next Term (Saxton et al., 2019) Generation 2M - 10k Round Number (Saxton et al., 2019) Generation 2M - 10k
Table A3: Statistics regarding the benchmarks employed in the experiment.
template T of our input consists of three parts: Instruction, demonstration and the test sample, which is shown as follows:
T = [Instruction,Demonstration,Test], (14)
Demonstration = [“Q : {xi} A : {yi}”]×10, (15)
Test = “Q : {Test Query} A : ”. (16)
Here, ‘Instruction’ refers to the description of the task, and ‘Demonstration’ consists of 10 in-context examples arranged in a certain permutation. The query xi and answer yi of each in-context sample are positioned within the template following ‘Q:’ and ‘A:’, respectively. ‘Test’ refers to the test query we need the model to answer. The default sizes for both the candidate pool P and the pseudo test set PT are established at 100. We sample N = 1000 training batches to construct the training dataset. For each training batch, we sample m = 8 permutations to construct prompts. In the inference stage, 20 permutations are sampled for each test case to construct prompts, consistent with the preliminary experiments described in section 2.2.
We evaluate our method on the development sets of the QQP and SST-2 datasets, which are part of the GLUE benchmark (Wang et al., 2019), owing to the unavailability of publicly released test labels. Specifically, for the QQP dataset, due to the large size of its original development set, we select 1000 development samples as our test set. In contrast, for the SST-2 benchmark, we perform our evaluation using its entire original development set. The evaluation for the SST-5 dataset (Socher et al., 2013) is carried out on its original test set. For the Sequence Next Term and Round Number datasets, we sample 1000 test samples for our evaluation.
A.6 Implementation Details of the In-context Selection Approaches
In this section, we outline the implementation details of the two in-context selection approaches. The setup of in-context learning is the same as the main experiments, as detailed in Appendix A.5.
For the KNN method, the candidate pool is randomly selected from the training set and consists of 100 examples. For each test sample, we adopt RoBERTa (Liu et al., 2019) to embed the test query and all the candidate queries. The average representation of all tokens in a sample is used as the sample’s representation, and the semantic similarities between the test query and all candidate queries are calculated. The 10 samples with the highest semantic similarity are selected as the in-context examples for the test case.
As for the One-Shot method, We reserve 100 samples from the training set to serve as Ddev. The remaining 5000 training samples that do not appear in Ddev are assessed to create the candidate pool. The 100 samples with the highest one-shot in-context learning accuracy on the Ddev are selected as the candidate pool. For each test case, we randomly select 10 samples from this candidate pool as its in-context samples.
We construct training data for InfoAC sharing the same candidate pool as both KNN and OneShot respectively. In the inference stage, InfoAC and the original LLM use exactly the same test dataset for evaluation.

heading: A.7 Generalizability Across Different Candidate Pools
text: In this section, we explore the generalizability of the proposed InfoAC when the demonstrations used during inference are not from the same candidate pool in training. Table A5 presents the results for the SST-5 and the Sequence Next Term dataset benchmark. It shows that our method has good generalizability to different candidate pools. When a different candidate pool is used during testing, the model still significantly reduces its sensitivity to the order of in-context examples. The main reason for this is that our method is trained in an unsupervised manner, aiming to learn a better representation for each in-context example. Since the information in various samples shares similarities, the insights and knowledge acquired by the model
Dataset SST-2
Metrics MV(↑) Partial(↓) Entropy(↓) Llama-7B 0.934±0.005 0.081±0.005 0.050±0.004 +IA 0.944±0.001 0.036±0.010 0.023±0.006 +InfoAC 0.944±0.002 0.024±0.007 0.015±0.003 Llama-13B 0.935±0.004 0.080±0.015 0.050±0.009 +IA 0.943±0.003 0.037±0.003 0.022±0.002 +InfoAC 0.944±0.004 0.015±0.004 0.010±0.003 Vicuna-7B 0.938±0.007 0.074±0.004 0.045±0.003 +IA 0.941±0.007 0.029±0.003 0.020±0.003 +InfoAC 0.942±0.006 0.022±0.003 0.014±0.002 Vicuna-13B 0.924±0.009 0.065±0.011 0.040±0.006 +IA 0.940±0.002 0.039±0.001 0.022±0.002 +InfoAC 0.943±0.004 0.026±0.002 0.016±0.002
Table A4: Experimental results on the SST-2 benchmark.
Metrics MV(↑) Partial(↓) Entropy(↓) Metrics MV(↑) Partial(↓) Entropy(↓) SST-5
Llama-7B 0.509 0.559 0.391 Vicuna-7B 0.505 0.457 0.314 +InfoAC(Same) 0.533 0.161 0.108 +InfoAC(Same) 0.497 0.108 0.066 +InfoAC(Diff) 0.520 0.160 0.108 +InfoAC(Diff) 0.503 0.138 0.095
Llama-13B 0.495 0.600 0.452 Vicuna-13B 0.490 0.497 0.356 +InfoAC(Same) 0.502 0.176 0.117 +InfoAC(Same) 0.520 0.224 0.149 +InfoAC(Diff) 0.509 0.188 0.120 +InfoAC(Diff) 0.517 0.195 0.128
Sequence Next Term
Llama-7B 0.233 0.369 0.342 Vicuna-7B 0.226 0.504 0.546 +InfoAC(Same) 0.257 0.175 0.147 +InfoAC(Same) 0.234 0.235 0.198 +InfoAC(Diff) 0.244 0.189 0.132 +InfoAC(Diff) 0.219 0.237 0.184
Llama-13B 0.230 0.404 0.372 Vicuna-13B 0.233 0.442 0.431 +InfoAC(Same) 0.284 0.134 0.108 +InfoAC(Same) 0.278 0.187 0.159 +InfoAC(Diff) 0.289 0.166 0.121 +InfoAC(Diff) 0.281 0.242 0.181
Table A5: Cross-Pool experimental results on the SST-5 and Sequence Next Term dataset. “Diff” denotes that the training set for InfoAC from a candidate pool differs from the candidate pool for the test data. “Same” denotes that the same pool is used for both the training and testing phases.
from one candidate set can also help reduce the order sensitivity of in-context examples in another candidate set.

heading: A.8 Additional Supplementary Experimental Results on the Sequence Next Term benchmark
text: In this section, we provide in-context selection and cross-count results on the Sequence Next Term benchmark, as detailed in Table A6, and Table A7. As shown in Table A6, using KNN for in-context selection on the Sequence Next Term dataset does not lead to performance improvement when compared to random selection 2. This is because the query of this dataset consists of a sequence of numbers
2Results can be found in Table 2
and does not contain semantic information. KNN selects samples that are semantically most similar to the test case as in-context examples, which is not suitable for this dataset.

heading: A.9 Ethical Statement
text: In this paper, we conduct preliminary experiments on the Massive Multitask Language Understanding (Hendrycks et al., 2020) (MMLU) benchmark to explore the impact of the order of in-context examples on the performance of PrefixLMs and CausalLMs. This benchmark is crafted to assess the knowledge acquisition of LLMs via pretraining, by evaluating them exclusively in zero-shot and few-shot settings. It encompasses 57 subtasks, covering a wide range of areas including
Metrics MV(↑) Partial(↓) Entropy(↓) MV(↑) Partial(↓) Entropy(↓) KNN One-Shot
Llama-7B 0.212 0.458 0.502 0.270 0.359 0.354 +InfoAC 0.255 0.204 0.177 0.277 0.181 0.141
Llama-13B 0.262 0.447 0.431 0.299 0.314 0.250 +InfoAC 0.283 0.170 0.140 0.312 0.151 0.112
Vicuna-7B 0.202 0.535 0.604 0.256 0.473 0.462 +InfoAC 0.234 0.252 0.232 0.272 0.206 0.154
Vicuna-13B 0.276 0.460 0.467 0.290 0.303 0.280 +InfoAC 0.283 0.208 0.163 0.294 0.228 0.158
Table A6: Performances of different in-context sample selection methods on the Sequence Next Term dataset.
Metrics MV(↑) Partial(↓) Entropy(↓) MV(↑) Partial(↓) Entropy(↓) MV(↑) Partial(↓) Entropy(↓) k=4 k=20 k=50
Llama-7B 0.233 0.283 0.261 0.251 0.482 0.452 0.229 0.550 0.583 +InfoAC 0.263 0.163 0.123 0.266 0.214 0.187 0.252 0.337 0.274
Llama-13B 0.261 0.264 0.232 0.287 0.380 0.316 0.287 0.390 0.331 +InfoAC 0.286 0.119 0.092 0.287 0.164 0.142 0.281 0.178 0.141
Vicuna-7B 0.210 0.424 0.436 0.229 0.546 0.583 0.237 0.561 0.700 +InfoAC 0.244 0.238 0.191 0.236 0.292 0.257 0.258 0.411 0.417
Vicuna-13B 0.271 0.369 0.344 0.283 0.378 0.327 0.277 0.343 0.330 +InfoAC 0.284 0.211 0.161 0.288 0.236 0.199 0.271 0.173 0.155
Table A7: Cross-count experimental results on the Sequence Next Term dataset.
STEM, the humanities, and beyond. Certain subtasks, like “moral scenarios” and “moral disputes”, might include statements that elicit ethical considerations. There is a certain risk that LLMs may misuse text from datasets, therefore, we recommend thoroughly considering safety before practical application.

heading: Future Work
text: We will continue to explore this type of task in the future