diff --git a/python-package/xgboost/compat.py b/python-package/xgboost/compat.py index f3f8705b97d9..e09ccb6f4d29 100644 --- a/python-package/xgboost/compat.py +++ b/python-package/xgboost/compat.py @@ -1,5 +1,6 @@ # pylint: disable=invalid-name,unused-import """For compatibility and optional dependencies.""" +import functools import importlib.util import logging import sys @@ -84,6 +85,7 @@ class XGBRegressorBase: # type: ignore[no-redef] _logger = logging.getLogger(__name__) +@functools.cache def is_cudf_available() -> bool: """Check cuDF package available or not""" if importlib.util.find_spec("cudf") is None: @@ -97,6 +99,7 @@ def is_cudf_available() -> bool: return False +@functools.cache def is_cupy_available() -> bool: """Check cupy package available or not""" if importlib.util.find_spec("cupy") is None: @@ -109,6 +112,7 @@ def is_cupy_available() -> bool: return False +@functools.cache def import_cupy() -> types.ModuleType: """Import cupy.""" if not is_cupy_available(): diff --git a/python-package/xgboost/data.py b/python-package/xgboost/data.py index 7b37ca50e4e9..1085f28f8ff5 100644 --- a/python-package/xgboost/data.py +++ b/python-package/xgboost/data.py @@ -872,7 +872,8 @@ def _is_cudf_pandas(data: DataType) -> bool: ) -def _get_cudf_cat_predicate() -> Callable[[Any], bool]: +@functools.cache +def _lazy_load_cudf_is_cat() -> Callable[[Any], bool]: try: from cudf import CategoricalDtype @@ -895,7 +896,7 @@ def _cudf_array_interfaces(data: DataType, cat_codes: list) -> bytes: array interface is finished. """ - is_categorical_dtype = _get_cudf_cat_predicate() + is_categorical_dtype = _lazy_load_cudf_is_cat() interfaces = [] def append(interface: dict) -> None: @@ -933,7 +934,7 @@ def _transform_cudf_df( except ImportError: from pandas.api.types import is_bool_dtype - is_categorical_dtype = _get_cudf_cat_predicate() + is_categorical_dtype = _lazy_load_cudf_is_cat() # Work around https://github.com/dmlc/xgboost/issues/10181 if _is_cudf_ser(data): if is_bool_dtype(data.dtype):