Skip to content

Avoid allocating an intermediate array in cupy.random.choice - #9570

Merged
leofang merged 22 commits into
cupy:mainfrom
leofang:bijection_choice
Jan 17, 2026
Merged

leofang merged 22 commits into
cupy:mainfrom
leofang:bijection_choice

Conversation

@leofang

@leofang leofang commented Jan 5, 2026 •

Copy link
Copy Markdown
Member

Turns out we can't use cuda::shuffle_iterator or thrust::shuffle_iterator for a few reasons:

  1. They are not available in CCCL 3.1.2 that we just updated to
  2. Using them as-is is challenging because
    • We need pre-compiled kernels + C/Cython bindings (also noted in the inline comment)
    • We need to figure out a way to influence its RNG there
  3. Both have serious bugs that just got fixed upstream (I propagated the fixes to here)
    • uniform randomness
    • cipher bit initialization

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.

@leofang leofang linked an issue Jan 5, 2026 that may be closed by this pull request
@leofang leofang added this to the v15 milestone Jan 5, 2026
Comment thread cupy/random/_generator_api.pyx Resolved
@leofang

leofang commented Jan 5, 2026

Copy link
Copy Markdown
Member Author

Discovered a bug locally. Let's see if Copilot can spot it 🙂

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FeistelBijection class in _generator_api.pyx based on libcudacxx's Feistel bijection algorithm
  • Adds a CUDA kernel that applies the Feistel cipher to generate permuted indices
  • Modifies choice() in _generator.py to 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.

Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator.py Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator.py Resolved
Comment thread cupy/random/_generator.py Resolved
Comment thread cupy/random/_generator_api.pyx Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator.py Resolved
@leofang

This comment was marked as resolved.

@leofang leofang changed the title WIP: Avoid allocating an intermediate array in cupy.random.choice Avoid allocating an intermediate array in cupy.random.choice Jan 12, 2026
@leofang
leofang marked this pull request as ready for review January 15, 2026 03:41
@leofang
leofang requested a review from a team as a code owner January 15, 2026 03:41
@leofang leofang added cat:enhancement Improvements to existing features to-be-backported Pull-requests to be backported to stable branch prio:medium labels Jan 15, 2026
@leofang

leofang commented Jan 15, 2026

Copy link
Copy Markdown
Member Author

/test mini

Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator_api.pyx Resolved
Comment thread cupy/random/_generator.py Resolved
@leofang

leofang commented Jan 15, 2026

Copy link
Copy Markdown
Member Author

cc @asi1024 for vis (in case you have time to take a look 🙂)

kernel = self.get_kernel()

# Allocate output
# TODO(leofang): check if this equals to dtype='l' on Windows

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@RAMitchell RAMitchell left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feistel code looks correct.

Comment thread cupy/random/_generator_api.pyx Resolved
cdef object _feistel_bijection_with_cutoff_kernel = None


cdef inline uint64_t get_cipher_bits(uint64_t m) nogil noexcept:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should write tests that the bijection size is as exected, including edge cases e.g. m=0.

seberg
seberg previously approved these changes Jan 16, 2026

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as it is, the only real thing is that windows long issue...

@leofang

leofang commented Jan 16, 2026

Copy link
Copy Markdown
Member Author

/test mini

@leofang
leofang requested a review from seberg January 16, 2026 23:04
@leofang
leofang enabled auto-merge January 16, 2026 23:05
@leofang

leofang commented Jan 17, 2026

Copy link
Copy Markdown
Member Author

@seberg looks like I invalidated your approval so we need to approve again lol

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the change to int64 on windows seems sane (NumPy is just very conservative here).

@leofang
leofang merged commit 0362252 into cupy:main Jan 17, 2026
chainer-ci pushed a commit to chainer-ci/cupy that referenced this pull request Jan 17, 2026
Avoid allocating an intermediate array in `cupy.random.choice`
@seberg seberg added the no-compat Disrupts backward compatibility label Jan 17, 2026
@seberg

seberg commented Jan 17, 2026

Copy link
Copy Markdown
Member

@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.

@leofang
leofang deleted the bijection_choice branch January 17, 2026 21:13
@leofang leofang modified the milestones: v15, v15.0.0a1 Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:enhancement Improvements to existing features no-compat Disrupts backward compatibility prio:medium to-be-backported Pull-requests to be backported to stable branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce memory usage of cupy.random.choice(N, replace=False)

5 participants