Skip to content

Scale update: roll back when the solve for g fails its check - #559

Draft
bodono wants to merge 1 commit into
masterfrom
claude/g-check-scale-rollback
Draft

bodono wants to merge 1 commit into
masterfrom
claude/g-check-scale-rollback

Conversation

@bodono

@bodono bodono commented Nov 23, 2026

Copy link
Copy Markdown
Member

At a scale update, when the solve for g = K^{-1}(c, -b) fails the check of #528 with the new R, SCS now goes back to the previous R and continues. Before, it stopped with SCS_FAILED and the "This should never happen ... bug in SCS" message. The setup of every solve keeps the hard check, so a broken linear solver still fails the first solve.

Decision (maintainer, 2026-11-23): "Roll back the scale".

Why

The check exists to catch broken linear solvers. At a scale update it can also fire on a working solver at the limit of what it can do with the new R.

beaconfd (Netlib LP, n 262, m 436), CPU indirect solver, eps 1e-4, max_iters 700, on 64-bit ARM Linux with NVPL. Start uK moves the initial scale by K ulps.

  • Master stopped with the check's message at 3 of 564 starts (u160, u375 and u432). The refined residuals were 0.57 to 0.69 of the size of their terms, against G_FAIL_REL_RES = 0.5.
  • The state studied in detail was found at u11 with the eigCG change on GPU indirect solver: eigCG deflation, as the CPU solver (shared linsys/eigcg.c) #558's branch. Replayed in a fresh process, master's code fails it the same way, bit for bit (ratio 0.7258).
    • The scale had risen from 0.1 to 3.918 in two updates (iterations 500 and 600).
    • The per-row multipliers were at both clamps (107 of 436 rows at 1e-3, 219 at 1e3), so R_y spanned 2.6e-7 to 255.
    • CG ran its cap of 10 n = 2620 iterations without a correct digit, and so did each refinement solve. The refined residual was 0.73 of the size of its terms.
  • With QDLDL, beaconfd solved at every start measured on the Mac (u0..u63 at eps 1e-4 and 1e-6: 128 of 128 runs per arm verified solved, below).

What changes

  • update_work_cache(w, report). report is 1 at setup and at a restore (the message, as before), and 0 at a scale update's attempt (silent).
  • update_scale. If the solve for g fails at the new R, it calls scale_update_rollback and returns. The acceleration reset, the cone cache reset and the remap of v run only after a solve for g that passes. scale_updates is incremented only then, so it counts the updates kept.
  • scale_update_rollback puts back:
    • stgs->scale;
    • the per-row multipliers;
    • diag_r (set_diag_r, a pure function of the same values, so the same bits);
    • the linear solver's R (scs_update_lin_sys_diag_r);
    • g, solved again at the previous R with the hard check (update_work_cache(w, 1)).
  • Multipliers by swap. An update now installs the multipliers by swapping scale_mults and scale_mults_next instead of memcpy. The previous ones stay in scale_mults_next (scratch, rebuilt in full at each decision), and a rollback swaps them back. No copy, no new memory.
  • Reporting. scale_rollbacks (internal, scs_work.h, reset per solve) counts rollbacks for the tests. With verbose on, each rollback prints one line: scale update at iteration N rolled back: K g = (c, -b) could not be solved accurately with the new R (scale X); continuing with the previous R (scale Y).
  • Comments and docs. The G_FAIL_REL_RES comment (include/glbopts.h) covers both cases. Docs: linear_solver/index.rst (if the solve for g fails at the start of a solve, and after a scale update), algorithm/scale.rst, and api/info.rst (scale_updates counts only the updates kept).

No new setting and no API change. The decision is the check's result, a function of the iterates and the linear solver. It is deterministic and the same on every backend.

How the solve continues after a rollback

  • Untouched: Anderson acceleration's memory, the cone cache (the warm starts of the spectral projections), v and u, and the per-iteration refinement's state. The metric is back to the one they were computed in.

  • Direct solvers. The restore refactors from the same diag_p + diag_r values. Where the numeric factorization is a deterministic function of the values, g comes back as the same bits, and the iterates continue bit for bit as if the update had never been attempted. That covers QDLDL, LAPACK potrf, Accelerate's SparseRefactor against its SparseFactor (the test checks it), MKL Pardiso on a fixed number of threads, and cuDSS in its deterministic mode.

  • CPU CG with eigCG deflation. The update resets the deflation basis, so the restore's solve for g harvests it again. The basis and g come back bit for bit under two conditions:

    • the solve for g that installed the previous R also started with no basis (always after a scale update, and in the first solve after scs_init);
    • no iteration's solve has harvested since (possible only if that solve for g kept no vector).

    Otherwise, as in a later scs_solve on the same workspace before its first kept update, the rollback gives a freshly harvested basis and its g. That is deterministic and checked, but not the bits of never attempting.

  • Pacing. The attempt clears the samples and sets last_scale_update_iter before the solve for g, as an update does. So the next attempt comes at least RESCALING_MIN_ITERS (100) iterations later, from new samples. There is no new threshold.

  • Cost. A rollback costs the attempt (a factorization or preconditioner at the new R, and its failed solve for g with refinement) plus the restore (the same at the previous R, with one solve for g).

  • If the restore fails (the solver update returns < 0, or g fails the check at the previous R), SCS stops with SCS_FAILED and the message, as at setup.

What stays

  • Setup keeps the hard check. update_work still stops on it, so a broken linear solver stops the first solve with SCS_FAILED and the message. test_g_accuracy checks 4 and 5 are unchanged.
  • A failed factorization at the new R still stops the solve (scs_update_lin_sys_diag_r < 0), as before.
  • Pre-existing. The dense solver returns potrf's info > 0 on a failed factorization, which update_scale does not treat as an error (it tests < 0; linsys.h says nonzero is failure). On this branch such a factorization fails the solve for g and is rolled back.

Tests

test_g_rollback (test/problems/test_g_accuracy.h; MU_TOTAL_TESTS 107).

  • The mechanism. After scs_init the test points w->diag_r at an array of its own, as test_g_accuracy check 4 and test_refine_perturbed do. The linear solver then follows each attempted update, while SCS's products keep measuring the R of setup.

  • Every attempt fails. At scale 1e-12 every update raises the scale at least 1e6-fold (to the floor 1e-6). Every attempt then fails the check (measured ratio 5e5 to 6e6, against 0.5) and is rolled back.

  • The assertion. So the solve must be bit-identical (status, iterations, x, y, s, residuals) to the same solve with adaptive_scale 0. The test also checks: at least one rollback, at most one per RESCALING_MIN_ITERS, no update kept, the multipliers back at 1, the verbose line, and no bug-report message.

  • Skipped where the solver measures with its own K (MKL Pardiso, cuDSS from CUDSS_MIN_PRODUCT_NNZ), as check 4 is.

  • Measured, in every build:

    • run 1 (1000 iterations, adaptive_diag_scale 0) has 9 rollbacks, at iterations 100 to 900;
    • run 2 (150 iterations, adaptive_diag_scale 1) has 1 rollback, of a scalar plus per-row update.
  • Mutations (review, QDLDL and CG). The test fails on 6 of 7 mutants:

    • the multipliers not swapped back;
    • g not recomputed at the restore;
    • the scalar scale not restored;
    • the acceleration reset before the check;
    • no pacing after a rollback;
    • the solver not given the previous R back.

    It passes with the cone cache reset at a rollback, since its problem has no spectral cone. That case is not covered.

On the Mac (Apple M4, Apple clang):

  • Make, master and branch: default, SFLOAT=1, USE_LAPACK=0, CFLAGS=-DDEFLATE_VECTORS=0 and DLONG=1. Every run_tests_* binary prints ALL TESTS PASSED (direct, indirect, dense where LAPACK, accelerate where not DLONG; 18 per side).
  • CMake (default options) + ctest: 3 of 3 on both.
  • Suite logs, branch against master, timings removed. Only test_g_rollback and the test count line differ. That holds for QDLDL, CG and dense in all 5 builds and the 3 ctest binaries, and for Accelerate at VECLIB_MAXIMUM_THREADS=1. At default threading, Accelerate's factorization varies between processes on master too.
  • test_g_rollback repeated, 200 processes x 5 calls per driver (double and single; QDLDL, CG, dense and Accelerate): 8,000 of 8,000 pass.
  • test_g_accuracy checks 4 and 5: unchanged output (SCS_FAILED with the message) in every binary.
  • Sphinx: no warning from the three edited pages.

Measurement (Mac, Apple Accelerate, one thread)

Master (3238373) against this branch:

  • Driver: a driver that reads .scs files, with each file's settings, then eps_abs = eps_rel = eps. max_iters is 700 on beaconfd and 1e5 otherwise, with no time limit.
  • Verification: every status is checked independently against the KKT conditions at 10 x eps, in all 2,936 main runs.

beaconfd, CPU indirect: no failure, so no rollback.

  • No failures. Master failed the check at none of 564 starts at eps 1e-4 (u0..u563) and none of 128 at 1e-6 (u0..u127). The branch rolled back in none of its runs.
  • Same regime as the failures. The runs reached it without failing:
    • 522 of 564 runs at 1e-4 end at 700 iterations;
    • 71 end at a scale of 2 or more, and 5 at 3.9 or more (maximum 4.57).
  • Against NVPL. Against NVPL's 3 of 564, the two-sided Fisher exact p is 0.25: within chance. Each BLAS follows its own trajectories.
  • Verified outcomes, identical in both arms:
eps starts solved inaccurate certificate diverged failure
1e-4 564 43 174 347 0
1e-6 128 9 34 85 0

At 20 starts at 1e-4 and 3 at 1e-6, status_val is SCS_FAILED from set_unfinished (tau <= 0 at max_iters, status "inaccurate - reached max_iters"), identically in both arms. That path is not the g check.

Where nothing is rolled back, nothing changes. There were 1,468 master/branch pairs:

  • beaconfd: CG at both eps, and QDLDL at u0..u63 at both eps;
  • at u0, eps 1e-4 and 1e-6, CG and QDLDL: the 53 test-suite problems, 9 other problems (among them socp1000 and max_ent) and 100 random SOCPs (n 10 to 100).

All 1,468 agree in status, iterations, scale updates, scale, the primal, dual and infeasibility residuals, the CG iterations, and the x, y and s hashes. Two of them are a QDLDL scs_init failure on one test-suite problem, with the same message in both arms. With QDLDL, all 128 beaconfd runs of each arm are verified solved.

Four pairs differ in the last digits of pobj and gap (and res_unbdd_a): socp1000 three times and max_ent once. The cause is Accelerate's reductions, not this change:

  • socp1000. Master varies the same way against itself between processes.
  • USE_LAPACK=0. Master and branch are identical in every field in all 48 runs of the 8 cells.
  • max_ent CG 1e-4. The measured branch binary gives ...097 against master's ...096 every time. An independent build of the same commit gives master's value in 41 of 41 runs (review).

Forced rollbacks (review, Mac). These test the continuation on real problems with a non-empty eigCG basis.

  • Setup. A scratch patch attempted every other update decision in full and then treated it as failed ("force"). It was compared with a tree that only paces those decisions ("skip", as if never attempted).
  • Cells. 23 cells: issue_140, max_ent, socp1000, socp100, random_prob and issue_220 with CG and QDLDL, plus beaconfd CG u0..u3 and QDLDL u0. Each had 1 to 6 rollbacks.
  • Result. Force and skip agree in status, iterations, scale, residuals, objectives and the x/y/s hashes in 22 of 23: all 10 QDLDL cells and 12 of 13 CG cells.
  • The exception, max_ent CG 1e-4, already differed between the two processes at setup, before any update decision: the first harvest kept 3 vectors in one and 5 in the other.
  • Direct check of the restore. A second instrumented tree hashed g, the deflation basis, its small factor and the preconditioner before each forced attempt and after its restore. They were identical at all 19 forced rollbacks printed, with bases of 3 to 30 vectors.

A forced failure is not a natural one.

Still to run

  • The known failing starts on NVPL (64-bit ARM Linux, one thread), once that machine is free (2026-11-24). Master and this branch at beaconfd u160, u375 and u432, eps 1e-4.
    • Expected: master stops with the check's message, and the branch rolls back and continues.
    • Also to run: the rollbacks' iterations and cost (verbose); a comparison against a build that skips the same updates (bit identity on a natural failing state, repeated per arm); and prefix identity up to the failing update.
    • This is the only test of a natural, not forced, failure, and the only measurement of a rollback's cost.
  • u11 with GPU indirect solver: eigCG deflation, as the CPU solver (shared linsys/eigcg.c) #558's eigCG change: needs a tree with both changes, or a run after both merge.
  • Not run: GPU, cuDSS, MKL and other BLAS libraries; a NO_PRINTING build; valgrind.

🤖 Generated with Claude Code

At a scale update, if the solve for g = K^{-1}(c, -b) fails with the new R
(update_work_cache, the check of #528), SCS now goes back to the previous R
and continues instead of stopping with SCS_FAILED and the "This should never
happen ... bug in SCS" message. It puts back the scale and the per-row
multipliers, gives the linear solver the previous R again and solves for g
there; it stops as before only if that fails too. The setup of every solve
keeps the hard check, so a broken linear solver still fails the first solve
(test_g_accuracy, checks 4 and 5, unchanged).

Why: the check exists to catch broken linear solvers, but at a scale update
it can also fire on a working solver at the limit of what it can do with the
new R. On beaconfd (Netlib LP, n 262, m 436) with the CPU indirect solver, a
diverged trajectory moved the scale from 0.1 to 3.9 with the per-row
multipliers at both clamps (R_y spanning 1e9); CG ran its 10 n = 2620
iterations without a correct digit, refinement too (0.73 of the size of the
terms, above G_FAIL_REL_RES 0.5). Master stopped that way at 3 of 564 starts
(eps 1e-4). No direct solver has done it (0 of 3,700 benchmark rows).

- update_work_cache(w, report): report 1 at setup and at the restore (the
  message, as before), 0 at the attempt (silent).
- scale_update_rollback: restores the scale, the multipliers (an update now
  installs them by swapping scale_mults and scale_mults_next, so the
  previous ones stay there; no copy), diag_r (same function of the same
  values), the solver's R, and g (recomputed, which also rebuilds CG's eigCG
  basis). The acceleration, the cone cache and v are untouched, so the
  iterates continue as if the update had not been attempted: bit for bit
  where the solver's factorization is reproducible; with CG in the first
  solve after scs_init (the comment states the two cases where the rebuilt
  deflation basis can differ).
- Paced as an update: the next attempt comes at least RESCALING_MIN_ITERS
  iterations later, from new samples. A rollback costs the attempt plus one
  more factorization (or preconditioner) and solve for g.
- scale_updates counts only the updates kept; an internal counter
  scale_rollbacks (scs_work.h) for the tests; one line when verbose. No API
  change, no setting. Deterministic, the same on every backend.
- test_g_rollback: after scs_init the test points w->diag_r at an array of
  its own (the mirror of check 4 and test_refine_perturbed), so the solver
  follows each attempted update while SCS's products keep measuring the R
  of setup; at scale 1e-12 every update raises the scale at least 1e6-fold
  and the check fails by 5e5 or more. Every attempt is rolled back, so the
  solve must be bit-identical to the same solve with adaptive_scale off;
  also: at least one rollback, at most one per RESCALING_MIN_ITERS, no
  update kept, multipliers back at 1, the verbose line and no bug-report
  message. Skipped where the solver measures with its own K (MKL Pardiso,
  large cuDSS), as check 4 is.
- Docs: the refinement page (if the solve for g fails at the start of a
  solve / after a scale update), the scale page, info's scale_updates.

Runs where the check never fails are bit-identical to master: suite logs on
the Mac (Make: default, SFLOAT, USE_LAPACK=0, DEFLATE_VECTORS=0, DLONG;
QDLDL, CG and dense; Accelerate at VECLIB_MAXIMUM_THREADS=1, since its
threaded factorization varies from process to process on master too)
differ only in the new test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant