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

Fix warnings in GPU dask tests. #10358

Merged
merged 1 commit into from
Jun 4, 2024
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
41 changes: 29 additions & 12 deletions python-package/xgboost/testing/dask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for dask shared by different test modules."""

from typing import Literal

import numpy as np
import pandas as pd
from dask import array as da
Expand All @@ -10,63 +12,78 @@
from xgboost.testing.updater import get_basescore


def check_init_estimation_clf(tree_method: str, client: Client) -> None:
def check_init_estimation_clf(
tree_method: str, device: Literal["cpu", "cuda"], client: Client
) -> None:
"""Test init estimation for classsifier."""
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=4096 * 2, n_features=32, random_state=1994)
clf = xgb.XGBClassifier(n_estimators=1, max_depth=1, tree_method=tree_method)
clf = xgb.XGBClassifier(
n_estimators=1, max_depth=1, tree_method=tree_method, device=device
)
clf.fit(X, y)
base_score = get_basescore(clf)

dx = da.from_array(X).rechunk(chunks=(32, None))
dy = da.from_array(y).rechunk(chunks=(32,))
dclf = xgb.dask.DaskXGBClassifier(
n_estimators=1, max_depth=1, tree_method=tree_method
n_estimators=1,
max_depth=1,
tree_method=tree_method,
device=device,
)
dclf.client = client
dclf.fit(dx, dy)
dbase_score = get_basescore(dclf)
np.testing.assert_allclose(base_score, dbase_score)


def check_init_estimation_reg(tree_method: str, client: Client) -> None:
def check_init_estimation_reg(
tree_method: str, device: Literal["cpu", "cuda"], client: Client
) -> None:
"""Test init estimation for regressor."""
from sklearn.datasets import make_regression

# pylint: disable=unbalanced-tuple-unpacking
X, y = make_regression(n_samples=4096 * 2, n_features=32, random_state=1994)
reg = xgb.XGBRegressor(n_estimators=1, max_depth=1, tree_method=tree_method)
reg = xgb.XGBRegressor(
n_estimators=1, max_depth=1, tree_method=tree_method, device=device
)
reg.fit(X, y)
base_score = get_basescore(reg)

dx = da.from_array(X).rechunk(chunks=(32, None))
dy = da.from_array(y).rechunk(chunks=(32,))
dreg = xgb.dask.DaskXGBRegressor(
n_estimators=1, max_depth=1, tree_method=tree_method
n_estimators=1, max_depth=1, tree_method=tree_method, device=device
)
dreg.client = client
dreg.fit(dx, dy)
dbase_score = get_basescore(dreg)
np.testing.assert_allclose(base_score, dbase_score)


def check_init_estimation(tree_method: str, client: Client) -> None:
def check_init_estimation(
tree_method: str, device: Literal["cpu", "cuda"], client: Client
) -> None:
"""Test init estimation."""
check_init_estimation_reg(tree_method, client)
check_init_estimation_clf(tree_method, client)
check_init_estimation_reg(tree_method, device, client)
check_init_estimation_clf(tree_method, device, client)


def check_uneven_nan(client: Client, tree_method: str, n_workers: int) -> None:
def check_uneven_nan(
client: Client, tree_method: str, device: Literal["cpu", "cuda"], n_workers: int
) -> None:
"""Issue #9271, not every worker has missing value."""
assert n_workers >= 2

with client.as_current():
clf = xgb.dask.DaskXGBClassifier(tree_method=tree_method)
clf = xgb.dask.DaskXGBClassifier(tree_method=tree_method, device=device)
X = pd.DataFrame({"a": range(10000), "b": range(10000, 0, -1)})
y = pd.Series([*[0] * 5000, *[1] * 5000])

X["a"][:3000:1000] = np.nan
X.loc[:3000:1000, "a"] = np.nan

client.wait_for_workers(n_workers=n_workers)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ def test_boost_from_prediction(self, local_cuda_client: Client) -> None:
run_boost_from_prediction_multi_class(X, y, "hist", "cuda", local_cuda_client)

def test_init_estimation(self, local_cuda_client: Client) -> None:
check_init_estimation("gpu_hist", local_cuda_client)
check_init_estimation("hist", "cuda", local_cuda_client)

def test_uneven_nan(self) -> None:
n_workers = 2
with LocalCUDACluster(n_workers=n_workers) as cluster:
with Client(cluster) as client:
check_uneven_nan(client, "gpu_hist", n_workers)
check_uneven_nan(client, "hist", "cuda", n_workers)

@pytest.mark.skipif(**tm.no_dask_cudf())
def test_dask_dataframe(self, local_cuda_client: Client) -> None:
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_dask_classifier(self, model: str, local_cuda_client: Client) -> None:
X = dask_cudf.from_dask_dataframe(dd.from_dask_array(X_))
y = dask_cudf.from_dask_dataframe(dd.from_dask_array(y_))
w = dask_cudf.from_dask_dataframe(dd.from_dask_array(w_))
run_dask_classifier(X, y, w, model, "gpu_hist", local_cuda_client, 10)
run_dask_classifier(X, y, w, model, "hist", "cuda", local_cuda_client, 10)

def test_empty_dmatrix(self, local_cuda_client: Client) -> None:
parameters = {
Expand Down
23 changes: 16 additions & 7 deletions tests/test_distributed/test_with_dask/test_with_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from math import ceil
from operator import attrgetter, getitem
from pathlib import Path
from typing import Any, Dict, Generator, Optional, Tuple, Type, TypeVar, Union
from typing import Any, Dict, Generator, Literal, Optional, Tuple, Type, TypeVar, Union

import hypothesis
import numpy as np
Expand Down Expand Up @@ -700,18 +700,27 @@ def run_dask_classifier(
w: xgb.dask._DaskCollection,
model: str,
tree_method: Optional[str],
device: Literal["cpu", "cuda"],
client: "Client",
n_classes,
) -> None:
metric = "merror" if n_classes > 2 else "logloss"

if model == "boosting":
classifier = xgb.dask.DaskXGBClassifier(
verbosity=1, n_estimators=2, eval_metric=metric, tree_method=tree_method
verbosity=1,
n_estimators=2,
eval_metric=metric,
tree_method=tree_method,
device=device,
)
else:
classifier = xgb.dask.DaskXGBRFClassifier(
verbosity=1, n_estimators=2, eval_metric=metric, tree_method=tree_method
verbosity=1,
n_estimators=2,
eval_metric=metric,
tree_method=tree_method,
device=device,
)

assert classifier._estimator_type == "classifier"
Expand Down Expand Up @@ -785,12 +794,12 @@ def test_dask_classifier(model: str, client: "Client") -> None:
X, y, w = generate_array(with_weights=True)
y = (y * 10).astype(np.int32)
assert w is not None
run_dask_classifier(X, y, w, model, None, client, 10)
run_dask_classifier(X, y, w, model, None, "cpu", client, 10)

y_bin = y.copy()
y_bin[y > 5] = 1.0
y_bin[y <= 5] = 0.0
run_dask_classifier(X, y_bin, w, model, None, client, 2)
run_dask_classifier(X, y_bin, w, model, None, "cpu", client, 2)


def test_empty_dmatrix_training_continuation(client: "Client") -> None:
Expand Down Expand Up @@ -2136,15 +2145,15 @@ def _() -> xgb.dask.DaskXGBClassifier:


def test_init_estimation(client: Client) -> None:
check_init_estimation("hist", client)
check_init_estimation("hist", "cpu", client)


@pytest.mark.parametrize("tree_method", ["hist", "approx"])
def test_uneven_nan(tree_method) -> None:
n_workers = 2
with LocalCluster(n_workers=n_workers) as cluster:
with Client(cluster) as client:
check_uneven_nan(client, tree_method, n_workers)
check_uneven_nan(client, tree_method, "cpu", n_workers)


class TestDaskCallbacks:
Expand Down
Loading