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

Check cupy lazily. #7752

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 9 additions & 6 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
import scipy.sparse

from .compat import STRING_TYPES, DataFrame, py_str, PANDAS_INSTALLED, lazy_isinstance
from .compat import STRING_TYPES, DataFrame, py_str, PANDAS_INSTALLED
from .libpath import find_lib_path
from ._typing import (
CStrPptr,
Expand Down Expand Up @@ -2080,8 +2080,13 @@ def inplace_predict(
f"got {data.shape[1]}"
)

from .data import _is_pandas_df, _transform_pandas_df, _is_cudf_df
from .data import _array_interface
from .data import (
_is_pandas_df,
_transform_pandas_df,
_is_cudf_df,
_is_cupy_array,
_array_interface,
)
enable_categorical = _has_categorical(self, data)
if _is_pandas_df(data):
data, _, _ = _transform_pandas_df(data, enable_categorical)
Expand Down Expand Up @@ -2118,9 +2123,7 @@ def inplace_predict(
)
)
return _prediction_output(shape, dims, preds, False)
if lazy_isinstance(data, "cupy.core.core", "ndarray") or lazy_isinstance(
data, "cupy._core.core", "ndarray"
):
if _is_cupy_array(data):
from .data import _transform_cupy_array

data = _transform_cupy_array(data)
Expand Down
10 changes: 4 additions & 6 deletions python-package/xgboost/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,10 @@ def _is_cudf_ser(data):
return isinstance(data, cudf.Series)


def _is_cupy_array(data):
try:
import cupy
except ImportError:
return False
return isinstance(data, cupy.ndarray)
def _is_cupy_array(data: Any) -> bool:
return lazy_isinstance(data, "cupy.core.core", "ndarray") or lazy_isinstance(
data, "cupy._core.core", "ndarray"
)


def _transform_cupy_array(data):
Expand Down