Head tail truncation

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

Head tail truncation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Preserves both the start and end of long text sequences when truncating for transformer input limits.

How to use this skill

Skill file

Head-Tail Truncation

Overview

Standard BERT truncation drops tokens from the end, losing potentially important closing content. Head-tail truncation keeps the first N and last M tokens, discarding the middle. This preserves both the introduction and conclusion of long documents, which often carry the most signal.

Quick Start

def head_tail_truncate(token_ids, max_length, head_ratio=0.5):
    """Keep head and tail tokens, drop the middle.

    Args:
        token_ids: list of token IDs (without special tokens)
        max_length: maximum allowed tokens (excluding [CLS]/[SEP])
        head_ratio: fraction of budget allocated to head

    Returns:
        truncated token ID list
    """
    if len(token_ids) <= max_length:
        return token_ids

    head_len = int(max_length * head_ratio)
    tail_len = max_length - head_len
    return token_ids[:head_len] + token_ids[-tail_len:]

Workflow

  1. Tokenize input text into subword tokens
  2. If length exceeds budget, split budget between head and tail
  3. Concatenate head tokens + tail tokens
  4. Add special tokens ([CLS], [SEP]) around the result
  5. For multi-segment inputs, apply per-segment or with shared budget

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.