Abstract
Decoder-only Large Language Models (LLMs) are optimized for unidirectional text generation, making them suboptimal for generating text embeddings that require holistic, bidirectional context. This paper introduces Retrieval Backward Attention (ReBA), a training-free approach that enhances text embeddings by combining simple text repetition with a novel backward attention mechanism. By fusing attention matrices across all layers and using them to re-weight token representations, ReBA "unlocks" bidirectional understanding latent within generative models. Experimental results on the Chinese Massive Text Embedding Benchmark (C-MTEB) show substantial gains in zero-shot performance for models like GPT-2 and LLaMA-2 without requiring additional training or fine-tuning.
The Challenge of Unidirectional Embeddings
Most state-of-the-art LLMs, such as GPT-4 and Llama, utilize a decoder-only architecture. These models are inherently unidirectional due to a causal mask that prevents a token from attending to subsequent tokens. While this is ideal for autoregressive text generation, it poses a significant hurdle for creating high-quality text embeddings.
In tasks like semantic search or clustering, an ideal embedding should be aware of the entire sentence context. For instance, in the sentence "The bank was flooded," the meaning of "bank" is ambiguous until the end of the sentence is processed. Traditionally, encoder-only models like BERT provided this bidirectional context, but maintaining separate models for generation and embedding is computationally inefficient. The core challenge is extracting BERT-like bidirectional understanding from GPT-style models without expensive retraining.
The ReBA Framework
The researchers propose Retrieval Backward Attention (ReBA), a three-step, zero-shot process designed to enhance embeddings by leveraging the model's latent knowledge of token relationships.
- Repetition (The "Echo"): The input text is repeated (e.g., "I love NLP. I love NLP."). This simple trick allows the second occurrence of a word to attend to the first occurrence, effectively providing it with "past" context that was previously "future" context.
- Attention Matrix Fusion: Rather than relying on a single layer, ReBA extracts attention matrices from all layers and heads. It employs a "maximum update rule" to fuse these into a single symmetric matrix, ensuring that strong semantic relationships identified anywhere in the architecture are preserved.
- Backward Attention: This is the method's primary innovation. For any given token, ReBA computes a weighted sum of the embeddings of all subsequent tokens (including those in the repeated sequence) using the weights from the fused attention matrix. This process effectively "pulls" information from the future back into the current token's representation.
Experimental Evaluation
The authors evaluated ReBA across several benchmarks, focusing on both sentence-level and word-level understanding:
- C-MTEB (Chinese Massive Text Embedding Benchmark): A suite of 31 datasets covering classification, retrieval, and similarity.
- C-SEM & WSD: Tasks specifically designed to test a model's ability to disambiguate polysemous words.
Key Results
ReBA demonstrated significant performance improvements across the board:
- GPT-2 (Chinese): On C-MTEB, ReBA-2 (one repetition) achieved an average score of 0.3634, compared to the classical baseline of 0.2590.
- LLaMA-2-7B: ReBA-2 reached 0.4801, outperforming both the baseline (0.3951) and simple repetition methods (0.4733).
- Word-Level Performance: On WSD and SLPWC tasks, ReBA consistently outperformed simple repetition ("Echo embeddings"), proving that the backward attention mechanism is crucial for capturing fine-grained semantic nuances.
Discussion and Limitations
A key insight of this work is that decoder-only models already possess bidirectional understanding; the causal mask simply prevents its utilization during standard inference. ReBA successfully "unlocks" this latent capability. Furthermore, the authors improved algorithmic efficiency by reordering mathematical operations, reducing the complexity of mean pooling from $O(n^2)$ to $O(n)$.
However, the method has limitations. Repeating text increases the input sequence length, which can quadruple computational costs due to the $O(L^2)$ nature of transformer attention. Future work may explore "subsequence repetition" to mitigate this overhead while maintaining the benefits of bidirectional context.
Summary updated