From 0846ad860cec25d536f82a8448e2ac0eff487846 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Sat, 20 Jul 2024 22:12:12 +0800 Subject: [PATCH] Optionally skip cupy on windows. (#10611) --- python-package/xgboost/testing/__init__.py | 41 ++++++---------------- python-package/xgboost/testing/data.py | 31 ++++++++++++++++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/python-package/xgboost/testing/__init__.py b/python-package/xgboost/testing/__init__.py index e0096c89c..b934e99e7 100644 --- a/python-package/xgboost/testing/__init__.py +++ b/python-package/xgboost/testing/__init__.py @@ -45,6 +45,7 @@ from xgboost.testing.data import ( get_cancer, get_digits, get_sparse, + make_batches, memory, ) @@ -161,7 +162,16 @@ def no_cudf() -> PytestSkip: def no_cupy() -> PytestSkip: - return no_mod("cupy") + skip_cupy = no_mod("cupy") + if not skip_cupy["condition"] and system() == "Windows": + import cupy as cp + + # Cupy might run into issue on Windows due to missing compiler + try: + cp.array([1, 2, 3]).sum() + except Exception: # pylint: disable=broad-except + skip_cupy["condition"] = True + return skip_cupy def no_dask_cudf() -> PytestSkip: @@ -248,35 +258,6 @@ class IteratorForTest(xgb.core.DataIter): return X, y, w -def make_batches( # pylint: disable=too-many-arguments,too-many-locals - n_samples_per_batch: int, - n_features: int, - n_batches: int, - use_cupy: bool = False, - *, - vary_size: bool = False, - random_state: int = 1994, -) -> Tuple[List[np.ndarray], List[np.ndarray], List[np.ndarray]]: - X = [] - y = [] - w = [] - if use_cupy: - import cupy - - rng = cupy.random.RandomState(random_state) - else: - rng = np.random.RandomState(random_state) - for i in range(n_batches): - n_samples = n_samples_per_batch + i * 10 if vary_size else n_samples_per_batch - _X = rng.randn(n_samples, n_features) - _y = rng.randn(n_samples) - _w = rng.uniform(low=0, high=1, size=n_samples) - X.append(_X) - y.append(_y) - w.append(_w) - return X, y, w - - def make_regression( n_samples: int, n_features: int, use_cupy: bool ) -> Tuple[ArrayLike, ArrayLike, ArrayLike]: diff --git a/python-package/xgboost/testing/data.py b/python-package/xgboost/testing/data.py index f4e97e59d..4071219c4 100644 --- a/python-package/xgboost/testing/data.py +++ b/python-package/xgboost/testing/data.py @@ -9,6 +9,7 @@ from typing import ( Callable, Dict, Generator, + List, NamedTuple, Optional, Tuple, @@ -506,6 +507,36 @@ def get_mq2008( ) +def make_batches( # pylint: disable=too-many-arguments,too-many-locals + n_samples_per_batch: int, + n_features: int, + n_batches: int, + use_cupy: bool = False, + *, + vary_size: bool = False, + random_state: int = 1994, +) -> Tuple[List[np.ndarray], List[np.ndarray], List[np.ndarray]]: + """Make batches of dense data.""" + X = [] + y = [] + w = [] + if use_cupy: + import cupy # pylint: disable=import-error + + rng = cupy.random.RandomState(random_state) + else: + rng = np.random.RandomState(random_state) + for i in range(n_batches): + n_samples = n_samples_per_batch + i * 10 if vary_size else n_samples_per_batch + _X = rng.randn(n_samples, n_features) + _y = rng.randn(n_samples) + _w = rng.uniform(low=0, high=1, size=n_samples) + X.append(_X) + y.append(_y) + w.append(_w) + return X, y, w + + RelData = Tuple[sparse.csr_matrix, npt.NDArray[np.int32], npt.NDArray[np.int32]]