-
Notifications
You must be signed in to change notification settings - Fork 182
[enhancement] enable array_api return values from from_table
#2441
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
Draft
icfaust
wants to merge
38
commits into
uxlfoundation:main
Choose a base branch
from
icfaust:dev/refactor_from_table
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
402f498
startup
icfaust f779aee
refactor from_table
icfaust 048cf58
Update _data_conversion.py
icfaust 8039919
Update _data_conversion.py
icfaust 81b454f
Update data_conversion.hpp
icfaust 7b2c8a5
Update _data_conversion.py
icfaust 4524be9
Update _data_conversion.py
icfaust 6aec73a
Update dbscan.py
icfaust 62a7966
Update dbscan.py
icfaust 227d857
Update _data_conversion.py
icfaust 06761b7
Update _data_conversion.py
icfaust 044441a
Update _data_conversion.py
icfaust 44d2c99
Update base.py
icfaust e6b7532
Update covariance.py
icfaust 278054a
Update linear_model.py
icfaust 9bcb63a
Update logistic_regression.py
icfaust 3b1d96d
Update forest.py
icfaust 9b35c20
Update incremental_linear_model.py
icfaust 8b2f186
Update incremental_linear_model.py
icfaust a9bd2cf
formatting
icfaust 230a4ab
Update __init__.py
icfaust 29d3db8
Update forest.py
icfaust 0714ede
Update covariance.py
icfaust 1053040
Update test_data.py
icfaust dc5ca8d
Update data_conversion.cpp
icfaust 8cc801b
Update _data_conversion.py
icfaust 9bfcb8a
Update data_conversion.cpp
icfaust 8287874
Update data_conversion.cpp
icfaust 5519888
Update _data_conversion.py
icfaust 7db4432
Update data_conversion.cpp
icfaust a55c723
Update _data_conversion.py
icfaust dbfb85c
Update test_data.py
icfaust be918d7
Update test_data.py
icfaust 08fc37a
Update test_data.py
icfaust 2873b6d
Update test_data.py
icfaust a4338b8
formatting
icfaust 486a2c8
clang-formatting
icfaust 3b9ba91
Update _data_conversion.py
icfaust 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
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 |
---|---|---|
|
@@ -34,84 +34,108 @@ def to_table(*args, queue=None): | |
"""Create oneDAL tables from scalars and/or arrays. | ||
|
||
Note: this implementation can be used with scipy.sparse, numpy ndarrays, | ||
DPCTL/DPNP usm_ndarrays and scalars. Tables will use pointers to the | ||
original array data. Scalars and non-contiguous arrays will be copies. | ||
Arrays may be modified in-place by oneDAL during computation. This works | ||
for data located on CPU and SYCL-enabled Intel GPUs. Each array may only | ||
be of a single datatype (i.e. each must be homogeneous). | ||
dpctl/dpnp usm_ndarrays, array API standard arrays, and scalars. Tables | ||
will use pointers to the original array data. Scalars and non-contiguous | ||
arrays will be copies. Arrays may be modified in-place by oneDAL during | ||
computation. This works for data located on CPU and SYCL-enabled Intel GPUs. | ||
Each array may only be of a single datatype (i.e. each must be homogeneous). | ||
|
||
Parameters | ||
---------- | ||
*args : {scalar, numpy array, sycl_usm_ndarray, csr_matrix, or csr_array} | ||
*args : scalar, numpy array, sycl_usm_ndarray, array API standard array, | ||
csr_matrix, or csr_array | ||
arg1, arg2... The arrays should be given as arguments. | ||
|
||
queue : SyclQueue or None, default=None | ||
A dpctl or oneDAL backend python representation of a SYCL Queue or None | ||
|
||
Returns | ||
------- | ||
tables: {oneDAL homogeneous tables} | ||
tables: oneDAL homogeneous tables | ||
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. Format here would need to be:
|
||
""" | ||
return _apply_and_pass(_convert_one_to_table, *args, queue=queue) | ||
|
||
|
||
if backend.is_dpc: | ||
|
||
try: | ||
# try/catch is used here instead of dpep_helpers because | ||
# of circular import issues of _data_conversion.py and | ||
# utils/validation.py. This is a temporary fix until the | ||
# issue with dpnp is addressed, at which point this can | ||
# be removed entirely. | ||
import dpnp | ||
|
||
def _table_to_array(table, xp=None): | ||
# By default DPNP ndarray created with a copy. | ||
# TODO: | ||
# investigate why dpnp.array(table, copy=False) doesn't work. | ||
# Work around with using dpctl.tensor.asarray. | ||
if xp == dpnp: | ||
return dpnp.array(dpnp.dpctl.tensor.asarray(table), copy=False) | ||
else: | ||
return xp.asarray(table) | ||
|
||
except ImportError: | ||
|
||
def _table_to_array(table, xp=None): | ||
return xp.asarray(table) | ||
|
||
def convert_one_from_table(table, sycl_queue=None, sua_iface=None, xp=None): | ||
# Currently only `__sycl_usm_array_interface__` protocol used to | ||
# convert into dpnp/dpctl tensors. | ||
if sua_iface: | ||
if ( | ||
sycl_queue | ||
and sycl_queue.sycl_device.is_cpu | ||
and table.__sycl_usm_array_interface__["syclobj"] is None | ||
): | ||
# oneDAL returns tables with None sycl queue for CPU sycl queue inputs. | ||
# This workaround is necessary for the functional preservation | ||
# of the compute-follows-data execution. | ||
# Host tables first converted into numpy.narrays and then to array from xp | ||
# namespace. | ||
return xp.asarray( | ||
backend.from_table(table), usm_type="device", sycl_queue=sycl_queue | ||
) | ||
else: | ||
return _table_to_array(table, xp=xp) | ||
|
||
return backend.from_table(table) | ||
|
||
else: | ||
|
||
def convert_one_from_table(table, sycl_queue=None, sua_iface=None, xp=None): | ||
# Currently only `__sycl_usm_array_interface__` protocol used to | ||
# convert into dpnp/dpctl tensors. | ||
if sua_iface: | ||
raise RuntimeError( | ||
"SYCL usm array conversion from table requires the DPC backend" | ||
def return_type_constructor(array): | ||
"""generator function for converting oneDAL tables to arrays. | ||
|
||
Note: this implementation will convert any table to numpy ndarrays, | ||
scipy csr_arrays, dpctl/dpnp usm_ndarrays, and array API standard | ||
arrays of designated type. By default, from_table will return numpy | ||
arrays and can only return other types when necessary object | ||
attributes exist (i.e. ``__sycl_usm_array_interface__`` or | ||
``__array_namespace__``). | ||
|
||
Parameters | ||
---------- | ||
array : array-like or None | ||
python object representing an array instance of the return type | ||
for converting oneDAL tables. Arrays are queried for conversion | ||
namespace when of sycl_usm_array type or array API standard type. | ||
When set to None, will return numpy arrays or scipy csr arrays. | ||
|
||
Returns | ||
------- | ||
func : callable | ||
a function which takes in a single table input and returns an array | ||
""" | ||
func = backend.from_table | ||
if isinstance(array, np.ndarray) or array is None: | ||
pass | ||
elif hasattr(array, "__sycl_usm_array_interface__"): | ||
# oneDAL returns tables without sycl queues for CPU sycl queue inputs. | ||
# This workaround is necessary for the functional preservation | ||
# of the compute-follows-data execution. | ||
device = array.sycl_queue | ||
# Its important to note why the __sycl_usm_array_interface__ is | ||
# prioritized: it provides finer-grained control of SYCL queues and the | ||
# related SYCL devices which are generally unavailable via DLPack | ||
# representations (such as SYCL contexts, SYCL sub-devices, etc.). | ||
if hasattr(array, "__array_namespace__"): | ||
xp = array.__array_namespace__() | ||
func = lambda x: ( | ||
xp.asarray(x) | ||
if hasattr(x, "__sycl_usm_array_interface__") | ||
else xp.asarray(backend.from_table(x), device=device) | ||
) | ||
elif hasattr(array, "_create_from_usm_ndarray"): # signifier of dpnp < 0.19 | ||
xp = array._array_obj.__array_namespace__() | ||
from_usm = array._create_from_usm_ndarray | ||
func = lambda x: from_usm( | ||
xp.asarray(x) | ||
if hasattr(x, "__sycl_usm_array_interface__") | ||
else xp.asarray(backend.from_table(x), device=device) | ||
) | ||
return backend.from_table(table) | ||
elif hasattr(array, "__array_namespace__"): | ||
func = array.__array_namespace__().from_dlpack | ||
return func | ||
|
||
|
||
def from_table(*args, sycl_queue=None, sua_iface=None, xp=None): | ||
return _apply_and_pass( | ||
convert_one_from_table, *args, sycl_queue=sycl_queue, sua_iface=sua_iface, xp=xp | ||
) | ||
def from_table(*args, like=None): | ||
"""Create 2 dimensional arrays from oneDAL tables. | ||
|
||
Note: this implementation will convert any table to numpy ndarrays, | ||
scipy csr_arrays, dpctl/dpnp usm_ndarrays, and array API standard | ||
arrays of designated type. By default, from_table will return numpy | ||
arrays and can only return other types when necessary object | ||
attributes exist (i.e. ``__sycl_usm_array_interface__`` or | ||
``__array_namespace__``). | ||
|
||
Parameters | ||
---------- | ||
*args : single or multiple python oneDAL tables | ||
arg1, arg2... The arrays should be given as arguments. | ||
|
||
like : callable, array-like or None, default=None | ||
python object representing an array instance of the return type | ||
or function capable of converting oneDAL tables into arrays of | ||
desired type. Arrays are queried for conversion namespace when | ||
of sycl_usm_array type or array API standard type. When set to | ||
None, will return numpy arrays or scipy csr arrays. | ||
|
||
Returns | ||
------- | ||
arrays : numpy arrays, sycl_usm_ndarrays, or array API standard arrays | ||
""" | ||
func = like if callable(like) else return_type_constructor(like) | ||
return _apply_and_pass(func, *args) |
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
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.
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.
Is
sycl_usm_ndarray
this specific class? https://intelpython.github.io/dpctl/latest/api_reference/dpctl/generated/dpctl.tensor.usm_ndarray.html