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

[jvm-packages] [pyspark] Make QDM optional based on cuDF check #8471

Merged
merged 6 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions python-package/xgboost/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ def lazy_isinstance(instance: Any, module: str, name: str) -> bool:
PANDAS_INSTALLED = False


# cuDF
CUDF_INSTALLED = importlib.util.find_spec("cudf") is not None


# sklearn
try:
from sklearn.base import BaseEstimator as XGBModelBase
Expand Down Expand Up @@ -77,6 +73,18 @@ def lazy_isinstance(instance: Any, module: str, name: str) -> bool:
XGBStratifiedKFold = None


def is_cudf_installed():
"""Check cuDF installed or not"""
# Checking by `importing` instead of check `importlib.util.find_spec("cudf") is not None`
WeichenXu123 marked this conversation as resolved.
Show resolved Hide resolved
# because user might install cudf successfully but importing cudf raises issues (e.g. saying
# running on mismatched cuda version)
try:
import cudf
return True
except ImportError:
return False


class XGBoostLabelEncoder(LabelEncoder):
"""Label encoder with JSON serialization methods."""

Expand Down
14 changes: 8 additions & 6 deletions python-package/xgboost/spark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import xgboost
from xgboost import XGBClassifier, XGBRanker, XGBRegressor
from xgboost.compat import CUDF_INSTALLED
from xgboost.compat import is_cudf_installed

from .data import (
_read_csr_matrix_from_unwrapped_spark_vec,
Expand All @@ -57,7 +57,6 @@
HasEnableSparseDataOptim,
HasFeaturesCols,
HasQueryIdCol,
UseQuantileDMatrix,
)
from .utils import (
CommunicatorContext,
Expand Down Expand Up @@ -758,10 +757,7 @@ def _fit(self, dataset):
}
dmatrix_kwargs = {k: v for k, v in dmatrix_kwargs.items() if v is not None}

# If cuDF is not installed, then using DMatrix instead of QDM,
# because without cuDF, DMatrix performs better than QDM.
use_qdm = CUDF_INSTALLED and \
booster_params.get("tree_method", None) in ("hist", "gpu_hist")
use_hist = booster_params.get("tree_method", None) in ("hist", "gpu_hist")

def _train_booster(pandas_df_iter):
"""Takes in an RDD partition and outputs a booster for that partition after
Expand All @@ -775,6 +771,12 @@ def _train_booster(pandas_df_iter):

gpu_id = None

# If cuDF is not installed, then using DMatrix instead of QDM,
# because without cuDF, DMatrix performs better than QDM.
# Note: Checking `is_cudf_installed` in spark worker side because
# spark worker might has different python environment with driver side.
use_qdm = use_hist and is_cudf_installed()
WeichenXu123 marked this conversation as resolved.
Show resolved Hide resolved

if use_qdm and (booster_params.get("max_bin", None) is not None):
dmatrix_kwargs["max_bin"] = booster_params["max_bin"]

Expand Down