Add inverse-CDF path for large categorical sampling#3864
Open
MicroMilo wants to merge 1 commit into
Open
Conversation
MicroMilo
marked this pull request as ready for review
July 18, 2026 03:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a transform-aware inverse-CDF path for large categorical sampling
Summary
This change adds a source-level
CategoricalSearchprimitive and uses it to avoidthe
O(B*N*M)Gumbel temporary created byrandom::categorical(logits, num_samples=M)for large Metal workloads.Fixes #3847.
The inverse-CDF path uses FP32 softmax probabilities rounded onto a 52-bit uint64
mass grid, an exact uint64 cumulative sum, one uint32 random word per output
sample, and a Metal binary search kernel. CPU and non-Metal backends retain the
existing Gumbel-max path.
Why
Issue #3847 reports that drawing many samples from a large categorical creates a
Gumbel tensor with
B*N*Melements. ForN=M=1,000,000, that formulation wouldrequire roughly 4 TB for the FP32 temporary.
A float32 inverse-CDF prototype reduced memory but failed an adversarial support
test: with one million categories and a 0.99 head probability, only 249,772 CDF
entries had positive increments. The uint64 fixed-point representation preserves
all one million positive weights.
Implementation
CategoricalSearch(cdf[..., N], bits[..., M]) -> uint32[..., M].Contiguousdoesnot leak into exported graphs.
first
+infwins, NaNs are masked when another valid category exists, androws containing only NaN/
-infchoose category zero.num_samplesoverload whenB*N*M >= 2^19.CPU, CUDA, other overloads, and smaller workloads remain unchanged.
Correctness
Performance (Apple M5)
31 interleaved paired measurements, bootstrap 95% CI:
The final
2^19guard selected 23 formal grid cases; all 23 had a paired-speedupCI lower bound above 1.02. Source graph checks confirm that guard-outside and CPU
paths contain the stock
ArgReducegraph and do not constructCategoricalSearch.Compatibility and limitations
Gumbel-max's exact random sequence.
device validation.
cumulative sum but does not provide higher-precision probabilities than FP32.
Tests
python/tests/test_random.py: 17 passed.python/tests/test_export_import.py: 19 passed, 6 subtests passed.