Repository navigation
cuda.compute: Add ShuffleIterator (redux) - #7721
Conversation
| extra_ltoirs=[], | ||
| ) | ||
|
|
||
| def _make_input_deref_op(self) -> Op | None: |
There was a problem hiding this comment.
@RAMitchell in particular, could you review the dereference method here? Thank you.
The ShuffleState type is defined above near line 20.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I think it is trivially copyable and the needed keys are finite:
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
or to replicate the construction logic using ctypes.
Without an extension, we'd still have this problem.
There was a problem hiding this comment.
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
ShuffleIteratorimplementation in_shuffle.pywith C++ NVRTC-compiled advance and dereference operations - Added comprehensive tests covering bijectivity, determinism, edge cases, and integration with
PermutationIterator - Added example demonstrating usage with
PermutationIteratorto 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
This comment has been minimized.
This comment has been minimized.
f6885a6 to
d7ee022
Compare
This comment has been minimized.
This comment has been minimized.
| {_SHUFFLE_STATE_STRUCT} | ||
|
|
||
| extern "C" __device__ void {symbol}(void* state, void* offset) {{ | ||
| auto* s = static_cast<ShuffleState*>(state); |
There was a problem hiding this comment.
| auto* s = static_cast<ShuffleState*>(state); | |
| auto s = static_cast<ShuffleState*>(state); |
| auto dist = *static_cast<uint64_t*>(offset); | ||
| s->current_index += static_cast<int64_t>(dist); |
There was a problem hiding this comment.
Can we do the following instead?
| 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() |
There was a problem hiding this comment.
Is the purpose of using strip() to make debug prints nicer? Should .strip() be removed before merging?
There was a problem hiding this comment.
Yes, I suppose we don't need it. Removed.
oleksandr-pavlyk
left a comment
There was a problem hiding this comment.
LGTM! Thanks @shwina
This comment has been minimized.
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); |
There was a problem hiding this comment.
My wishlist is we could bring in our own RNG, but at this point I am asking too much! :D
🥳 CI Workflow Results🟩 Finished in 1h 20m: Pass: 100%/56 | Total: 17h 58m | Max: 55m 22sSee results here. |
Description
Closes #5684.
Following the refactor of iterators to use C++, this PR implements
ShuffleIteratorusing the new approach, superseding #7062.Like
thrust::shuffle_iterator, the implementation usescuda::random_bijection. Unlikethrust::shuffle_iterator, it usescuda::pcg64for the RNG, rather thanminstd_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):Checklist