Repository navigation
Avoid allocating an intermediate array in cupy.random.choice - #9570
Conversation
|
Discovered a bug locally. Let's see if Copilot can spot it 🙂 |
There was a problem hiding this comment.
Pull request overview
This PR attempts to optimize cupy.random.choice for the replace=False case when a is an integer by implementing a Feistel bijection to avoid allocating an intermediate array. The approach uses a cryptographic Feistel network to create a random permutation of indices without materializing the full array.
Key changes:
- Implements a
FeistelBijectionclass in_generator_api.pyxbased on libcudacxx's Feistel bijection algorithm - Adds a CUDA kernel that applies the Feistel cipher to generate permuted indices
- Modifies
choice()in_generator.pyto use the new bijection approach when sampling without replacement from an integer range
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 19 comments.
| File | Description |
|---|---|
| cupy/random/_generator_api.pyx | Adds FeistelBijection class with C struct, kernel implementation, and helper functions for memory-efficient random sampling |
| cupy/random/_generator.py | Integrates FeistelBijection into the choice() method for the replace=False case with integer populations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment was marked as resolved.
This comment was marked as resolved.
775783e to
54f60d1
Compare
cupy.random.choicecupy.random.choice
|
/test mini |
|
cc @asi1024 for vis (in case you have time to take a look 🙂) |
Co-authored-by: leofang <[email protected]>
| kernel = self.get_kernel() | ||
|
|
||
| # Allocate output | ||
| # TODO(leofang): check if this equals to dtype='l' on Windows |
There was a problem hiding this comment.
Hmmmmpf, this should not be the case. OTOH, the new generator always returns 64bit...
I can see an argument in diverging from NumPy here and just using 64bit integers always.
There was a problem hiding this comment.
Ahh ok I see what you meant:
- With the old RNG, on Windows NumPy returns int32
- With the new RNG, on Windows NumPy returns int64
It seems CuPy shares the implementation for both old/new RNG. I think it is the best to align and mark this PR as no-compat indeed.
9205055 to
db106f8
Compare
| cdef object _feistel_bijection_with_cutoff_kernel = None | ||
|
|
||
|
|
||
| cdef inline uint64_t get_cipher_bits(uint64_t m) nogil noexcept: |
There was a problem hiding this comment.
I think we should write tests that the bijection size is as exected, including edge cases e.g. m=0.
seberg
left a comment
There was a problem hiding this comment.
LGTM as it is, the only real thing is that windows long issue...
|
/test mini |
|
@seberg looks like I invalidated your approval so we need to approve again lol |
seberg
left a comment
There was a problem hiding this comment.
LGTM, the change to int64 on windows seems sane (NumPy is just very conservative here).
Avoid allocating an intermediate array in `cupy.random.choice`
|
@leofang I'll mark it as no-compat. It is a smaller change (the 32->64bit is windows only). It is a random stream change, though. So it could be added to the final release notes. |
Turns out we can't use
cuda::shuffle_iteratororthrust::shuffle_iteratorfor a few reasons:The solution here is arguably very simple: We initialize the state (a POD struct) on host and pass it to the device as a kernel argument. Then, the bijection device function can be invoked straightforwardly.