Bucket batching seq2seq

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

Bucket batching seq2seq is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Group variable-length sequences into length-sorted buckets before batching to minimize padding waste during seq2seq inference

How to use this skill

Skill file

Bucket Batching for Seq2Seq

Overview

Naive batching pads all sequences to the longest in the batch, wasting compute on short inputs. Sort by length, split into buckets, then batch within each bucket. Reduces padding by 30-60% and stabilizes GPU memory usage.

Quick Start

from torch.utils.data import Sampler
import numpy as np

class BucketBatchSampler(Sampler):
    def __init__(self, lengths, batch_size, num_buckets=8, shuffle=False):
        sorted_idx = sorted(range(len(lengths)), key=lambda i: lengths[i])
        bsize = max(1, len(sorted_idx) // num_buckets)
        self.batches = []
        for b in range(num_buckets):
            start = b * bsize
            end = None if b == num_buckets - 1 else (b + 1) * bsize
            bucket = sorted_idx[start:end]
            if shuffle:
                np.random.shuffle(bucket)
            for i in range(0, len(bucket), batch_size):
                self.batches.append(bucket[i:i + batch_size])

    def __iter__(self):
        return iter(self.batches)

    def __len__(self):
        return len(self.batches)

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.