Skip to content

StandardScaler output differs across platforms enough to move downstream diagnostics #461

Closed

Description

@godofecht

Observed during the #437 investigation; recording so the next person comparing local diagnostics against committed evidence does not chase a phantom.

The observation

The committed sklearn_results_v2.txt records RandomForest digits unanimity 0.197222; the same measurement locally (macOS/Accelerate) gives 0.205556 — 3 of 360 test rows — while the per-tree node counts match exactly. Identical fitted trees producing different vote patterns means the inputs differ: StandardScaler's scaled test matrix is not bit-identical across platforms, and rows near a split threshold land on different sides.

Most likely cause: f32 accumulation order in the mean/variance passes differing between platforms/compilers, the same reassociation class as #397/#377 established for BLAS — but this is scalar library code, so it is Flow codegen or libm differences rather than a BLAS vendor.

Why it is worth an issue rather than a shrug

What would settle it

  1. Instrument standard_scaler_fit/transform to dump mean/std/first-row bits on both platforms and confirm where the first ulp difference enters.
  2. If it is summation order: accumulate mean/variance in f64 (the pattern feat: Newton / IRLS solver for logistic regression, LBFGS stays the default #443 used for its line-search objective, where f32 accumulation demonstrably changed decisions), which should make the scaler bit-stable across platforms at negligible cost.
  3. Re-freeze evidence once, and note in benchmarks/README.md that pre-fix snapshots carry the artifact.

Low urgency, real payoff: it converts "diagnostics differ, is that a bug?" from a recurring investigation into a non-event.

Activity

  1. godofecht commented on Aug 20, 2026

    @godofecht
    OwnerAuthor

    Correcting the diagnosis I filed this issue with, before the PR closes it.

    The premise was wrong on two counts, established by PR #465's investigation:

    1. standard_scaler_fit was already f64 two-pass accumulation (since commit 9b12016, predating this issue). All 64 digits columns produce the correctly rounded f32 mean and std, verified against exact rational arithmetic — the tightest column sits 4.9e-10 relative from a rounding boundary, four orders of magnitude beyond what an f64 sum can disturb.

    2. The 0.197222 vs 0.205556 unanimity difference is not cross-platform drift. 0.197222 comes from sklearn_results_v2.txt — the Python side — and reproduces exactly on macOS. The cause: bench_sklearn_v2.py casts to float32 before fitting, so sklearn's StandardScaler.transform evaluates (x - mean_)/scale_ in f32 with one rounding, while an f64-scale-then-round path differs in 7412 of 23040 test cells' last bits, flipping 3 rows. Same machine, two precision paths. The committed snapshot is correct as recorded and needs no re-freeze.

    What the PR delivers anyway, and why it still merges: four other reductions in preprocessing.flow had exactly the hazard this issue described and are widened to f64 — normalizer_transform L1/L2 norms, power_transformer_transform mean/variance, the Yeo-Johnson log-likelihood in power_transformer_fit (where a drifting total picks a different lambda, not just a different last bit), and kbins_discretizer_fit sums. Order-independence of MinMax/MaxAbs/Robust is now asserted by a permutation test rather than assumed. All 165 canonical records unchanged; KMeans bit-identical.

    Lesson recorded for the next reader: identical trees + different votes means different inputs, but "different inputs" has more than one cause — check which side of the comparison produced the committed number before assuming the platform.

  2. added a commit that references this issue on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions