← Back to Pull requests

fix(df): make decision forest OOB results and variable importance independent of the thread count #3911

Closed
unknown closed this pull request without merging on Nov 09, 2026 22:41 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 09:04 UTC
Author

Description

CPU decision forest training accumulates out-of-bag predictions and variable importance in thread-local buffers and merges them in scheduling order (tlsCtx.reduce in df_train_dense_default_impl.i). The regression prediction sums and the importance means are floating-point, so the OOB error, the per-observation OOB results, the OOB predictions and variableImportance differ in the last bits between runs with the same seed and between thread counts. scikit-learn computes these sequentially in tree order and is reproducible; sklearnex oob_score_, oob_prediction_ and feature_importances_ inherit the drift. Surfaced during the review of #3872.

The threading layer's static_threader_reduce uses TBB's static_partitioner, whose chunking depends on the thread count, and a reducer body for OOB would be a full nRows buffer per split, so a deterministic reduction would either depend on the thread count or multiply memory. This change sums every floating-point result in tree order instead and keeps tree building fully dynamic:

  • Regression OOB predictions are summed in tree order after training. Each task marks the OOB rows of its tree in a per-tree bitmask (nTrees x ceil(nRows / 8) bytes; only that task writes its tree's mask). After the parallel loop, a pass parallel over row blocks adds, for each row, the predictions of the trees for which it is OOB in tree index order, in double, using the model's trees (findNode on the DecisionTreeTable, as in prediction). This is the order scikit-learn uses for oob_prediction_, and no extended precision is involved. The per-thread regression OOB buffers are removed.

  • Variable importance is written per tree into an nTrees x nFeatures buffer (each task writes the row of the tree it builds) and reduced in tree order after the parallel loop: a plain sum for MDI, Welford mean/variance for MDA_Raw and MDA_Scaled. The cross-thread Welford merge and varImpVariance are removed.

  • Classification votes are integers, so their sum does not depend on the merge order; they stay in the thread-local buffers. The helpers now receive the typed thread context instead of a raw oobBuf pointer.

  • Thread contexts copy the per-fit state from the main context (init(par, x, nClasses, mainCtx)), and createTlsContext checks its allocation before constructing.
  • Both defaultDense and hist, best and random splitters, float and double share computeImpl, so all CPU paths are covered. For regression with OOB results, the per-thread nRows x 16 byte OOB buffers are replaced by the nTrees x nRows / 8 byte masks; variable importance adds nTrees x nFeatures values when requested.

    What changes for users: only the OOB outputs (OOB error, per-observation error, R2, oob_prediction_) and variable importance, and only in the last bits, now identical across thread counts. The trees and therefore predict() are unchanged. One exception to "last bits": with MDA_Raw or MDA_Scaled, a tree without OOB rows (possible for small nRows with observations_per_tree_fraction near 1) now enters the per-tree mean as a zero. On main it was counted in the divisor but not in the running mean, so how much it weighed depended on where it fell in its thread's sequence of trees, and MDA for such fits changes by more than rounding.

    Performance: to be re-measured for the tree-order OOB pass.

    Tests: df cls responses in the other float type and df reg responses in the other float type in //cpp/oneapi/dal/algo/decision_forest:tests train the same forest twice, once with float and once with double responses holding the same float-representable values, and require bitwise-equal predictions, per-observation OOB error and MDI importance. On main they fail in every run (the importance for classification, the OOB error or the importance for regression); with this PR they pass. //cpp/oneapi/dal/algo/decision_forest:tests passes.

    No public API or ABI changes.

    ---

    <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. (not applicable)

  • 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. (in-repo timing, ratios above)

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected.

  • I have extended the benchmarking suite and provided a corresponding scikit-learn_bench PR if new measurable functionality was introduced in this PR. (not applicable)
  • </details>

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Vika-F Review duration: 28176m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3911"
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%