← Back to Pull requests

fix(df): keep the best split's sample order when constant features are skipped #3856

Closed
unknown closed this pull request without merging on Oct 07, 2026 12:59 UTC
View on GitHub
unknown unknown commented on Oct 06, 2026 17:02 UTC
Author

Description

Random-forest training on CPU could split a node's samples by the wrong feature. The class histograms recorded for its children then belonged to other samples, and deeper nodes built from them came out with negative class counts. Through scikit-learn-intelex this showed up as predict_proba rows holding a negative probability, e.g. -0.005 with 100 trees, on make_classification(n_samples=200_000, n_features=30, n_informative=15, n_classes=7, n_clusters_per_class=1, random_state=0). Its clip to [0, 1] then broke the row sum, and roc_auc_score(..., multi_class="ovr") rejected the scores. The wrapper side is uxlfoundation/scikit-learn-intelex#3449.

Cause (findBestSplitSerial in df_train_dense_default_impl.i). Features already known to be constant are skipped without counting as visited, so the feature loop can run past i == _nFeaturesPerNode - 1. The code assumed that a split found at that index was the last one evaluated. So it neither saved aIdx into bestSplitIdx nor restored it afterwards. When a later feature took the non-indexed path, featureValuesToBuf re-sorted aIdx by that feature. If that feature did not win, the node was then partitioned as "the first nLeft samples" in the wrong order. Only small nodes skip the precomputed bin indices, so this needs a node of a few rows with a single-bin feature, which the hist method produces regularly. Both the dense and hist methods are affected, with both splitters. Regression goes through the same code and got wrong leaf means silently. It is deterministic (single-threaded runs give the same leaf), not a race.

On the reported data, 119 nodes over the 100 trees were split this way. Only one of them produced a visible negative probability; the others left leaves with wrong but non-negative values.

Fix: track whether aIdx still holds the best split's order. Save it whenever another feature may still be visited, and copy it back only if a later feature re-sorted it.

Test: "df cls probabilities stay valid when constant features are skipped" uses 2000 rows of 8 binary and 4 continuous features over 4 random classes. It checks that every probability is in [0, 1], that rows sum to 1, and that each training row's own class has a positive probability. It fails before the fix on CPU in every float/double × dense/hist variant with the best splitter, with values down to -0.1, and passes after it. The whole decision_forest suite passes on CPU and GPU. Through scikit-learn-intelex, with only this fix and the current clip-only wrapper, the reported script gives non-negative raw probabilities, row sums within 2.2e-16, and roc_auc_score succeeds, for both RandomForestClassifier and ExtraTreesClassifier.

Not fixed here: the GPU hist path does not have this bug, because it splits by bin index. But the GPU random splitter has a separate one: random_split lets each work-item read one row, so for nodes over max_work_group_size rows (1024 on the test device) the histogram is built from only the first 1024 rows, and that also yields invalid probabilities. The new test skips random splitter on GPU, with a comment, until that gets its own fix.

---

<details>
<summary>Checklist:</summary>

Completeness and readability

  • I have commented my code, particularly in hard-to-understand areas.

  • I have updated the documentation to reflect the changes or created a separate PR with updates and provided its number in the description, if necessary.

  • Git commit message contains an appropriate signed-off-by string _(see CONTRIBUTING.md for details)_.

  • I have resolved any merge conflicts that might occur with the base branch.
  • Testing

  • I have run it locally and tested the changes extensively.

  • All CI jobs are green or I have provided justification why they aren't.

  • I have extended testing suite if new functionality was introduced in this PR.
  • Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least a summary table with measured data, if performance change is expected.

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected.
  • No performance change is expected: one extra memcpy of a node's index range in the rare case where the best split was found at the last index but more features follow. Model quality can change slightly, since affected nodes are now split correctly.

    </details>

    🤖 Generated with Claude Code

    Diff: 590 lines (+442 / -148) across 7 files Reviewer: Vika-F Review duration: 1196m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 590 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3856"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.0% (MEDIUM) · Higher = larger blast radius
    50.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%