-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of np.unique(return_index=True) #1138
base: branch-24.03
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
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) | ||
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: | ||
|
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: { | ||
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; | ||
|
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.