Cleanup Python GPU tests. (#9934)
* Cleanup Python GPU tests. - Remove the use of `gpu_hist` and `gpu_id` in cudf/cupy tests. - Move base margin test into the testing directory.
This commit is contained in:
@@ -1,31 +1,25 @@
|
||||
import json
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import xgboost as xgb
|
||||
|
||||
sys.path.append("tests/python")
|
||||
from test_dmatrix import set_base_margin_info
|
||||
|
||||
from xgboost import testing as tm
|
||||
from xgboost.testing.data import run_base_margin_info
|
||||
|
||||
cupy = pytest.importorskip("cupy")
|
||||
cp = pytest.importorskip("cupy")
|
||||
|
||||
|
||||
def test_array_interface() -> None:
|
||||
arr = cupy.array([[1, 2, 3, 4], [1, 2, 3, 4]])
|
||||
arr = cp.array([[1, 2, 3, 4], [1, 2, 3, 4]])
|
||||
i_arr = arr.__cuda_array_interface__
|
||||
i_arr = json.loads(json.dumps(i_arr))
|
||||
ret = xgb.core.from_array_interface(i_arr)
|
||||
np.testing.assert_equal(cupy.asnumpy(arr), cupy.asnumpy(ret))
|
||||
np.testing.assert_equal(cp.asnumpy(arr), cp.asnumpy(ret))
|
||||
|
||||
|
||||
def dmatrix_from_cupy(input_type, DMatrixT, missing=np.NAN):
|
||||
'''Test constructing DMatrix from cupy'''
|
||||
import cupy as cp
|
||||
|
||||
"""Test constructing DMatrix from cupy"""
|
||||
kRows = 80
|
||||
kCols = 3
|
||||
|
||||
@@ -51,9 +45,7 @@ def dmatrix_from_cupy(input_type, DMatrixT, missing=np.NAN):
|
||||
|
||||
|
||||
def _test_from_cupy(DMatrixT):
|
||||
'''Test constructing DMatrix from cupy'''
|
||||
import cupy as cp
|
||||
|
||||
"""Test constructing DMatrix from cupy"""
|
||||
dmatrix_from_cupy(np.float16, DMatrixT, np.NAN)
|
||||
dmatrix_from_cupy(np.float32, DMatrixT, np.NAN)
|
||||
dmatrix_from_cupy(np.float64, DMatrixT, np.NAN)
|
||||
@@ -73,7 +65,6 @@ def _test_from_cupy(DMatrixT):
|
||||
|
||||
|
||||
def _test_cupy_training(DMatrixT):
|
||||
import cupy as cp
|
||||
np.random.seed(1)
|
||||
cp.random.seed(1)
|
||||
X = cp.random.randn(50, 10, dtype="float32")
|
||||
@@ -85,19 +76,23 @@ def _test_cupy_training(DMatrixT):
|
||||
|
||||
evals_result_cupy = {}
|
||||
dtrain_cp = DMatrixT(X, y, weight=cupy_weights, base_margin=cupy_base_margin)
|
||||
params = {'gpu_id': 0, 'nthread': 1, 'tree_method': 'gpu_hist'}
|
||||
xgb.train(params, dtrain_cp, evals=[(dtrain_cp, "train")],
|
||||
evals_result=evals_result_cupy)
|
||||
params = {"tree_method": "hist", "device": "cuda:0"}
|
||||
xgb.train(
|
||||
params, dtrain_cp, evals=[(dtrain_cp, "train")], evals_result=evals_result_cupy
|
||||
)
|
||||
evals_result_np = {}
|
||||
dtrain_np = xgb.DMatrix(cp.asnumpy(X), cp.asnumpy(y), weight=weights,
|
||||
base_margin=base_margin)
|
||||
xgb.train(params, dtrain_np, evals=[(dtrain_np, "train")],
|
||||
evals_result=evals_result_np)
|
||||
assert np.array_equal(evals_result_cupy["train"]["rmse"], evals_result_np["train"]["rmse"])
|
||||
dtrain_np = xgb.DMatrix(
|
||||
cp.asnumpy(X), cp.asnumpy(y), weight=weights, base_margin=base_margin
|
||||
)
|
||||
xgb.train(
|
||||
params, dtrain_np, evals=[(dtrain_np, "train")], evals_result=evals_result_np
|
||||
)
|
||||
assert np.array_equal(
|
||||
evals_result_cupy["train"]["rmse"], evals_result_np["train"]["rmse"]
|
||||
)
|
||||
|
||||
|
||||
def _test_cupy_metainfo(DMatrixT):
|
||||
import cupy as cp
|
||||
n = 100
|
||||
X = np.random.random((n, 2))
|
||||
dmat_cupy = DMatrixT(cp.array(X))
|
||||
@@ -106,33 +101,35 @@ def _test_cupy_metainfo(DMatrixT):
|
||||
uints = np.array([4, 2, 8]).astype("uint32")
|
||||
cupy_floats = cp.array(floats)
|
||||
cupy_uints = cp.array(uints)
|
||||
dmat.set_float_info('weight', floats)
|
||||
dmat.set_float_info('label', floats)
|
||||
dmat.set_float_info('base_margin', floats)
|
||||
dmat.set_uint_info('group', uints)
|
||||
dmat.set_float_info("weight", floats)
|
||||
dmat.set_float_info("label", floats)
|
||||
dmat.set_float_info("base_margin", floats)
|
||||
dmat.set_uint_info("group", uints)
|
||||
dmat_cupy.set_info(weight=cupy_floats)
|
||||
dmat_cupy.set_info(label=cupy_floats)
|
||||
dmat_cupy.set_info(base_margin=cupy_floats)
|
||||
dmat_cupy.set_info(group=cupy_uints)
|
||||
|
||||
# Test setting info with cupy
|
||||
assert np.array_equal(dmat.get_float_info('weight'),
|
||||
dmat_cupy.get_float_info('weight'))
|
||||
assert np.array_equal(dmat.get_float_info('label'),
|
||||
dmat_cupy.get_float_info('label'))
|
||||
assert np.array_equal(dmat.get_float_info('base_margin'),
|
||||
dmat_cupy.get_float_info('base_margin'))
|
||||
assert np.array_equal(dmat.get_uint_info('group_ptr'),
|
||||
dmat_cupy.get_uint_info('group_ptr'))
|
||||
assert np.array_equal(
|
||||
dmat.get_float_info("weight"), dmat_cupy.get_float_info("weight")
|
||||
)
|
||||
assert np.array_equal(
|
||||
dmat.get_float_info("label"), dmat_cupy.get_float_info("label")
|
||||
)
|
||||
assert np.array_equal(
|
||||
dmat.get_float_info("base_margin"), dmat_cupy.get_float_info("base_margin")
|
||||
)
|
||||
assert np.array_equal(
|
||||
dmat.get_uint_info("group_ptr"), dmat_cupy.get_uint_info("group_ptr")
|
||||
)
|
||||
|
||||
set_base_margin_info(cp.asarray, DMatrixT, "gpu_hist")
|
||||
run_base_margin_info(cp.asarray, DMatrixT, "cuda")
|
||||
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
@pytest.mark.skipif(**tm.no_sklearn())
|
||||
def test_cupy_training_with_sklearn():
|
||||
import cupy as cp
|
||||
|
||||
np.random.seed(1)
|
||||
cp.random.seed(1)
|
||||
X = cp.random.randn(50, 10, dtype="float32")
|
||||
@@ -142,7 +139,7 @@ def test_cupy_training_with_sklearn():
|
||||
base_margin = np.random.random(50)
|
||||
cupy_base_margin = cp.array(base_margin)
|
||||
|
||||
clf = xgb.XGBClassifier(gpu_id=0, tree_method="gpu_hist")
|
||||
clf = xgb.XGBClassifier(tree_method="hist", device="cuda:0")
|
||||
clf.fit(
|
||||
X,
|
||||
y,
|
||||
@@ -155,8 +152,8 @@ def test_cupy_training_with_sklearn():
|
||||
|
||||
|
||||
class TestFromCupy:
|
||||
'''Tests for constructing DMatrix from data structure conforming Apache
|
||||
Arrow specification.'''
|
||||
"""Tests for constructing DMatrix from data structure conforming Apache
|
||||
Arrow specification."""
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
def test_simple_dmat_from_cupy(self):
|
||||
@@ -184,19 +181,17 @@ Arrow specification.'''
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
def test_dlpack_simple_dmat(self):
|
||||
import cupy as cp
|
||||
n = 100
|
||||
X = cp.random.random((n, 2))
|
||||
xgb.DMatrix(X.toDlpack())
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
def test_cupy_categorical(self):
|
||||
import cupy as cp
|
||||
n_features = 10
|
||||
X, y = tm.make_categorical(10, n_features, n_categories=4, onehot=False)
|
||||
X = cp.asarray(X.values.astype(cp.float32))
|
||||
y = cp.array(y)
|
||||
feature_types = ['c'] * n_features
|
||||
feature_types = ["c"] * n_features
|
||||
|
||||
assert isinstance(X, cp.ndarray)
|
||||
Xy = xgb.DMatrix(X, y, feature_types=feature_types)
|
||||
@@ -204,7 +199,6 @@ Arrow specification.'''
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
def test_dlpack_device_dmat(self):
|
||||
import cupy as cp
|
||||
n = 100
|
||||
X = cp.random.random((n, 2))
|
||||
m = xgb.QuantileDMatrix(X.toDlpack())
|
||||
@@ -213,7 +207,6 @@ Arrow specification.'''
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
def test_qid(self):
|
||||
import cupy as cp
|
||||
rng = cp.random.RandomState(1994)
|
||||
rows = 100
|
||||
cols = 10
|
||||
@@ -223,19 +216,16 @@ Arrow specification.'''
|
||||
|
||||
Xy = xgb.DMatrix(X, y)
|
||||
Xy.set_info(qid=qid)
|
||||
group_ptr = Xy.get_uint_info('group_ptr')
|
||||
group_ptr = Xy.get_uint_info("group_ptr")
|
||||
assert group_ptr[0] == 0
|
||||
assert group_ptr[-1] == rows
|
||||
|
||||
@pytest.mark.skipif(**tm.no_cupy())
|
||||
@pytest.mark.mgpu
|
||||
def test_specified_device(self):
|
||||
import cupy as cp
|
||||
cp.cuda.runtime.setDevice(0)
|
||||
dtrain = dmatrix_from_cupy(np.float32, xgb.QuantileDMatrix, np.nan)
|
||||
with pytest.raises(
|
||||
xgb.core.XGBoostError, match="Invalid device ordinal"
|
||||
):
|
||||
with pytest.raises(xgb.core.XGBoostError, match="Invalid device ordinal"):
|
||||
xgb.train(
|
||||
{'tree_method': 'gpu_hist', 'gpu_id': 1}, dtrain, num_boost_round=10
|
||||
{"tree_method": "hist", "device": "cuda:1"}, dtrain, num_boost_round=10
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user