Skip to main content

Fast Sampling from Categorical Distributions on the GPU using PyTorch

Currently, the pytorch.distributions.Categorical is a bit slow if you need to draw a large number of samples from a static categorical distribution. Also, you are limited to having no more than 2^24 different outcomes.

The "alias method" let's you sample very quickly from distributions with large support, and this implementation in PyTorch let's you have more than 2^24 outcomes.

I needed this for rapid generation of word embeddings in hilbert.

Install

pip install pytorch-categorical

Use

import pytorch_categorical
import torch

num_outcomes = int(1e6)
probs = torch.random(num_outcomes)
probs /= probs.sum()

sampler = pytorch_categorical.Categorical(probs)

num_samples = int(1e6)
samples = sampler.sample((num_samples,))

The constructor also takes a dtype and a device if you want to specify them. By default

Posterity

At the time I made this, there was an open issue to incorporate a more rapid sampler based on the alias method, but nothing was released yet. Hopefully that will get into a release soon! For now, use this!

Tested. It's Correct and Fast.

I've backed this by a few simple tests, including a benchmark against torch. This implementation takes about 175X longer to construct a sampler with one million outcomes, but after this up-front cost, it yields (draws of ten thousand) samples about 3000X faster (with greater advantage the more samples that are eventually drawn). So the main usecase is when you have to draw many samples from a stable distribution.

Run the correctness and benchmark tests: python test.py.

Enjoy!

Metadata

Release files for pytorch-categorical 0.0.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 pytorch-categorical 0.0.3
File Size Uploaded Provenance
pytorch-categorical-0.0.3.tar.gz 4.3 kB Without provenance Details

Release files / pytorch-categorical-0.0.3.tar.gz

Download URL pytorch-categorical-0.0.3.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d71c6d93da62cab4d2bd57d1ea58105ba1354bf14e85a9b94ca4e9c506bf93cb
BLAKE2b-256 checksum
How to use checksums
b0b88b82b5c66bef1fdfd8b6c5241fc175cf5e9d9ac33fec7aebeb8dda796177
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.9.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Security for pytorch-categorical 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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