This repository was archived by the owner on Sep 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Initial implementation of np.unique(return_index=True) #1138
Open
JosephGuman
wants to merge
3
commits into
nv-legate:branch-24.03
Choose a base branch
from
JosephGuman:unique_return_index
base: branch-24.03
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3487,25 +3487,76 @@ def scan( | |
| assert self.shape == swapped.shape | ||
| self.copy(swapped, deep=True) | ||
|
|
||
| def unique(self) -> NumPyThunk: | ||
| result = self.runtime.create_unbound_thunk(self.base.type) | ||
|
|
||
| def unique( | ||
| self, return_index: bool = False | ||
| ) -> Union[NumPyThunk, tuple[NumPyThunk, Optional[NumPyThunk]]]: | ||
| task = self.context.create_auto_task(CuNumericOpCode.UNIQUE) | ||
|
|
||
| task.add_output(result.base) | ||
| task.add_input(self.base) | ||
| task.add_scalar_arg(return_index, ty.bool_) | ||
|
|
||
| result = None | ||
| # Assuming legate core will always choose GPU variant | ||
| # CPU uses legate.core Reduce op, which requires storing indices in struct | ||
| if self.runtime.num_gpus > 0: | ||
| task.add_nccl_communicator() | ||
| result = self.runtime.create_unbound_thunk(self.base.type) | ||
| elif return_index: | ||
| result = self.runtime.create_unbound_thunk( | ||
| ty.struct_type( | ||
| [ | ||
| self.base.type, | ||
| ty.int64, | ||
| ], | ||
| True, | ||
| ) | ||
| ) | ||
| else: | ||
| result = self.runtime.create_unbound_thunk(self.base.type) | ||
| task.add_output(result.base) | ||
|
|
||
| returned_indices = None | ||
| if return_index: | ||
| # GPU variant uses NCCL for reduction so can directly output indices | ||
| if self.runtime.num_gpus > 0: | ||
| returned_indices = self.runtime.create_unbound_thunk(ty.int64) | ||
| task.add_output(returned_indices.base) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, comment this asymmetry between the CPU and GPU implementations |
||
|
|
||
| for i in range(self.ndim): | ||
| task.add_scalar_arg(self.shape[i], ty.int32) | ||
|
|
||
| task.execute() | ||
|
|
||
| if self.runtime.num_gpus == 0 and self.runtime.num_procs > 1: | ||
| result.base = self.context.tree_reduce( | ||
| CuNumericOpCode.UNIQUE_REDUCE, result.base | ||
| ) | ||
| if self.runtime.num_gpus == 0: | ||
| if self.runtime.num_procs > 1: | ||
| result.base = self.context.tree_reduce( | ||
| CuNumericOpCode.UNIQUE_REDUCE, | ||
| result.base, | ||
| scalar_args=[(return_index, ty.bool_)], | ||
| ) | ||
| if return_index: | ||
| task = self.context.create_auto_task(CuNumericOpCode.UNZIP) | ||
| task.add_input(result.base) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an unclear way of writing the code, adding |
||
|
|
||
| return result | ||
| result = self.runtime.create_empty_thunk( | ||
| result.shape, self.base.type | ||
| ) | ||
| returned_indices = self.runtime.create_empty_thunk( | ||
| result.shape, ty.int64 | ||
| ) | ||
|
|
||
| task.add_output(result.base) | ||
|
|
||
| returned_indices = cast(DeferredArray, returned_indices) | ||
| task.add_output(returned_indices.base) | ||
| task.add_alignment(result.base, returned_indices.base) | ||
|
|
||
| task.execute() | ||
|
|
||
| if return_index: | ||
| return result, returned_indices | ||
| else: | ||
| return result | ||
|
|
||
| @auto_convert("rhs", "v") | ||
| def searchsorted(self, rhs: Any, v: Any, side: SortSide = "left") -> None: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,7 +105,8 @@ std::vector<StoreMapping> CuNumericMapper::store_mappings( | |
| } | ||
| case CUNUMERIC_MATMUL: | ||
| case CUNUMERIC_MATVECMUL: | ||
| case CUNUMERIC_UNIQUE_REDUCE: { | ||
| case CUNUMERIC_UNIQUE_REDUCE: | ||
| case CUNUMERIC_UNZIP_INDICES: { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on looking at the implementation of |
||
| // TODO: Our actual requirements are a little less strict than this; we require each array or | ||
| // vector to have a stride of 1 on at least one dimension. | ||
| std::vector<StoreMapping> mappings; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit]: Add a comment as to why the branches here are creating the thunk in different ways (i.e. different types)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra nit -- comments should be complete sentences, so punctuation and periods please.