Skip to content
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

Steps toward full compatibility with sklearnsklearn.utils.validation.check_array: missing default __array_namespace__ and dpt.isfinite #997

Closed
fcharras opened this issue Nov 21, 2022 · 3 comments

Comments

@fcharras
Copy link
Contributor

Some work has been started on sklearn to interface estimators that are fully written using only typical array functions with array libraries that implement the Array API, see scikit-learn/scikit-learn#22554

In this effort, the default data validation function sklearn.utils.validation.check_array has been adapted to all array libraries that implement the array API.

For the plugin system we're considering at scikit-learn/scikit-learn#24497 along with our plugin at https://github.com/soda-inria/sklearn-numba-dpex I've found that it could be interesting to re-use sklearn.utils.validation.check_array on usm_ndarray inputs with sklearn validation rules and it might also prevent unnecessary data copies.

I've found that currently a usm_ndarray will fail the check_array for those two reasons:

  • requires dpctl.tensor.isfinite to be implemented
  • requires the .__array_namespace__ attribute of usm_ndarray array to return dpctl.tensor rather than None

In the meantime it's possible to work around those two missing features with:

import dpctl.tensor as dpt
from sklearn.utils.validation import check_array
from sklearn import config_context

array = dpctl.tensor.asarray(np.arange(12).reshape(4,3))
with config_context(array_api_dispatch=True, assume_finite=True):    # workaround 1: assume_finite=True
    tensor._set_namespace(dpt)    # workaround 2: manually call ._set_namespace
    checked_array = check_array(array, accept_sparse=False, dtype=[np.float32, np.float64], order="C", copy=False, force_all_finite=True)
@oleksandr-pavlyk
Copy link
Collaborator

Since gh-1324, dpctl.tensor.usm_ndarray.__array_namespace__ returns dpctl.tensor by default.

dpctl.tensor.isfinite has been implemented for some time as well:

In [1]: import dpctl.tensor as dpt

In [2]: x = dpt.ones(10, dtype=dpt.float64)

In [3]: x
Out[3]: usm_ndarray([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [4]: dpt.isfinite(_)
Out[4]:
usm_ndarray([ True,  True,  True,  True,  True,  True,  True,  True,
              True,  True])

In [5]: x + 2
Out[5]: usm_ndarray([3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])

In [6]: 'isfinite' in dir(x.__array_namespace__())
Out[6]: True

@oleksandr-pavlyk
Copy link
Collaborator

@fcharras Ok to close?

@fcharras
Copy link
Contributor Author

Yes, thank you @oleksandr-pavlyk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants