fix(basic_statistics): merge the centered sums of squares without cancellation on CPU and GPU #4014
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
Vika-F
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:
coeff (m1m1 + m2m2 - 2m1m2). Over the reals it equalscoeff (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)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 isupdate_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)^2on each rank before one allreduce (allreduce_sums2cent). Before, the per-rank sums centered on different means were summed directly.-
compute_all_metricsno longer overwrites the merged values.GPU
finalize_computealso 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'scheck_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
mainand pass with this PR. Allbasic_statisticstargets pass with--config=hostand with--config=dpc --device=gpuon an Intel GPU.Not covered:
Merge Danger
Door: two-way
Blast Radius: numeric
Online and distributed variance, standard deviation, variation and
sum_squares_centeredchange only within rounding for well-conditioned data; large-offset data becomes correct. On GPU thepartial_sum_squares_centeredof 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:148covariance_impl.i:850-852and the CSR covariance merge🤖 Generated with Claude Code