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

ENH: initial _array_api module + for dpnp/dpctl extension #1813

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
58faa0f
ENH: inital array_api module + for dpnp/dpctl extension
samir-nasibli Apr 26, 2024
88369e6
lint fix
samir-nasibli Apr 26, 2024
affb86c
minor update
samir-nasibli Apr 27, 2024
a5691c5
update sklearnex/dispatcher.py
samir-nasibli Apr 29, 2024
008cb52
typo fix
samir-nasibli Apr 29, 2024
9a0166e
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli Apr 29, 2024
4db263f
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli May 7, 2024
00f7a8d
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli May 13, 2024
8c30212
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli May 17, 2024
28962e3
added _array_api module for onedal4py
samir-nasibli May 17, 2024
ee1e957
minor refactoring and fixes
samir-nasibli May 17, 2024
b2693b3
refactor get_namespace sklearn/onedal4py
samir-nasibli May 17, 2024
c4a4dcd
minor fixes
samir-nasibli May 17, 2024
f252047
minor fix for incremental PCA
samir-nasibli May 17, 2024
b19decc
tests from get_namespace
samir-nasibli May 17, 2024
fa39c37
fix test_get_namespace_with_patching
samir-nasibli May 18, 2024
d7f5010
update requirements-test.txt
samir-nasibli May 18, 2024
caf3029
Merge branch 'main' into enh/dpnp_dpctl_get_namespace
samir-nasibli May 31, 2024
6a5aec4
array api make2d
samir-nasibli Jun 1, 2024
c9021b8
array api DBSCAN enabled
samir-nasibli Jun 2, 2024
d8d6594
minor
samir-nasibli Jun 6, 2024
d294ae5
Merge branch 'master' into enh/dpnp_dpctl_get_namespace
samir-nasibli Jun 6, 2024
22cfb8f
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli Jun 10, 2024
826554a
Merge branch 'intel:main' into enh/dpnp_dpctl_get_namespace
samir-nasibli Jun 11, 2024
488fa14
Merge branch 'main' into enh/dpnp_dpctl_get_namespace
samir-nasibli Jun 14, 2024
6a78b2c
Merge branch 'main' into enh/dpnp_dpctl_get_namespace
samir-nasibli Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sklearnex/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def get_patch_map_core(preview=False):
import sklearn.neighbors as neighbors_module
import sklearn.svm as svm_module

if sklearn_check_version("1.4"):
import sklearn.utils._array_api as _array_api_module

if sklearn_check_version("1.2.1"):
import sklearn.utils.parallel as parallel_module
else:
Expand Down Expand Up @@ -134,6 +137,12 @@ def get_patch_map_core(preview=False):
from .svm import NuSVC as NuSVC_sklearnex
from .svm import NuSVR as NuSVR_sklearnex

if sklearn_check_version("1.4"):
from .utils._array_api import _convert_to_numpy as _convert_to_numpy_sklearnex
from .utils._array_api import (
yield_namespace_device_dtype_combinations as yield_namespace_device_dtype_combinations_sklearnex,
)

# DBSCAN
mapping.pop("dbscan")
mapping["dbscan"] = [[(cluster_module, "DBSCAN", DBSCAN_sklearnex), None]]
Expand Down Expand Up @@ -306,6 +315,20 @@ def get_patch_map_core(preview=False):
mapping["_funcwrapper"] = [
[(parallel_module, "_FuncWrapper", _FuncWrapper_sklearnex), None]
]
# Necessary for array_api support
mapping["yield_namespace_device_dtype_combinations"] = [
[
(
_array_api_module,
"yield_namespace_device_dtype_combinations",
yield_namespace_device_dtype_combinations_sklearnex,
),
None,
]
]
mapping["_convert_to_numpy"] = [
[(_array_api_module, "_convert_to_numpy", _convert_to_numpy_sklearnex), None]
]
return mapping


Expand Down
96 changes: 96 additions & 0 deletions sklearnex/utils/_array_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# ==============================================================================
# Copyright 2024 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.
# ==============================================================================

"""Tools to support array_api."""
import itertools

import numpy as np

from daal4py.sklearn._utils import sklearn_check_version

from .._device_offload import dpctl_available, dpnp_available

# import math
# from functools import wraps




if sklearn_check_version("1.2"):
from sklearn.utils._array_api import _convert_to_numpy as _sklearn_convert_to_numpy
from sklearn.utils._array_api import get_namespace as sklearn_get_namespace
from sklearn.utils._array_api import (
yield_namespace_device_dtype_combinations as sklearn_yield_namespace_device_dtype_combinations,
)

if dpctl_available:
import dpctl.tensot as dpt

if dpnp_available:
import dpnp


def _convert_to_numpy(array, xp):
"""Convert X into a NumPy ndarray on the CPU."""
xp_name = xp.__name__

if dpctl_available and xp_name in {
"dpctl.tensor",
}:
return dpt.to_numpy(array)
else:
return _sklearn_convert_to_numpy(array, xp)


def yield_namespace_device_dtype_combinations():
"""Yield supported namespace, device, dtype tuples for testing.

Use this to test that an estimator works with all combinations.

Returns
-------
array_namespace : str
The name of the Array API namespace.

device : str
The name of the device on which to allocate the arrays. Can be None to
indicate that the default value should be used.

dtype_name : str
The name of the data type to use for arrays. Can be None to indicate
that the default value should be used.
"""
for array_namespace in [
samir-nasibli marked this conversation as resolved.
Show resolved Hide resolved
# The following is used to test the array_api_compat wrapper when
# array_api_dispatch is enabled: in particular, the arrays used in the
# tests are regular numpy arrays without any "device" attribute.
"numpy",
# Stricter NumPy-based Array API implementation. The
# numpy.array_api.Array instances always a dummy "device" attribute.
"numpy.array_api",
"cupy",
"cupy.array_api",
"torch",
"dpctl.tensor",
]:
if array_namespace == "torch":
for device, dtype in itertools.product(
("cpu", "cuda"), ("float64", "float32")
):
yield array_namespace, device, dtype
yield array_namespace, "mps", "float32"
else:
yield array_namespace, None, None
Loading