Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:LLMBook zh LLMBook zh github io Batch Data Collation

From Leeroopedia


Knowledge Sources
Domains NLP, Data_Engineering
Last Updated 2026-02-08 00:00 GMT

Overview

A batching technique that pads variable-length sequences to equal length within a batch, using appropriate padding values for inputs and labels.

Description

Batch Data Collation addresses the fundamental requirement of batched neural network training: all sequences in a batch must have the same length. For supervised fine-tuning, this means padding input_ids with the tokenizer's pad_token_id and padding labels with IGNORE_INDEX (-100) so that padding positions do not contribute to the loss.

Usage

Use this principle when training with variable-length sequence data. Pass the collator to the Trainer's data_collator argument.

Theoretical Basis

Given a batch of sequences with varying lengths:

  1. Find the maximum length in the batch.
  2. Pad all input_ids to max length with pad_token_id.
  3. Pad all labels to max length with IGNORE_INDEX (-100).
  4. Stack into batch tensors.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment