Last token pooling embedding

From wenmin-wu/ds-skills · View interactive version

Last token pooling embedding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Extracts dense sentence embeddings from decoder-only LLMs by pooling the last non-padding token's hidden state.

How to use this skill

Skill file

Last-Token Pooling Embedding

Overview

Decoder-only LLMs (Qwen, LLaMA, Mistral) can produce high-quality dense embeddings for retrieval. Unlike encoder models that use [CLS] or mean pooling, causal LMs encode all context into the final token. Extract the last non-padding token's hidden state as the sentence embedding, then L2-normalize for cosine similarity search.

Quick Start

import torch
import torch.nn.functional as F
from transformers import AutoModel, AutoTokenizer

def last_token_pool(hidden_states, attention_mask):
    left_padding = attention_mask[:, -1].sum() == attention_mask.shape[0]
    if left_padding:
        return hidden_states[:, -1]
    seq_lengths = attention_mask.sum(dim=1) - 1
    return hidden_states[torch.arange(hidden_states.size(0), device=hidden_states.device), seq_lengths]

model = AutoModel.from_pretrained("Qwen/Qwen2.5-14B", torch_dtype=torch.float16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-14B")

inputs = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors="pt")
with torch.no_grad():
    outputs = model(**inputs)
embeddings = last_token_pool(outputs.last_hidden_state, inputs["attention_mask"])
embeddings = F.normalize(embeddings, p=2, dim=1)

Workflow

  1. Tokenize with left-padding or right-padding (function handles both)
  2. Forward pass through the decoder LLM
  3. Extract hidden state at the last non-padding position per sequence
  4. L2-normalize embeddings for cosine similarity
  5. Use dot product or cosine for retrieval scoring

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.