← Browse

@wenmin-wu/nlp-bucket-batching-seq2seq

B

Group variable-length sequences into length-sorted buckets before batching to minimize padding waste during seq2seq inference

skillclaude

Install

agr install @wenmin-wu/nlp-bucket-batching-seq2seq --target claude

Writes 1 file into .claude/skills/, pinned to git-d355215a.

  • .claude/skills/nlp-bucket-batching-seq2seq/SKILL.md

Document


name: nlp-bucket-batching-seq2seq description: Group variable-length sequences into length-sorted buckets before batching to minimize padding waste during seq2seq inference domain: nlp

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

  • num_buckets=8: enough granularity without over-fragmenting
  • Sort within bucket only: preserves some randomness across epochs if shuffled
  • Restore original order: after inference, unsort results back to input order

References

Trustgrade B

  • passBody integrity

    Whether the stored document is plausibly the kind of file the artifact declares, rather than something fetched by mistake.

  • passType matchnot applicable to this artifact type

    Whether the artifact is really the kind of thing its metadata claims it is.

  • passFreshness

    How long since the source repository was last pushed to.

  • passPrompt injection

    Scans the artifact's own text for instructions aimed at your agent rather than at you.

  • warnLicenseno SPDX license detected

    Whether the source repository declares an SPDX license permissive enough to redistribute.

How the grade is calculated

Each check contributes 0 points when it passes, 1 when it warns, and 2 when it fails. The total maps to a letter:

  • Aevery check passed
  • Bone warning
  • Ctwo warnings
  • Dprompt injection or body integrity failed, or three warnings
  • Fone of those failed, and something else is wrong

These are automated hygiene checks, not a security audit, and not a dependency or vulnerability scan. A grade of A means nothing was flagged — not that the artifact is safe.

Versions

  • git-d355215af6dd2026-07-31