Skip to main content

cut2min-bucket

A PyTorch Batch Sampler that buckets by input length and cuts to min size in batch

PyPi Version GitHub license

This package provides 2 utilities:

  1. cut2min_bucket.DatasetWrapper to eliminate padding and cut to min size in batch
  2. cut2min_bucket.BucketBatchSampler a batch sampler that buckets by input length.

In addition, we provide a Distributed Data Parallel version of the batch sampler: cut2min_bucket.DistributedBucketBatchSampler.

A detailed motivation for this package can be found on my blog.

Simple example:

import cut2min_bucket
import torch
import numpy as np

X = []
for _ in range(10000):
    X.append(torch.tensor(np.random.randn(torch.randint(size=(), low=2, high=1000),)))

seqlens = torch.tensor([len(x) for x in X])

X = torch.nn.utils.rnn.pad_sequence(X, batch_first=True)
y = (torch.rand(10000)>0.5).int()

dataset = torch.utils.data.TensorDataset(X, y)

dataset = cut2min_bucket.DatasetWrapper(
    dataset, seqlens,
    index_or_key=0
)

batch_sampler = cut2min_bucket.BucketBatchSampler(
    dataset,
    seqlens,
    batch_size=8,
    n_partitions=5
)

dataloader = torch.utils.data.DataLoader(
    dataset,
    batch_sampler=batch_sampler,
    collate_fn=dataset.collate_fn,
)

next(iter(dataloader))

Metadata

Release files for cut2min-bucket 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cut2min-bucket 0.1.3
File Size Uploaded
cut2min_bucket-0.1.3.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cut2min-bucket 0.1.3
File Interpreter ABI Platform
cut2min_bucket-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / cut2min_bucket-0.1.3.tar.gz

Download URL cut2min_bucket-0.1.3.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
92c039ed45fdbcab29ce3d390a76623b70afed6c071bdff1eac3f758dae54095
BLAKE2b-256 checksum
How to use checksums
a331596dd32e1ffc0c13212bb508294dc2388278c583be3f2faecd879e04162f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / cut2min_bucket-0.1.3-py3-none-any.whl

Download URL cut2min_bucket-0.1.3-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1df4c7c3c00c74c2e28c27b89d308f9786b1d40f7c2c22b67c41217a29d40581
BLAKE2b-256 checksum
How to use checksums
269d5b56814174d6f7717c4b761ecf2e3742fdb4809200cc19efb9bc3fc0e474
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page