Skip to content

Commit 8461059

Browse files
authored
FIX: Improve GPU TreeExplainer parity (#4997)
* Fix GPUTreeExplainer missing value handling * Broaden GPUTreeExplainer parity tests * Fix XGBoost categorical guard * Preserve XGBoost vector base scores * Simplify GPU missing value test * Remove duplicate GPU SHAP output helper * Apply pre-commit formatting
1 parent 8549e6b commit 8461059

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

‎shap/cext/_cext_gpu.cu‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@ void RecurseTree(
7676

7777
// Add left split to the path
7878
unsigned left_child = tree.children_left[pos];
79+
bool is_left_default = tree.children_default[pos] == left_child;
7980
double left_zero_fraction =
8081
tree.node_sample_weights[left_child] / tree.node_sample_weights[pos];
8182
// Encode the range of feature values that flow down this path
8283
tmp_path->emplace_back(0, tree.features[pos], 0,
83-
ShapSplitCondition{-inf, tree.thresholds[pos], false},
84+
ShapSplitCondition{-inf, tree.thresholds[pos],
85+
is_left_default},
8486
left_zero_fraction, 0.0f);
8587

8688
RecurseTree(left_child, tree, tmp_path, paths, path_idx, num_outputs);
8789

88-
// Add left split to the path
90+
// Add right split to the path
8991
tmp_path->back() = gpu_treeshap::PathElement<ShapSplitCondition>(
9092
0, tree.features[pos], 0,
91-
ShapSplitCondition{tree.thresholds[pos], inf, false},
93+
ShapSplitCondition{tree.thresholds[pos], inf, !is_left_default},
9294
1.0 - left_zero_fraction, 0.0f);
9395

9496
RecurseTree(tree.children_right[pos], tree, tmp_path, paths, path_idx,

‎shap/explainers/_tree.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def _xgboost_n_iterations(tree_limit: int, num_stacked_models: int) -> int:
105105

106106

107107
def _xgboost_cat_unsupported(model: TreeEnsemble) -> None:
108-
if model.model_type == "xgboost" and model.cat_feature_indices is not None:
108+
if model.model_type == "xgboost" and (
109+
model.cat_feature_indices is not None or getattr(model, "_xgb_enable_categorical", False)
110+
):
109111
raise NotImplementedError(
110112
"Categorical split is not yet supported. You can still use"
111113
" TreeExplainer with `feature_perturbation=tree_path_dependent`."
@@ -988,6 +990,7 @@ def __init__(
988990
self.tree_limit = None # used for limiting the number of trees we use by default (like from early stopping)
989991
self.num_stacked_models = 1 # If this is greater than 1 it means we have multiple stacked models with the same number of trees in each model (XGBoost multi-output style)
990992
self.cat_feature_indices = None # If this is set it tells us which features are treated categorically
993+
self._xgb_enable_categorical = False
991994

992995
# we use names like keras
993996
objective_name_map = {
@@ -1358,6 +1361,7 @@ def __init__(
13581361
# Some properties of the sklearn API are passed to a DMatrix object in
13591362
# xgboost We need to make sure we do the same here - GH #3313
13601363
self._xgb_dmatrix_props = get_xgboost_dmatrix_properties(model)
1364+
self._xgb_enable_categorical = bool(self._xgb_dmatrix_props.get("enable_categorical", False))
13611365
elif safe_isinstance(model, ["xgboost.sklearn.XGBRegressor", "xgboost.sklearn.XGBRanker"]):
13621366
self.original_model = model.get_booster()
13631367
self._set_xgboost_model_attributes(
@@ -1369,6 +1373,7 @@ def __init__(
13691373
# Some properties of the sklearn API are passed to a DMatrix object in
13701374
# xgboost We need to make sure we do the same here - GH #3313
13711375
self._xgb_dmatrix_props = get_xgboost_dmatrix_properties(model)
1376+
self._xgb_enable_categorical = bool(self._xgb_dmatrix_props.get("enable_categorical", False))
13721377
elif safe_isinstance(model, "lightgbm.basic.Booster"):
13731378
assert_import("lightgbm")
13741379
self.model_type = "lightgbm"
@@ -2274,8 +2279,11 @@ def __init__(self, xgb_model: Any) -> None:
22742279
emsg = f"Expected the base_score to contain a list or float, received {base_score}"
22752280
raise ValueError(emsg) from e
22762281
if isinstance(base_score, (list, tuple, np.ndarray)):
2277-
base_score = base_score[0]
2278-
base_score = float(base_score)
2282+
base_score = np.asarray(base_score, dtype=float)
2283+
if base_score.size == 1:
2284+
base_score = float(base_score[0])
2285+
else:
2286+
base_score = float(base_score)
22792287
self.base_score = base_score
22802288
if self.name_obj in ("binary:logistic", "reg:logistic"):
22812289
self.base_score = scipy.special.logit(base_score)

‎tests/explainers/test_gpu_tree.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,13 @@ def idfn(task):
222222
return type(model).__module__ + "." + type(model).__qualname__
223223

224224

225-
@pytest.mark.parametrize("task", tasks, ids=idfn)
226-
@pytest.mark.parametrize("feature_perturbation", ["interventional", "tree_path_dependent"])
227-
def test_gpu_tree_explainer_shap(task, feature_perturbation):
228-
model, X, _ = task
229-
gpu_ex = shap.GPUTreeExplainer(model, X, feature_perturbation=feature_perturbation)
230-
ex = shap.TreeExplainer(model, X, feature_perturbation=feature_perturbation)
225+
def assert_gpu_matches_cpu(task, feature_perturbation, X=None):
226+
model, background, _ = task
227+
if X is None:
228+
X = background
229+
230+
gpu_ex = shap.GPUTreeExplainer(model, background, feature_perturbation=feature_perturbation)
231+
ex = shap.TreeExplainer(model, background, feature_perturbation=feature_perturbation)
231232
host_shap = ex.shap_values(X, check_additivity=True)
232233
gpu_shap = gpu_ex.shap_values(X, check_additivity=True)
233234

@@ -241,6 +242,22 @@ def test_gpu_tree_explainer_shap(task, feature_perturbation):
241242
assert np.allclose(host_shap, gpu_shap, 1e-3, 1e-3)
242243

243244

245+
@pytest.mark.parametrize("task", tasks, ids=idfn)
246+
@pytest.mark.parametrize("feature_perturbation", ["interventional", "tree_path_dependent"])
247+
def test_gpu_tree_explainer_shap(task, feature_perturbation):
248+
assert_gpu_matches_cpu(task, feature_perturbation)
249+
250+
251+
def test_gpu_tree_explainer_shap_with_missing_values():
252+
task = xgboost_base()
253+
X = task[1].copy()
254+
rows = np.arange(0, X.shape[0], 10)
255+
X[rows, 0] = np.nan
256+
257+
for feature_perturbation in ["interventional", "tree_path_dependent"]:
258+
assert_gpu_matches_cpu(task, feature_perturbation, X)
259+
260+
244261
@pytest.mark.parametrize("task", tasks, ids=idfn)
245262
@pytest.mark.parametrize("feature_perturbation", ["tree_path_dependent"])
246263
def test_gpu_tree_explainer_shap_interactions(task, feature_perturbation):

0 commit comments

Comments
 (0)