Skip to content
Back to skills

Dynamic Batch Padding

ASecurity

Pads each batch to its actual max sequence length instead of the global max_len, reducing wasted computation.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
datapython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill dynamic-batch-padding --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Dynamic Batch Padding?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Dynamic Batch Padding
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-dynamic-batch-padding/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-dynamic-batch-padding)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-dynamic-batch-padding
description: >
  Pads each batch to its actual max sequence length instead of the global max_len, reducing wasted computation.
---
# Dynamic Batch Padding

## Overview

Standard padding pads all sequences to `max_len` (e.g., 512 or 1024). Dynamic batch padding pads only to the longest sequence in each batch, saving significant GPU memory and computation — especially when most sequences are much shorter than the limit.

## Quick Start

```python
def collate_fn(batch):
    """Custom collator that trims padding to batch max length."""
    inputs = default_collate(batch)
    max_len = int(inputs["attention_mask"].sum(dim=1).max())
    for key in ["input_ids", "attention_mask", "token_type_ids"]:
        if key in inputs:
            inputs[key] = inputs[key][:, :max_len]
    return inputs
```

## Workflow

1. Tokenize with a generous `max_length` (covers longest possible input)
2. In the collate function, find actual max attention length in the batch
3. Trim all tensors to that length
4. Optionally sort dataset by length first for even better packing

## Key Decisions

- **Sort by length**: Grouping similar-length sequences minimizes padding waste further
- **Combine with adaptive max_len**: Set global max from data distribution, not a fixed number
- **Memory savings**: 30-50% reduction on datasets with high length variance
- **Compatibility**: Works with any HuggingFace tokenizer output

## References

- Feedback Prize - English Language Learning (Kaggle)
- Source: [fb3-deberta-v3-base-baseline-train](https://www.kaggle.com/code/yasufuminakama/fb3-deberta-v3-base-baseline-train)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.