Dwell in the Beginning: How Language Models Embed Long Documents for Dense Retrieval (2024)

Abstract: This study investigates the existence of positional biases in Transformer-based language models for text representation learning, particularly in the context of web document retrieval. We build on previous research that demonstrated loss of information in the middle of input sequences for causal language models, extending it to the domain of embedding learning. We examine positional biases at multiple stages of the training pipeline for an encoder-decoder neural retrieval model, namely language model pre-training, contrastive pre-training, and contrastive fine-tuning. Experiments with the MSMARCO document collection reveal that after contrastive pre-training the model already generates embeddings that better capture the beginning of the input content, with fine-tuning further aggravating this effect.

heading: 1 Introduction
text: Recent advancements have allowed Transformerbased models to handle increasingly larger context lengths, resulting in the availability of Language Models (LMs) that can accommodate input lengths reaching tens of thousands of tokens (Xiong et al., 2023). However, studies assessing how well this context is captured by causal LMs (Liu et al., 2023) have shown that models are biased to information contained at the beginning or end of the input, losing information in the middle.
Instead of further analysing text generation, we extend this type of study to text representation learning, which has been a fundamental task for dense retrieval (Xiong et al., 2021; Karpukhin et al., 2020), and is also gaining attention in the context of retrieval-augmented generation (Chevalier et al., 2023; Mu et al., 2023) and recommendation systems (Doddapaneni et al., 2024). Specifically, we focus on web document retrieval, examining how well a single embedding represents a complete web document, while assessing the emergence of eventual position biases.
We start by continuously pre-training and finetuning an encoder-decoder model similar to T5base (Raffel et al., 2020) but with a context length of 2048 tokens, following standard techniques to achieve a model that is representative of the state-of-the-art among the low-parameter scale. We leverage the MS-MARCO (v1) document collection (Nguyen et al., 2016), as this dataset is commonly used in retrieval evaluation benchmarks (Thakur et al., 2021; Muennighoff et al., 2023), and it is one of the major sources of training data for the fine-tuning of neural retrieval models (Zhang et al., 2023; Wang et al., 2022).
We found the existence of a dwell in the beginning effect, i.e. a positional bias displayed by the model where earlier parts of the input are dominant in the embedding. We track this behavior by evaluating the model on position-aware tasks during multiple stages of its training. From our experiments, we conclude that these positional biases start emerging during unsupervised contrastive pre-training, and that the heavy reliance on MSMARCO data for fine-tuning will exacerbate this behavior. Our models and code are available in a public GitHub repository1.

heading: 2 Related Work
text: Bi-encoders are now the state of the art approach to dense retrieval (Xiong et al., 2021; Karpukhin et al., 2020). Current standard training setups leverage the usage of contrastive loss functions and methods such as ANCE (Xiong et al., 2021) to sample hard negative examples. Other techniques that are often employed include in-domain pre-training (Gao and Callan, 2022) and retrieval-aligned pre-training (Lu et al., 2021; Xiao et al., 2022; Lee et al., 2019; Ma et al., 2022, 2024), which allow for a better fine-tuning starting point, consequently achieving stronger retrieval results.
1https://github.com/cxcscmu/ LongEmbeddingAnalsys
370
For long document retrieval, early methods dealt with the increased input length through heuristic aggregation strategies, which rely on segmenting the document into passages that are scored independently, with max-pooling being particularly effective (Dai and Callan, 2019). Instead of aggregating scores, studies like PARADE (Li et al., 2020) considered the aggregation of passage-level representations. Other authors (Boytsov et al., 2022) used Transformer architectures with sparse attention patterns (Beltagy et al., 2020; Zaheer et al., 2020) to model the long inputs more efficiently, showing that, on MS-MARCO, the gains that arise from using such models are limited when compared to simple aggregation strategies.
Currently, LLaRA (Li et al., 2023) achieves stateof-the-art performance in the MS-MARCO document retrieval task, by continually pre-training LLaMA-7B (Touvron et al., 2023) with a retrievalaligned task. Models like LLaRA leverage context windows of up to 4096 tokens, relying on FlashAttention (Dao et al., 2022; Dao, 2023) for fast and exact full attention computation, together with some variation of Rotary Position Embeddings (RoPE) (Su et al., 2024) or Attention with Linear Biases (ALiBi) (Press et al., 2022). This enables stronger modeling of longer sequences, without the need of additional training, while resorting to full-attention computations.

heading: 3 Methodology
text: This section details the training of a T5-base retriever with 2048 input length (T5-2K), adapting the T5 architecture to follow recent advancements in long-context language modeling, and following a state-of-the-art dense retrieval training pipeline.

heading: 3.1 Model Architecture
text: We use the T5-base architecture as a backbone, replacing the positional embeddings by RoPE (Su et al., 2024). This change was motivated by RoPE’s ability to extrapolate to larger contexts, and its compatibility with FlashAttention. Specifically, we use Dynamic NTK-RoPE (Peng et al., 2024), which in theory allows for extrapolation to longer input sequences without further training. The retriever follows a tied bi-encoder architecture, i.e., the same model encodes both queries and documents. The T5 decoder is used as a pooler (Ni et al., 2022), generating a single token and considering its representation as the document embedding.

heading: 3.2 Dense Retriever Training Pipeline
text: Language Modelling Pre-training: Starting from T5-base available at HuggingFace2, we continuously pre-train the model on 8 billion tokens from the MS-MARCO document collection, for the model to adapt to the new maximum sequence length, new positional embeddings, and MS-MARCO’s document distribution. We follow the original T5 span-corruption task, masking 15% of the input sequence, with an average corrupted span length of 3 tokens. Unsupervised Contrastive Pre-training: In order to align the model with the fine-tuning task, we perform further pre-training following the cropping technique (Izacard et al., 2022). In this task, given a document, a positive pair (s, s+) is sampled by independently cropping two random spans comprising 10 to 50% of the input. The model is trained to minimize the following contrastive loss:
L = − 1 n
∑
i
log ecos(f(si),f(s
+ i ))
ecos(f(si),f(s + i )) + ∑
j e cos(f(si),f(s
− ij
)) , (1)
where each si is associated with one positive example s+i as per the sampling technique, and negatives {s−ij} are sampled in-batch. We use a batch size of 128 leveraging GradCache (Gao et al., 2021), and cross-device negatives across 4 GPUs. The representations f(.) generated by the model are compared using the cosine similarity function. Supervised Contrastive Fine-tuning: We finally fine-tune the model for retrieval in the MSMARCO dataset for eight epochs. Both the title and body of the documents are used, as this is the default setting for the document retrieval task. We start with ANCE-MaxP negatives (Xiong et al., 2021), refreshing them every two epochs with the model under training. We follow the loss introduced in Equation 1, leveraging labeled querydocument pairs. We sample 9 negatives per query, using a batch size of 128 and in-batch negatives. Moreover, cross-device negatives are considered across 4 GPUs, which totals 5120 documents for each query in the batch.

heading: 4 Experiments
text: This section starts by addressing the overall retrieval performance of the T5-2K model. Then, we show the dwell in the beginning behavior that is present in the model, investigating each of the training steps to identify its emergence.
2https://huggingface.co/t5-base

heading: 4.1 Retrieval Performance
text: Before moving to the study of the positional biases, we look into the overall performance of our model to assess its soundness, considering the official MSMARCO evaluation metrics (mean reciprocal rank and recall). For reference, Table 1 contains retrieval results on the MS-MARCO document dataset (development splits), where our model achieves comparable performance to models trained following similar pipelines. The first group references models that do not leverage pre-training tasks, while the ones in the second group incorporate them. Finally, the third group contains a model that also underwent simple fine-tuning, but has 30 times more parameters. Note that other authors have proposed heavily engineered pre-training tasks that do improve results (e.g., COSTA (Ma et al., 2022), Longtriever (Yang et al., 2023), or LLaRA (Li et al., 2023)), but that is out of scope for this work. Appendix A provides additional training details.

heading: 4.2 Impact of Relevant Passage Position
text: For a subset of the queries in the MS-MARCO dataset (i.e., 1130 queries), we can cross-reference their relevant documents with the MS-MARCO passage collection to identify the relevant information within the document through exact matching. In a first experiment assessing the impact of the position of the relevant passage, we retrieve from the collection 11 times: First, a default run with the documents unchanged, followed by 10 runs where the documents associated with the queries have the relevant passage moved to different positions. For each document, given its length ld and the length of the relevant passage lp (both in tokens), we compute 10 sequential and uniform insertion points (Ii) for the passage, according to Ii = (i− 1) ld−lp9 , i ∈ {1, ..., 10}, moving the passage from its original position to each Ii.
The performance of our model after one training episode (i.e., before the first ANCE negative
refreshing) is depicted in the blue lines of Figure 1. We see that when the relevant passage is moved to the beginning of the document, the performance increases when compared to the default setting (i.e., unchanged documents). Conversely, if the passage is moved anywhere else, the performance drops. The green lines show that the same pattern also holds for RepLLaMA-7B3 (Ma et al., 2023), i.e. a version of LLaMA-2 fine-tuned for dense retrieval on MS-MARCO for one epoch. In other words, a dwell in the beginning effect is observed, where the initial positions are heavily preferred to later ones.
This differs from the lost in the middle (Liu et al., 2023) phenomena, where performance would drop significantly only in middle sections, rising in the end. We also note that further fine-tuning on MS-MARCO data will aggravate the behavior, as shown by the orange lines in Figure 1, given the larger performance mismatch between the default setting and insertion positions other than the first.
3https://huggingface.co/castorini/ repllama-v1-7b-lora-doc
To better understand this behavior, we can look at the distribution in Figure 2, which shows that MS-MARCO documents tend to contain the relevant passage earlier in the document, with the median starting position at 746 characters. This can be impactful for the biases in Figure 1, given the lack of examples with relevant information later in the document. To further investigate this phenomenon, the next sub-sections explore the locality of the pre-training tasks to address potential impacts on long-context modeling.

heading: 4.3 Contrastive Pre-training Location Bias
text: To better estimate positional biases after the contrastive pre-training step, we evaluate the performance of the model on exactly matching substrings from different locations. For instance, given a document d, 10 sub-strings are sampled by segmenting d in 10 sequential groups with uniform token length. In other words, the first sub-string contains the first 10% tokens of d, while the last sub-string contains the last 10% tokens. Then, the embedding generated for d is compared with the embedding of each sub-string using the cosine similarity. Figure 3 shows that the similarity values tend to decrease when the position of the sub-string moves from the beginning, and that this behavior holds for strategies that either use decoder pooling or average pooling of token representations.
This indicates that the representation generated for a document is better at capturing its earlier contents. While in the previous sub-section similar behavior could be justified by the data’s underlying distribution, the pseudo-queries and documents for this task were independently sampled
from the same uniform distribution over the input. This suggests that the bias is intrinsic to models trained on web documents, e.g. by fitting to information distributions commonly found in real web documents that follow the inverted pyramid writing style (Koupaee and Wang, 2018), where earlier paragraphs are often more representative. Since web documents are the most common source of contrastive pre-training data (Wang et al., 2022; Izacard et al., 2022), this is problematic for tasks where the whole input must be accurately captured, as is for instance the case of retrieval augmented generation (Chevalier et al., 2023; Mu et al., 2023).

heading: 4.4 Span Corruption Location Bias
text: Finally, we look into the language model pretraining task. We evaluate on the original task, by independently corrupting spans of 3 tokens across multiple parts of the input, divided in ten windows as per the previous experiments. Through this, we can see if the accuracy of the model varies when
predicting the correct spans across the different parts of the input document.
Figure 4 shows uniform performance, suggesting no inherent bias in this task using RoPE. We also evaluate the original T5-base, and see that although it shows a slightly higher performance on predicting later positions, it is still rather uniform. As none of the models display the dwell in the beginning effect, we conclude that the language modeling pretraining task did not induce any biases, and that this behavior emerged as soon as the embedding task was added to the training pipeline. To further solidify this result, Figure 5 shows the evaluation of the T5-2K model after language model pre-training (but before embedding-based learning) on the embedding task from Section 4.3, showing a similar pattern to Figure 4, without a noticeable dwell in the beginning effect.

heading: 5 Conclusions and Future Work
text: This study investigated a dwell in the beginning effect on Transformer-based models for document retrieval. Through experiments with a T5 model and RepLLaMA, we observed that the embeddings tend to favor information located at the beginning of the input, leading to decreased performance when relevant information is elsewhere in the document. We investigate each step in the training pipeline, namely language model pre-training, contrastive pre-training, and contrastive fine-tuning, showing that biases emerge in the contrastive pretraining step, and that they persist throughout the fine-tuning process. Our findings emphasize the importance of considering the quality of embeddings for long inputs, particularly in contexts where effectively capturing the entire sequence is essential for the downstream task. Moreover, our results can further justify previous research which showed limited gains on long-sequence modeling for MS-MARCO, when compared to aggregation approaches (Boytsov et al., 2022).
As for future work, we note that while our experiments focused on tied encoders, a similar study can be conducted using untied weights, given the size mismatch between queries and documents. Furthermore, addressing the identified biases may involve devising more robust pre-training tasks, or curating better-distributed datasets, all while considering evaluation on appropriate retrieval benchmarks that require long-context modeling (Wang et al., 2023; Saad-Falcon et al., 2024).
Limitations and Ethical Considerations
All the datasets and models used in our experiments are publicly available, and we provide the source code that allows for reproduction of the results, as well as model checkpoints.
By using large pre-trained language models, we acknowledge the risks associated with the presence of inherent biases embedded within the models, which may inadvertently perpetuate or amplify societal biases present in the training data.
One limitation in the work reported on this paper relates to the fact that our tests have only used English data. Other languages can expose different phenomena in terms of how document-context is handled, and future work can perhaps consider other datasets such as the one from the NeuCLIR competition (Lawrie et al., 2024). Doing a similar analysis on other domains besides web documents would also be interesting, and we encourage the research community to further study documentcontext modeling in connection to different types of information retrieval tasks.

heading: Acknowledgements
text: We thank the anonymous reviewers for their valuable comments and suggestions. This research was supported by the Portuguese Recovery and Resilience Plan through project C64500888200000055 (i.e., the Center For Responsible AI), and also by the Fundação para a Ciência e Tecnologia (FCT), specifically through the project with reference UIDB/50021/2020 (DOI: 10.54499/UIDB/50021/2020), the project with reference UIDP/04516/2020 (DOI: 10.54499/UIDB/04516/2020), and also through the Ph.D. scholarship with reference PRT/BD/153683/2021 under the Carnegie Mellon Portugal Program.

heading: A Training Details
text: This appendix starts by detailing the training setup used in our experiments, and it then presents experimental results that further assess the impact of the different training stages.
A.1 Hyperparameters The following subsections detail the hyperparameters used for model training. If a certain element is not stated, the default value from the HuggingFace Trainer API was used. All models were trained in the same computational infrastructure with 4 NVIDIA A100 40GB GPUs.
A.1.1 Span Corruption Pre-training
A.1.2 Contrastive Pre-training
A.2 Impact of Each Training Step Table 5 aligns our training pipeline with previous work, showing the importance of the pre-training tasks, and the benefits of multiple fine-tuning steps with negative refreshing. Note that the performance without any pre-training is particularly low since the model had no previous exposure to the new rotary embeddings.

heading: Future Work
text: As for future work, we note that while our experiments focused on tied encoders, a similar study can be conducted using untied weights, given the size mismatch between queries and documents. Furthermore, addressing the identified biases may involve devising more robust pre-training tasks, or curating better-distributed datasets, all while considering evaluation on appropriate retrieval benchmarks that require long-context modeling (Wang et al. , 2023; Saad-Falcon et al. , 2024). Limitations and Ethical Considerations
All the datasets and models used in our experiments are publicly available, and we provide the source code that allows for reproduction of the results, as well as model checkpoints. By using large pre-trained language models, we acknowledge the risks associated with the presence of inherent biases embedded within the models, which may inadvertently perpetuate or amplify societal biases present in the training data. One limitation in the work reported on this paper relates to the fact that our tests have only used English data. Other languages can expose different phenomena in terms of how document-context is handled, and future work can perhaps consider other datasets such as the one from the NeuCLIR competition (Lawrie et al. , 2024). Doing a similar analysis on other domains besides web documents would also be interesting, and we encourage the research community to further study documentcontext modeling in connection to different types of information retrieval tasks