← Back to Pull requests

fix(basic_statistics): merge the centered sums of squares without cancellation on CPU and GPU #4014

Closed
unknown closed this pull request without merging on Nov 19, 2026 12:36 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 02:55 UTC
Author

Summary

Merging partial results of low order moments / basic statistics lost the variance when the data sit far from zero. Two causes, one per device:

  • CPU (DAAL). The between-block term of the merge was added in expanded form, coeff (m1m1 + m2m2 - 2m1m2). Over the reals it equals coeff (m2 - m1)^2, but in double with means near 1e8 the expanded form cancels catastrophically. All five sites now use (m2 - m1)^2:

  • - low_order_moments_impl.i (mergeSums: CSR online and distributed step 2)
    - low_order_moments_estimates_online.i (dense online)
  • GPU (oneAPI basic statistics). The centered sums of squares were never merged. They were rederived from the sums as sum_squares - sum^2 / n, which cancels the same way.

  • - Online blocks merge with Chan et al.'s update: ssc1 + ssc2 + n1 n2 / n (mean2 - mean1)^2. This is update_partial_sums, for both dense and CSR.
    - The first CSR block takes the kernel's own centered sum (stat::sum2_cent).
    - SPMD finalize adds n_r * (mean_r - mean)^2 on each rank before one allreduce (allreduce_sums2cent). Before, the per-rank sums centered on different means were summed directly.
    - compute_all_metrics no longer overwrites the merged values.

    GPU finalize_compute also wrote into the caller's partial result: min, max and the sums were allreduced in place. Every array it reduces is now copied first (copy_for_allreduce), so a partial result can be finalized again or extended with more blocks.

    Tests

    The large-offset tests build their data with te::dataframe_builder. The CSR and SPMD ones add the 1e8 offset in double, since float cannot hold a deviation of 1 at 1e8. They check through the fixture's check_vs_reference, which now takes a tolerance (default unchanged at 1e-2); they use 1e-6.

    | Test | Path | Devices |
    |---|---|---|
    | basic_statistics online merge with a large offset (online.cpp) | dense online, 3 blocks | CPU, GPU |
    | basic_statistics online CSR merge with a large offset (online_csr.cpp) | CSR online, one dense and one 1-in-4 sparse column | GPU |
    | basic_statistics online spmd merge with a large offset (online_spmd.cpp) | 2 and 4 ranks, second half shifted so the rank means differ | GPU |
    | basic_statistics finalize twice on the same partial result (online.cpp) | finalize must not modify the partial result | CPU, GPU |

    The CSR and SPMD large-offset tests fail with the merge sources from main and pass with this PR. All basic_statistics targets pass with --config=host and with --config=dpc --device=gpu on an Intel GPU.

    Not covered:

  • The CSR test skips the CPU, because the CPU CSR kernel densifies its input first. #4037 fixes that.

  • The SPMD suite runs only on GPU, so DAAL's distributed step 2 has no large-offset test.
  • Merge Danger

    Door: two-way

    Blast Radius: numeric

    Online and distributed variance, standard deviation, variation and sum_squares_centered change only within rounding for well-conditioned data; large-offset data becomes correct. On GPU the partial_sum_squares_centered of a partial result is now the merged centered sum, where before it was a value finalize recomputed anyway. No API or ABI change.

    Follow-ups, not in this PR, for the same class of cancellation:

  • pca_dense_svd_online_impl.i:148

  • covariance_impl.i:850-852 and the CSR covariance merge
  • 🤖 Generated with Claude Code

    Diff: 429 lines (+321 / -108) across 5 files Reviewer: Vika-F Review duration: 10661m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.45) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 429 lines diff across 5 files
    0.45
    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%
    56%
    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.363
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4014"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 45.1% (MEDIUM) · Higher = larger blast radius
    45.1%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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.5% · Safe to merge
    19.5%