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

Make the benchmark runnable on CPU-only instance; speed up CPU RF #256

Merged
merged 2 commits into from
Aug 2, 2023
Merged
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
20 changes: 14 additions & 6 deletions source/examples/xgboost-rf-gpu-cpu-benchmark/hpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
import time

import dask
import dask_cudf
import optuna
import xgboost as xgb
from cuml.dask.common.utils import persist_across_workers
from cuml.dask.ensemble import RandomForestClassifier as RF_gpu
from cuml.metrics import accuracy_score as accuracy_score_gpu
from dask.distributed import Client, LocalCluster, wait
from dask_cuda import LocalCUDACluster
from dask_ml.model_selection import train_test_split
Expand Down Expand Up @@ -40,6 +36,8 @@

def ingest_data(mode):
if mode == "gpu":
import dask_cudf

dataset = dask_cudf.read_parquet(
glob.glob("./data/*.parquet"),
columns=feature_columns,
Expand All @@ -61,6 +59,8 @@ def preprocess_data(dataset, *, client, i_fold, mode):
X_test, y_test = X_test.astype("float32"), y_test.astype("int32")

if mode == "gpu":
from cuml.dask.common.utils import persist_across_workers

X_train, y_train, X_test, y_test = persist_across_workers(
client, [X_train, y_train, X_test, y_test], workers=client.has_what().keys()
)
Expand Down Expand Up @@ -94,6 +94,8 @@ def train_xgboost(trial, *, dataset, client, mode):
)

if mode == "gpu":
from cuml.metrics import accuracy_score as accuracy_score_gpu

params["tree_method"] = "gpu_hist"
dtrain = xgb.dask.DaskDeviceQuantileDMatrix(client, X_train, y_train)
dtest = xgb.dask.DaskDeviceQuantileDMatrix(client, X_test)
Expand Down Expand Up @@ -125,7 +127,6 @@ def train_randomforest(trial, *, dataset, client, mode):
"n_estimators": trial.suggest_int("n_estimators", 100, 500, step=10),
"criterion": trial.suggest_categorical("criterion", ["gini", "entropy"]),
"min_samples_split": trial.suggest_int("min_samples_split", 2, 1000, log=True),
"n_bins": 256,
}

cv_fold_scores = []
Expand All @@ -135,14 +136,21 @@ def train_randomforest(trial, *, dataset, client, mode):
)

if mode == "gpu":
from cuml.dask.ensemble import RandomForestClassifier as RF_gpu
from cuml.metrics import accuracy_score as accuracy_score_gpu

params["n_bins"] = 256
trained_model = RF_gpu(client=client, **params)
accuracy_score_func = accuracy_score_gpu
else:
params["n_jobs"] = -1
trained_model = RF_cpu(**params)
accuracy_score_func = accuracy_score_cpu

trained_model.fit(X_train, y_train)
pred = trained_model.predict(X_test).compute()
pred = trained_model.predict(X_test)
if mode == "gpu":
pred = pred.compute()
y_test = y_test.compute()
score = accuracy_score_func(y_test, pred)
cv_fold_scores.append(score)
Expand Down
Loading