From cc41691dedeac1a711516995dc3d5167a84d2626 Mon Sep 17 00:00:00 2001 From: jiamingy Date: Fri, 25 Mar 2022 19:56:21 +0800 Subject: [PATCH 1/2] Check cupy lazily. --- python-package/xgboost/core.py | 13 ++++++++----- python-package/xgboost/data.py | 10 ++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 936b860232fc..924abede4021 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -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) @@ -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) diff --git a/python-package/xgboost/data.py b/python-package/xgboost/data.py index ee86a84912aa..7e9f7acf831c 100644 --- a/python-package/xgboost/data.py +++ b/python-package/xgboost/data.py @@ -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): From c1b505bc7f5391386837fdfc746ff3ffed76c331 Mon Sep 17 00:00:00 2001 From: jiamingy Date: Fri, 25 Mar 2022 21:14:33 +0800 Subject: [PATCH 2/2] Lint. --- python-package/xgboost/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 924abede4021..2c25c6e9a902 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -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,