KEMBAR78
Add segmented_reduce python api by oleksandr-pavlyk · Pull Request #3906 · NVIDIA/cccl · GitHub
Skip to content

Conversation

@oleksandr-pavlyk
Copy link
Contributor

Description

closes gh-3715

This PR adds Python API for segmented_reduce algorithm.

Checklist

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

Also avoid recomputing cccl_value of init in both segmented_reduce
and in reduce
1. Include np.complex64
2. Device output size in a variable and reuse it to avoid repeated
   occurrances of literal values
3. Generate real/imag values for complex arrays in a single
   call to sampling function for efficiency
4. Change range of generated integral arrays based on the signness
   of the integral data type. For unsigned types we continue to
   sample in interval [0, 10), for signed we sample from [-5, 5].
@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Feb 21, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

…e base

Additionally, changed the __hash__ of IteratorKind to mix the hash of
its value with hash of self.__class__.
This is used to advance a given iterator `it` the `offset` steps without
running into multiple definitions of the advance/derefence methods.
This calls IteratorBase.__add__ to produce an iterator whose state
is advanced by 1, but which shares the same advance/dereference methods.
@oleksandr-pavlyk oleksandr-pavlyk marked this pull request as ready for review February 24, 2025 22:58
@oleksandr-pavlyk oleksandr-pavlyk requested a review from a team as a code owner February 24, 2025 22:58
@github-actions
Copy link
Contributor

🟩 CI finished in 40m 15s: Pass: 100%/1 | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
  • 🟩 python: Pass: 100%/1 | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 40m 15s | Avg: 40m 15s | Max: 40m 15s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

@oleksandr-pavlyk oleksandr-pavlyk force-pushed the add-segmented-reduce-python-api branch from 73e2154 to ed864d7 Compare February 25, 2025 18:07
@github-actions
Copy link
Contributor

🟩 CI finished in 40m 27s: Pass: 100%/1 | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
  • 🟩 python: Pass: 100%/1 | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 40m 27s | Avg: 40m 27s | Max: 40m 27s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

Also make generation of complex array in test_reduce.py more
efficient by genering real and imaginary components in a single
call to np.random.random instead of using two calls.
These were only defined for TransformIterator and AdvancedIterator classes,
but not for other classes.

Implemented review suggestion to type type(self) instead of self.__class__
…cumulation

For short range data types we take a small slice of the input array to
avoid running into the overflow problem. This works because input_array
fixture samples from uniform discrete distribution with small upper range (8),
hence using 31 uint8 elements can run up to 31 * 7  = 217 ( < 255) and fits
in the type.
@github-actions
Copy link
Contributor

🟩 CI finished in 40m 55s: Pass: 100%/1 | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
  • 🟩 python: Pass: 100%/1 | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 40m 55s | Avg: 40m 55s | Max: 40m 55s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

This finds compute capability and include paths and appends them
to the algorithm-specific arguments. Used the utility in segmented_reduce.
Copy link
Contributor

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

I really like your last commit 13d8d19!

@github-actions
Copy link
Contributor

🟩 CI finished in 52m 20s: Pass: 100%/1 | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
  • 🟩 python: Pass: 100%/1 | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 52m 20s | Avg: 52m 20s | Max: 52m 20s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

@github-actions
Copy link
Contributor

🟩 CI finished in 51m 12s: Pass: 100%/1 | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
  • 🟩 python: Pass: 100%/1 | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 51m 12s | Avg: 51m 12s | Max: 51m 12s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

@github-actions
Copy link
Contributor

🟩 CI finished in 52m 35s: Pass: 100%/1 | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
  • 🟩 python: Pass: 100%/1 | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s

    🟩 cpu
      🟩 amd64              Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 ctk
      🟩 12.8               Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 cudacxx
      🟩 nvcc12.8           Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 cudacxx_family
      🟩 nvcc               Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 cxx
      🟩 GCC13              Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 cxx_family
      🟩 GCC                Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 gpu
      🟩 rtx2080            Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    🟩 jobs
      🟩 Test               Pass: 100%/1   | Total: 52m 35s | Avg: 52m 35s | Max: 52m 35s
    

👃 Inspect Changes

Modifications in project?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

Modifications in project or dependencies?

Project
CCCL Infrastructure
libcu++
CUB
Thrust
CUDA Experimental
+/- python
CCCL C Parallel Library
Catch2Helper

🏃‍ Runner counts (total jobs: 1)

# Runner
1 linux-amd64-gpu-rtx2080-latest-1

Copy link
Contributor

@shwina shwina left a comment

Choose a reason for hiding this comment

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

Looks great - thank you, Sasha!

@shwina shwina merged commit 0183959 into NVIDIA:main Feb 28, 2025
16 of 19 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in CCCL Feb 28, 2025
@oleksandr-pavlyk oleksandr-pavlyk deleted the add-segmented-reduce-python-api branch February 28, 2025 16:44
oleksandr-pavlyk added a commit to oleksandr-pavlyk/cccl that referenced this pull request Feb 28, 2025
davebayer pushed a commit to davebayer/cccl that referenced this pull request Mar 12, 2025
davebayer pushed a commit to davebayer/cccl that referenced this pull request Apr 7, 2025
* Add algorithms.segmented_reduce Python API

Also avoid recomputing cccl_value of init in both segmented_reduce
and in reduce

* Change to input_array fixture

1. Include np.complex64
2. Device output size in a variable and reuse it to avoid repeated
   occurrances of literal values
3. Generate real/imag values for complex arrays in a single
   call to sampling function for efficiency
4. Change range of generated integral arrays based on the signness
   of the integral data type. For unsigned types we continue to
   sample in interval [0, 10), for signed we sample from [-5, 5].

* Corrected docstring of segmented_reduce function

* Add initial tests for segmented_reduce

* Improve readability of test_segmented_reduce_api example

* TransformIteratorKind need not override __eq__/__hash__ methods of the base

Additionally, changed the __hash__ of IteratorKind to mix the hash of
its value with hash of self.__class__.

* Add AdvancedIterator(it, offset=1) function

This is used to advance a given iterator `it` the `offset` steps without
running into multiple definitions of the advance/derefence methods.

* Add example for summing rows of a matrix using segmented_reduce

* Implement IteratorBase.__add__(self, offset : int) using make_advanced_iterator

* Use end_offsets = start_offsets + 1

This calls IteratorBase.__add__ to produce an iterator whose state
is advanced by 1, but which shares the same advance/dereference methods.

* Add a test for segmented_reduce on gpu_struct

* Change hash of transform iterator to mix its kind

* Rename variable n to sample_size

Also make generation of complex array in test_reduce.py more
efficient by genering real and imaginary components in a single
call to np.random.random instead of using two calls.

* Remove __hash__ and __eq__ special methods from some iterator classes

These were only defined for TransformIterator and AdvancedIterator classes,
but not for other classes.

Implemented review suggestion to type type(self) instead of self.__class__

* Tweak test_scan_array_input to avoid integer overflows during host accumulation

For short range data types we take a small slice of the input array to
avoid running into the overflow problem. This works because input_array
fixture samples from uniform discrete distribution with small upper range (8),
hence using 31 uint8 elements can run up to 31 * 7  = 217 ( < 255) and fits
in the type.

* Add cccl.set_cccl_iterator_state utility function and use in segmented_reduce.py

* Introduce _bindings.call_build utility

This finds compute capability and include paths and appends them
to the algorithm-specific arguments. Used the utility in segmented_reduce.

* Make call_build take *args, **kwargs
davebayer pushed a commit to davebayer/cccl that referenced this pull request Apr 7, 2025
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.

Add Python wrappers for segmented reduce

3 participants