Skip to content

cuda.compute: Add ShuffleIterator (redux) - #7721

Merged
shwina merged 6 commits into
NVIDIA:mainfrom
shwina:cuda-compute-shuffle-iterator-redux
Feb 20, 2026
Merged

shwina merged 6 commits into
NVIDIA:mainfrom
shwina:cuda-compute-shuffle-iterator-redux

Conversation

@shwina

@shwina shwina commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #5684.

Following the refactor of iterators to use C++, this PR implements ShuffleIterator using the new approach, superseding #7062.

Like thrust::shuffle_iterator, the implementation uses cuda::random_bijection. Unlike thrust::shuffle_iterator, it uses cuda::pcg64 for the RNG, rather than minstd_rand. @RAMitchell to validate that this is not a bad idea :)

By virtue of implementing the iterators in C++, we can reuse the relevant pieces from libcudacxx, unlike in #7062 where we basically reimplemented the algorithm.

Validation

Using the same validation scripts as in the previous PR, I can confirm that we get statistically uniform distributions (p > 0.05):

validation

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@shwina
shwina requested review from a team as code owners February 19, 2026 10:52
@shwina
shwina requested a review from alliepiper February 19, 2026 10:52
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Feb 19, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Feb 19, 2026
extra_ltoirs=[],
)

def _make_input_deref_op(self) -> Op | None:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@RAMitchell in particular, could you review the dereference method here? Thank you.

The ShuffleState type is defined above near line 20.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be better to make the round keys (rather than the seed) part of the state so we don't have to regenerate them each dereference?
Not sure a if theres a good way to implement this while reusing the existing implementation, unless cuda::random_bijection itself is trivially copyable and can just be the contents of the state object?

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.

I think it is trivially copyable and the needed keys are finite:

uint32_t __keys_[__num_rounds] = {};

One way to do this is to initialize the host state in __init__ and make it part of state_bytes, but I dunno whether this would help improve perf.

@shwina shwina Feb 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, this would essentially need us to build a tiny C++ extension for Python, providing a constructor for cuda::random_bijection objects or to replicate the construction logic using ctypes.

I'll leave a comment in the code that this is a potential optimization we can do, but I'd rather only deal with the complexity if absolutely necessary.

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.

I think a host C++ extension is not necessary; the same struct.pack() trick can be extended to cover the keys too. But it is fine to punt on this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

or to replicate the construction logic using ctypes.

Without an extension, we'd still have this problem.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 implements ShuffleIterator in C++ following the iterator refactor (PR #7441), superseding the previous implementation attempt (PR #7062). The iterator produces deterministic random permutations of indices using cuda::random_bijection with cuda::pcg64 as the RNG engine. This provides a stateless approach to generating random permutations that is more memory-efficient than allocating temporary arrays.

Changes:

  • Added ShuffleIterator implementation in _shuffle.py with C++ NVRTC-compiled advance and dereference operations
  • Added comprehensive tests covering bijectivity, determinism, edge cases, and integration with PermutationIterator
  • Added example demonstrating usage with PermutationIterator to shuffle array data

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
python/cuda_cccl/cuda/compute/iterators/_shuffle.py New ShuffleIterator implementation using cuda::pcg64 and cuda::random_bijection
python/cuda_cccl/cuda/compute/iterators/init.py Export ShuffleIterator from iterators module
python/cuda_cccl/cuda/compute/init.py Export ShuffleIterator from main cuda.compute module
python/cuda_cccl/tests/compute/test_shuffle_iterator.py Comprehensive tests for bijectivity, determinism, various sizes, and integration
python/cuda_cccl/tests/compute/examples/iterator/shuffle_iterator_basic.py Example demonstrating usage with PermutationIterator
Comments suppressed due to low confidence (1)

python/cuda_cccl/tests/compute/examples/iterator/shuffle_iterator_basic.py:27

  • The comment refers to "d_out" but the variable is named "d_output". This should be corrected to maintain consistency.
# Use unary_transform to write values into d_out

@github-actions

This comment has been minimized.

@shwina
shwina force-pushed the cuda-compute-shuffle-iterator-redux branch from f6885a6 to d7ee022 Compare February 19, 2026 12:16
@github-actions

This comment has been minimized.

{_SHUFFLE_STATE_STRUCT}

extern "C" __device__ void {symbol}(void* state, void* offset) {{
auto* s = static_cast<ShuffleState*>(state);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto* s = static_cast<ShuffleState*>(state);
auto s = static_cast<ShuffleState*>(state);

Comment on lines +83 to +84
auto dist = *static_cast<uint64_t*>(offset);
s->current_index += static_cast<int64_t>(dist);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we do the following instead?

Suggested change
auto dist = *static_cast<uint64_t*>(offset);
s->current_index += static_cast<int64_t>(dist);
auto dist = *static_cast<int64_t*>(offset);
s->current_index += dist;

auto dist = *static_cast<uint64_t*>(offset);
s->current_index += static_cast<int64_t>(dist);
}}
""").strip()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the purpose of using strip() to make debug prints nicer? Should .strip() be removed before merging?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I suppose we don't need it. Removed.

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks @shwina

@github-actions

This comment has been minimized.

// causes spilling to local memory (LDL/STL instructions).
// Keeping it non-inlined gives it an isolated register frame.
__device__ __noinline__ int64_t __shuffle_apply(uint64_t num_items, uint64_t seed, uint64_t idx) {{
cuda::pcg64 rng(seed);

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.

My wishlist is we could bring in our own RNG, but at this point I am asking too much! :D

@shwina
shwina enabled auto-merge (squash) February 20, 2026 11:12
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 20m: Pass: 100%/56 | Total: 17h 58m | Max: 55m 22s

See results here.

@shwina
shwina merged commit d220c43 into NVIDIA:main Feb 20, 2026
71 of 74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

cuda.cccl.parallel: Add shuffle_iterator

6 participants