-
Notifications
You must be signed in to change notification settings - Fork 31
Implement tensor.isin
#2098
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
Merged
Merged
Implement tensor.isin
#2098
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
518fd6a
Implement tensor.isin
ndgrigorian 85106f4
factor common utilities for scalar arguments to a new file
ndgrigorian ce757c8
Make constexpr variables in `isin` static
ndgrigorian 8425cdc
Update implementation of `isin`
ndgrigorian 204025a
Update per review comments
ndgrigorian cfb5154
Allow x to be a scalar in isin and remove assume_unique
ndgrigorian a448725
Make comparator static constexpr
ndgrigorian 882a431
add basic tests for isin functionality
ndgrigorian d11626c
Remove unused import of dpctl in _set_functions.py
ndgrigorian 710a6d0
Add type hints to isin
ndgrigorian 411b976
Add usm_type to test_buf in isin
ndgrigorian 3077bd6
Address review comments for isin tests
ndgrigorian ed9f376
Add test covering nans and +/- 0 in isin
ndgrigorian 72278b5
Add test for isin with Python scalar args
ndgrigorian 5b877c8
Add test for combinations of dtypes as inputs to isin
ndgrigorian 538176c
Add compute follows data test for isin
ndgrigorian 0cd24f5
Add isin to rendered docs
ndgrigorian 0070331
Test that isin output is C-contiguous when input is strided
ndgrigorian 2ebb3a1
improve formatting of isin docstring
ndgrigorian 26f68d2
move rich_comparisons.hpp into utils
ndgrigorian 365c3b4
factor out compare template param in isin
ndgrigorian 44661ba
add missing includes to rich_comparisons
ndgrigorian 62a9fa4
drop unused includes in isin kernel implementation
ndgrigorian ff68bca
validate that `invert` kwarg is boolean
ndgrigorian e28e1aa
add a test for input validation of invert
ndgrigorian 68bc84e
make invert bool type check stricter
ndgrigorian 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ Set Functions | |
.. autosummary:: | ||
:toctree: generated | ||
|
||
isin | ||
unique_all | ||
unique_counts | ||
unique_inverse | ||
|
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Data Parallel Control (dpctl) | ||
# | ||
# Copyright 2020-2025 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import numbers | ||
|
||
import numpy as np | ||
|
||
import dpctl.memory as dpm | ||
import dpctl.tensor as dpt | ||
from dpctl.tensor._usmarray import _is_object_with_buffer_protocol as _is_buffer | ||
|
||
from ._type_utils import ( | ||
WeakBooleanType, | ||
WeakComplexType, | ||
WeakFloatingType, | ||
WeakIntegralType, | ||
_to_device_supported_dtype, | ||
) | ||
|
||
|
||
def _get_queue_usm_type(o): | ||
"""Return SYCL device where object `o` allocated memory, or None.""" | ||
if isinstance(o, dpt.usm_ndarray): | ||
return o.sycl_queue, o.usm_type | ||
elif hasattr(o, "__sycl_usm_array_interface__"): | ||
try: | ||
m = dpm.as_usm_memory(o) | ||
return m.sycl_queue, m.get_usm_type() | ||
except Exception: | ||
return None, None | ||
return None, None | ||
|
||
|
||
def _get_dtype(o, dev): | ||
if isinstance(o, dpt.usm_ndarray): | ||
return o.dtype | ||
if hasattr(o, "__sycl_usm_array_interface__"): | ||
return dpt.asarray(o).dtype | ||
if _is_buffer(o): | ||
host_dt = np.array(o).dtype | ||
dev_dt = _to_device_supported_dtype(host_dt, dev) | ||
return dev_dt | ||
if hasattr(o, "dtype"): | ||
dev_dt = _to_device_supported_dtype(o.dtype, dev) | ||
return dev_dt | ||
if isinstance(o, bool): | ||
return WeakBooleanType(o) | ||
if isinstance(o, int): | ||
return WeakIntegralType(o) | ||
if isinstance(o, float): | ||
return WeakFloatingType(o) | ||
if isinstance(o, complex): | ||
return WeakComplexType(o) | ||
return np.object_ | ||
|
||
|
||
def _validate_dtype(dt) -> bool: | ||
return isinstance( | ||
dt, | ||
(WeakBooleanType, WeakIntegralType, WeakFloatingType, WeakComplexType), | ||
) or ( | ||
isinstance(dt, dpt.dtype) | ||
and dt | ||
in [ | ||
dpt.bool, | ||
dpt.int8, | ||
dpt.uint8, | ||
dpt.int16, | ||
dpt.uint16, | ||
dpt.int32, | ||
dpt.uint32, | ||
dpt.int64, | ||
dpt.uint64, | ||
dpt.float16, | ||
dpt.float32, | ||
dpt.float64, | ||
dpt.complex64, | ||
dpt.complex128, | ||
] | ||
) | ||
|
||
|
||
def _get_shape(o): | ||
if isinstance(o, dpt.usm_ndarray): | ||
return o.shape | ||
if _is_buffer(o): | ||
return memoryview(o).shape | ||
if isinstance(o, numbers.Number): | ||
return tuple() | ||
return getattr(o, "shape", tuple()) | ||
|
||
|
||
__all__ = [ | ||
"_get_dtype", | ||
"_get_queue_usm_type", | ||
"_get_shape", | ||
"_validate_dtype", | ||
] |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.