You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Diabetes linear-family fitted coefficients diverge from sklearn: Lasso 6.9%, Ridge 7e-5, OLS at the collinear S1/S2 index #472
From the substantive-disparity survey. Three related rows on diabetes:
Lasso: coef_max_relative_diff = 0.069, divergent from index 0. L1 problems on correlated designs are not strictly convex, so different solvers can reach different coefficient vectors with equal objective. The decisive measurement, same method as the SMO diagnosis on Close the Digits KernelSVC quality gap and isolate the libsvm performance deficit #206: compute both sides' Lasso objective (0.5/m·||y-Xw||² + alpha·||w||₁, matching each side's convention) at each side's coefficients. Equal objectives → different-but-equal optimum, declare it. Flow's worse → convergence gap in the new Gram solver (perf: Gram-matrix coordinate descent for Lasso, 74x on the canonical diabetes row #427), fix it.
OLS: coef_first_divergent_index = 4. Diabetes features S1/S2 correlate at ~0.9, so coefficients on those coordinates are ill-conditioned: f32 QR vs sklearn's f64 SVD can legitimately disagree there while predictions match. Measure prediction agreement and the design's condition number; if predictions match within f32, declare as conditioning.
Ridge: coef_max_relative_diff = 6.9e-5 — small; likely solver-path (Flow QR+alpha·I vs sklearn cholesky). Quantify and declare or fix.
This is analysis-first: post the objective/conditioning numbers to this issue before changing any code. Only fix code if a genuine convergence or correctness gap emerges.
Analysis: all three rows are declare, none is a Flow defect
Measured before touching code, per the instruction in the issue. No code changed.
Method
Both sides' fitted coefficients are already committed at 9 significant figures in benchmarks/flow_results_v2.txt and benchmarks/sklearn_results_v2.txt, which is
enough to recover the f32 values exactly. Objectives, conditioning and predictions
were evaluated externally in numpy f64 on the shared design.
First I checked the design matrices are the same object on both sides. Flow's standard_scaler_fit computes mean and variance two-pass in f64 and rounds to f32;
sklearn's StandardScaler on a float32 input keeps mean_ in f64 and returns f32.
Reproducing Flow's scaler in numpy against sklearn's on the canonical diabetes split:
max |Xtr_sklearn - Xtr_flow| = 0.0
bitwise identical: True
So every difference below is the solver, not the data.
1. Lasso: Flow's objective is lower. sklearn stopped early.
Convention check first. Flow's lasso_fit sets threshold = alpha * m and minimises 0.5*||y - Xc*w||^2 + threshold*||w||_1, which is (1/(2m))*||y - Xw - b||^2 + alpha*||w||_1 after dividing through by m. That is sklearn's convention exactly.
Alpha is not scaled differently.
Objective at each side's coefficients, alpha = 0.1, m = 353:
Flow sits 3.2e-12 above the true optimum, which is the f32 output-rounding floor.
sklearn sits 7.7e-5 above it. Subgradient (KKT) optimality on the centred design
gives the same ordering:
max subgradient violation, Flow : 1.29e-06
max subgradient violation, sklearn : 1.71e-03
max subgradient violation, reference: 3.06e-13
The shipped sklearn call is Lasso(alpha=0.1, max_iter=1000) with default tol=1e-4.
It reports n_iter_ = 246 and dual_gap_ = 0.599. sklearn's stopping rule compares
the duality gap against tol * ||y||^2, and ||y||^2 here is 1.05e7, so the
threshold is 1048.81. It stopped on its own criterion long before the optimum.
The 6.9% is coefficient index 6, the smallest coefficient in the vector:
index 6: Flow 0.588628 sklearn 0.547944 true optimum 0.588628
Flow is on the optimum. The relative metric is large because the denominator is small.
Per-coefficient distance from the true optimum: Flow max 3.68e-06 relative, sklearn max
6.91e-02 relative.
I also reproduced Flow's Gram coordinate descent sweep for sweep to check #427's note
that the 1e-16 tolerance never fires. It does not fire; all 1000 sweeps run, ending at max_change = 1.43e-09, which is the f64 floor for this problem. The budget is not
wasted though: the float32-rounded coefficient vector stops moving at sweep 993. The
objective is within 6e-09 of optimal by sweep 500 and within 1e-12 by sweep 1000.
after 1 sweeps: max_change=4.43e+01 objective=1715.468216063112
after 100 sweeps: max_change=2.53e-02 objective=1450.509028468972
after 500 sweeps: max_change=1.55e-05 objective=1450.491383733015
after 1000 sweeps: max_change=1.43e-09 objective=1450.491383726853
Held-out predictions differ by at most 0.0268 absolute (4.39e-04 relative). Test r2 is
0.45552983 for Flow and 0.45554140 for sklearn. sklearn's early stop scores 1.16e-05
higher on the test split, which is noise on a 89-row split and not evidence of a better
fit; its training objective is worse by 7.7e-05.
Verdict: no convergence gap in the #427 Gram solver. Flow reaches the optimum;
sklearn stops at its default tolerance. Nothing to fix.
2. OLS: ill-conditioning, and the f32/f64 roles are the other way round
Flow is closer to the exact solution at nine of ten indices; max relative distance
3.33e-07 for Flow against 5.2e-06 for sklearn. Training residual sum of squares:
Held-out predictions agree to 6.68e-05 absolute, 1.19e-06 relative, roughly two f32
ulps at that magnitude. Test r2 0.45260275 (Flow) against 0.45260270 (sklearn).
The index pattern is the conditioning signature. Injecting f32-ulp jitter into X and
refitting 200 times gives per-coefficient standard deviations that track the observed
divergence closely:
Indices 4 through 8 are the amplified directions. Index 4 is the first to cross the
report's 1e-9 + 1e-6 * max(|a|,|b|) divergence threshold, which is why coef_first_divergent_index = 4.
One correction to the issue text. It describes this as "f32 QR vs sklearn's f64 SVD".
It is the reverse. linear_regression_fit centres in f64 and runs Householder QR on the
centred f64 design, narrowing to f32 only when it writes weights. sklearn's LinearRegression passes the input dtype straight to scipy.linalg.lstsq, and the
canonical harness feeds it float32, so sklearn's coef_ comes back float32 and its lstsq ran in single precision. Refitting sklearn locally on the identical float32
input reproduces this: its f32 answer sits 7.02e-06 relative from its own f64 answer,
and it differs from the committed sklearn run at the same magnitude, so this row's f32
noise is BLAS- and machine-dependent on sklearn's side.
Verdict: ill-conditioning amplifying sklearn's single-precision lstsq. Predictions
agree within f32. Nothing to fix.
3. Ridge: alpha convention matches. The 6.9e-5 is sklearn's f32 Cholesky.
Convention checked explicitly, since this is where #430 started. ridge_fit builds Xc^T Xc from the centred design, adds alpha (not alpha * m) to the diagonal, and
solves in f64. sklearn Ridge penalises alpha * ||w||^2 against the sum-of-squares
loss. Running sklearn's Ridge(alpha=1.0) on the same design in f64 and comparing to
Flow's centred normal equations solved in f64:
Flow is at or under one f32 ulp at eight of ten indices. The coef_max_relative_diff = 6.9e-5 is index 6, where sklearn's shipped f32 run is 964 ulps from the exact answer
and Flow is 8.5. Ridge objective ||y - Xw - b||^2 + ||w||^2:
Held-out predictions differ by 2.15e-04 absolute, 2.86e-06 relative. Test r2 0.45414651
(Flow) against 0.45414656 (sklearn).
Verdict: neither f32 storage on Flow's side nor an alpha convention mismatch. It is
sklearn's Cholesky running in float32 on a float32 input. Flow keeps the whole solve in
f64 and narrows once at the end. Nothing to fix.
Summary
row
reported metric
cause
Flow vs sklearn accuracy
Lasso
coef_max_relative_diff = 0.069
sklearn stops at tol=1e-4 after 246 iterations
Flow objective lower by 7.74e-05; Flow is on the optimum to 3.2e-12
Flow 3.33e-07 from exact, sklearn 5.2e-06; predictions agree to 1.19e-06
Ridge
coef_max_relative_diff = 6.9e-5
sklearn's f32 Cholesky
Flow under 1 f32 ulp at 8/10 indices, sklearn up to 964 ulps
No genuine convergence or correctness gap. No PR. The canonical RESULT| records are
untouched: LinearRegression r2 0.452602565, Ridge 0.454146445, Lasso 0.455529690.
On how to record it: I did not build the KMeans-style residual_semantic_differences
plumbing. That pattern needs a committed audit JSON plus a hardcoded branch in generate_disparity_report.py, and all three of these rows already pass their gates
(parity verified for LinearRegression, approximately equivalent for Ridge and
Lasso), so nothing is red and the plumbing would be documentation only. If a durable
record is wanted, the cheaper form is a short note in benchmarks/README.md next to the
existing model-state diagnostics section saying that these three model_state_diagnostics entries are sklearn-side precision and tolerance artefacts,
with a link back here. Happy to do that as a small follow-up if you want it.
Activity
godofecht commentedon Aug 21, 2026
Analysis: all three rows are declare, none is a Flow defect
Measured before touching code, per the instruction in the issue. No code changed.
Method
Both sides' fitted coefficients are already committed at 9 significant figures in
benchmarks/flow_results_v2.txtandbenchmarks/sklearn_results_v2.txt, which isenough to recover the f32 values exactly. Objectives, conditioning and predictions
were evaluated externally in numpy f64 on the shared design.
First I checked the design matrices are the same object on both sides. Flow's
standard_scaler_fitcomputes mean and variance two-pass in f64 and rounds to f32;sklearn's
StandardScaleron a float32 input keepsmean_in f64 and returns f32.Reproducing Flow's scaler in numpy against sklearn's on the canonical diabetes split:
So every difference below is the solver, not the data.
1. Lasso: Flow's objective is lower. sklearn stopped early.
Convention check first. Flow's
lasso_fitsetsthreshold = alpha * mand minimises0.5*||y - Xc*w||^2 + threshold*||w||_1, which is(1/(2m))*||y - Xw - b||^2 + alpha*||w||_1after dividing through bym. That is sklearn's convention exactly.Alpha is not scaled differently.
Objective at each side's coefficients, alpha = 0.1, m = 353:
tol=1e-14,max_iter=1e6)Flow sits 3.2e-12 above the true optimum, which is the f32 output-rounding floor.
sklearn sits 7.7e-5 above it. Subgradient (KKT) optimality on the centred design
gives the same ordering:
The shipped sklearn call is
Lasso(alpha=0.1, max_iter=1000)with defaulttol=1e-4.It reports
n_iter_ = 246anddual_gap_ = 0.599. sklearn's stopping rule comparesthe duality gap against
tol * ||y||^2, and||y||^2here is 1.05e7, so thethreshold is 1048.81. It stopped on its own criterion long before the optimum.
The 6.9% is coefficient index 6, the smallest coefficient in the vector:
Flow is on the optimum. The relative metric is large because the denominator is small.
Per-coefficient distance from the true optimum: Flow max 3.68e-06 relative, sklearn max
6.91e-02 relative.
I also reproduced Flow's Gram coordinate descent sweep for sweep to check #427's note
that the
1e-16tolerance never fires. It does not fire; all 1000 sweeps run, ending atmax_change = 1.43e-09, which is the f64 floor for this problem. The budget is notwasted though: the float32-rounded coefficient vector stops moving at sweep 993. The
objective is within 6e-09 of optimal by sweep 500 and within 1e-12 by sweep 1000.
Held-out predictions differ by at most 0.0268 absolute (4.39e-04 relative). Test r2 is
0.45552983 for Flow and 0.45554140 for sklearn. sklearn's early stop scores 1.16e-05
higher on the test split, which is noise on a 89-row split and not evidence of a better
fit; its training objective is worse by 7.7e-05.
Verdict: no convergence gap in the #427 Gram solver. Flow reaches the optimum;
sklearn stops at its default tolerance. Nothing to fix.
2. OLS: ill-conditioning, and the f32/f64 roles are the other way round
Conditioning of the centred training design:
Coefficients against the exact f64 least-squares solution:
Flow is closer to the exact solution at nine of ten indices; max relative distance
3.33e-07 for Flow against 5.2e-06 for sklearn. Training residual sum of squares:
Held-out predictions agree to 6.68e-05 absolute, 1.19e-06 relative, roughly two f32
ulps at that magnitude. Test r2 0.45260275 (Flow) against 0.45260270 (sklearn).
The index pattern is the conditioning signature. Injecting f32-ulp jitter into X and
refitting 200 times gives per-coefficient standard deviations that track the observed
divergence closely:
Indices 4 through 8 are the amplified directions. Index 4 is the first to cross the
report's
1e-9 + 1e-6 * max(|a|,|b|)divergence threshold, which is whycoef_first_divergent_index = 4.One correction to the issue text. It describes this as "f32 QR vs sklearn's f64 SVD".
It is the reverse.
linear_regression_fitcentres in f64 and runs Householder QR on thecentred f64 design, narrowing to f32 only when it writes
weights. sklearn'sLinearRegressionpasses the input dtype straight toscipy.linalg.lstsq, and thecanonical harness feeds it float32, so sklearn's
coef_comes back float32 and itslstsqran in single precision. Refitting sklearn locally on the identical float32input reproduces this: its f32 answer sits 7.02e-06 relative from its own f64 answer,
and it differs from the committed sklearn run at the same magnitude, so this row's f32
noise is BLAS- and machine-dependent on sklearn's side.
Verdict: ill-conditioning amplifying sklearn's single-precision lstsq. Predictions
agree within f32. Nothing to fix.
3. Ridge: alpha convention matches. The 6.9e-5 is sklearn's f32 Cholesky.
Convention checked explicitly, since this is where #430 started.
ridge_fitbuildsXc^T Xcfrom the centred design, addsalpha(notalpha * m) to the diagonal, andsolves in f64. sklearn
Ridgepenalisesalpha * ||w||^2against the sum-of-squaresloss. Running sklearn's
Ridge(alpha=1.0)on the same design in f64 and comparing toFlow's centred normal equations solved in f64:
Identical problem. No convention mismatch.
Against that exact f64 solution:
Distance from exact, measured in f32 ulps:
Flow is at or under one f32 ulp at eight of ten indices. The
coef_max_relative_diff = 6.9e-5is index 6, where sklearn's shipped f32 run is 964 ulps from the exact answerand Flow is 8.5. Ridge objective
||y - Xw - b||^2 + ||w||^2:Held-out predictions differ by 2.15e-04 absolute, 2.86e-06 relative. Test r2 0.45414651
(Flow) against 0.45414656 (sklearn).
Verdict: neither f32 storage on Flow's side nor an alpha convention mismatch. It is
sklearn's Cholesky running in float32 on a float32 input. Flow keeps the whole solve in
f64 and narrows once at the end. Nothing to fix.
Summary
coef_max_relative_diff = 0.069tol=1e-4after 246 iterationscoef_first_divergent_index = 4lstsqcoef_max_relative_diff = 6.9e-5No genuine convergence or correctness gap. No PR. The canonical
RESULT|records areuntouched: LinearRegression r2 0.452602565, Ridge 0.454146445, Lasso 0.455529690.
On how to record it: I did not build the KMeans-style
residual_semantic_differencesplumbing. That pattern needs a committed audit JSON plus a hardcoded branch in
generate_disparity_report.py, and all three of these rows already pass their gates(
parity verifiedfor LinearRegression,approximately equivalentfor Ridge andLasso), so nothing is red and the plumbing would be documentation only. If a durable
record is wanted, the cheaper form is a short note in
benchmarks/README.mdnext to theexisting model-state diagnostics section saying that these three
model_state_diagnosticsentries are sklearn-side precision and tolerance artefacts,with a link back here. Happy to do that as a small follow-up if you want it.
docs: record #472 verdict on diabetes linear-family diagnostics (#477)