Skip to content

LLM2Vec: Large Language Models Are Secretly Powerful Text Encoders

optionalextendedfundamentalpaperrubric 8/122024 · COLM 2024 · ~25 min

Parishad BehnamGhader, Vaibhav Adlakha, Marius Mosbach, … Siva Reddy

Status

TL;DR. LLM2Vec converts a decoder-only LLM into a text encoder in three steps: (1) turn on bidirectional attention, (2) train briefly with masked next-token prediction (MNTP) so the model learns to use it, and (3) train the pooled vector with unsupervised SimCSE. Without labels, Mistral-7B reaches 56.80 on MTEB. With public supervised data, LLaMA-3-8B reaches 65.01, the best among public-data models as of 2024-05-24.

  1. Bidirectional attention. Replace the causal mask with an all-ones mask.
  2. MNTP. Mask input tokens (with _, since these models have no mask token) and predict each one from the representation of the previous position, which keeps the model’s next-token head. Training uses LoRA, 1,000 steps at batch 32 on Wikitext-103, about 100 minutes for a 7B model on one A100.
  3. Unsupervised SimCSE. Merge the MNTP LoRA weights, add new LoRA weights, and train with dropout-based positives (the same sentence encoded twice) and in-batch negatives on Wikipedia sentences, with mean pooling.

Models are S-LLaMA-1.3B, LLaMA-2-7B, Mistral-7B (Instruct-v0.2) and LLaMA-3-8B. The authors argue that both steps use data the model has likely already seen, so they teach the model how to attend and pool, not new knowledge.

On the 56-task English MTEB without any labels, LLM2Vec reaches 49.42 (S-LLaMA-1.3B), 55.36 (LLaMA-2-7B), 56.80 (Mistral-7B) and 56.23 (LLaMA-3-8B). Mistral-7B goes from 42.46 with a causal model to 56.80, and its retrieval average from 10.43 to 38.05; Echo embeddings reach 50.26. With supervised contrastive training on a public replica of the E5 data (about 1.5M examples, hard and in-batch negatives, 1,000 steps at batch 512), the best results are 64.80 for Mistral-7B and 65.01 for LLaMA-3-8B, both with MNTP but without SimCSE (Table 2). The paper also shows large gains over encoder-only models on word-level tasks (chunking, NER, POS tagging).

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Mechanism

    Explain why simply removing the causal mask from a decoder-only LLM usually hurts embeddings (LLaMA-3-8B unsupervised MTEB falls from 43.98 with causal attention and weighted mean pooling to 30.56 with bidirectional attention and mean pooling), and how MNTP fixes this. Why does MNTP predict a masked token from the previous position rather than from the masked position itself, and what does Mistral-7B's improvement with no training (42.46 to 46.86) suggest?

    What a strong answer covers
    • Every layer of a causal LLM was trained on states that only saw the past. Letting a token attend to the future feeds each layer inputs from a distribution it never saw, so representations drift, and the drift compounds over layers.
    • MNTP masks some input tokens (with `_` as the mask) and trains with LoRA to recover them under bidirectional attention. To fill a gap, the model must use the right context, so it learns to use future tokens instead of being confused by them.
    • The prediction is made at position i − 1, as in next-token prediction, so the output head and the pretrained "predict the next token" pathway are reused. Only the attention pattern is new, which is why 1,000 steps (about 100 minutes for a 7B model on one A100) are enough.
    • Mistral-7B getting better with bidirectional attention before any training suggests its pretraining already included some form of bidirectional attention; the paper says so as a hypothesis, not a fact.
    • SimCSE then trains the sequence-level vector (mean pooling, dropout positives, in-batch negatives); MNTP alone fixes tokens, not the pooled embedding.
  2. Derive / compute

    Unsupervised MTEB averages for Mistral-7B (Table 1): causal with weighted mean 42.46; bidirectional with mean 46.86; + MNTP 49.43; + SimCSE (full LLM2Vec) 56.80; Echo embeddings 50.26. The retrieval columns (15 BEIR sets) are 10.43, 15.84, 19.74, 38.05 and 22.68. With supervised training on the public E5 replica (about 1.5M examples, 1,000 steps, batch 512), Mistral-7B gets 63.20 (causal), 64.80 (MNTP only) and 64.72 (with SimCSE). (a) Attribute the unsupervised gain to each step, for the average and for retrieval. (b) How many training examples does supervised training see, and how many epochs is that? (c) What does the supervised comparison say about the SimCSE step?

    What a strong answer covers
    • (a) Average: +4.40 (bidirectional), +2.57 (MNTP), +7.37 (SimCSE), total +14.34, so SimCSE gives about half. Retrieval: +5.41, +3.90, +18.31, total +27.62, so SimCSE gives about two thirds. Retrieval needs a good pooled sequence vector, which is exactly what SimCSE trains.
    • LLM2Vec beats Echo (which repeats the input so a causal model can see the whole text) by 6.54 on average and 15.37 on retrieval, at the cost of a short adaptation run instead of doubling input length at inference.
    • (b) 1,000 × 512 = 512k examples, about 0.34 epochs of 1.5M.
    • (c) With labeled contrastive data, SimCSE is unnecessary: 64.72 vs 64.80, within noise. Supervised contrastive training teaches the sequence vector directly, so the unsupervised step adds nothing. The best public-data model, LLaMA-3-8B with MNTP only, gets 65.01.
  3. Critique

    LLM2Vec claims state-of-the-art MTEB performance among models trained only on public data (as of 2024-05-24), and argues that LLMs become universal encoders without expensive adaptation or synthetic GPT-4 data. Critique the claim as an IR evaluator: consider what the public E5 replica contains, the size of the margins, and what the comparison leaves out. Propose one experiment that would test whether the backbone or the data drives the gain.

    What a strong answer covers
    • The public E5 replica includes HotpotQA, FEVER, MS MARCO, NQ and Quora, whose BEIR or MTEB counterparts are test sets, so retrieval results are partly in-domain, as for E5-Mistral.
    • "Public data only" excludes the strongest models (E5-Mistral with GPT-4 data, then NV-Embed), and the margins among public-data models are small, with no seeds or significance tests.
    • The supervised gain of LLM2Vec over a causal baseline with the same data is modest (63.20 to 64.80 for Mistral-7B); most of the score comes from the backbone plus supervised data.
    • Experiment: train causal last-token, causal mean, and LLM2Vec variants of the same 7B backbone on a mix with no BEIR-overlapping sets, and evaluate retrieval on held-out sets (BRIGHT, new-domain corpora), with several seeds.

Done with this page?

Status