Skip to content

Stop WolfLSZoom when the bracket has no interior double - #3439

Open
avehtari wants to merge 2 commits into
developfrom
bugfix/3229-wolflszoom-endless-loop
Open

avehtari wants to merge 2 commits into
developfrom
bugfix/3229-wolflszoom-endless-loop

Conversation

@avehtari

Copy link
Copy Markdown
Member

Fixes #3229.

This PR was Claude assisted

Submission Checklist

  • Run unit tests: ./runTests.py src/test/unit (only src/test/unit/optimization was run: 57 tests pass)
  • Run cpplint: make cpplint (status 0, no warnings; the new tests in src/test/unit/optimization/bfgs_linesearch_test.cpp also pass cpplint with the same options)
  • Declare copyright holder and open-source license: see below

Summary

stan::optimization::WolfLSZoom() can loop forever. Its stop test is the absolute |alo - ahi| < min_range with min_range = 1e-16, but for |alpha| >= 0.5 adjacent doubles are farther apart than 1e-16. When the bracket has shrunk to two adjacent doubles, every trial step equals alo or ahi. A trial at ahi evaluates the same point again and leaves the bracket as it was, so the loop never ends. The inner loop, which halves alpha after failed evaluations, can stall in the same way when the halving rounds back to the same alpha. This affects optimize (BFGS and L-BFGS) and Pathfinder.

This PR adds two stop conditions, each of which returns 1 (failure), the code that the existing stop tests return:

  • outer loop: std::nextafter(alo, ahi) == ahi, i.e. no double lies strictly between the bracket ends;
  • inner loop: the halving does not change alpha.

Intended Effect

Line searches that would never end now return failure. The callers already handle a failed line search (BFGSMinimizer::step() resets the Hessian approximation once and then returns TERM_LSFAIL).

A line search that already fails at such a bracket now fails one function evaluation earlier (case (a) below). All other line searches give the same result, bit for bit. Reason: when no double lies strictly between alo and ahi, the current code can only (a) evaluate at alo, set ahi = alo and return 1 in the next iteration, or (b) evaluate at ahi and repeat forever. By the zoom invariant, a point at ahi always takes the ahi = alpha branch again (f(ahi) >= f(alo), or ahi fails the sufficient-decrease condition), so no trial in such a bracket can succeed. In the inner loop, an unchanged alpha means the same failing point would be evaluated forever.

How to Verify

  • New unit tests in src/test/unit/optimization/bfgs_linesearch_test.cpp: a 1-D function with slope -1 below x = 10 and value and slope 1e300 from x = 10 on (with an optional interval of failed evaluations). The test function throws after 100,000 evaluations, so an endless loop makes the tests fail instead of hang.
    • wolfeLineSearch_jump_terminates: WolfeLineSearch() from alpha = 1; the zoom bisects towards 10 until the bracket is (10 - 1 ulp, 10). Before: endless. After: returns 1 after 55 evaluations.
    • WolfLSZoom_adjacent_bracket: WolfLSZoom() with alo = 10 - 1 ulp, ahi = 10. Before: endless (every bisection rounds to 10). After: returns 1 with no evaluation.
    • WolfLSZoom_failing_evaluations: evaluations fail for 10 <= x < 12, bracket (10 - 1 ulp, 13). The inner loop halves towards 10 - 1 ulp and stalls at 10. Before: endless. After: returns 1 after 50 evaluations.
  • With the patch, ./runTests.py src/test/unit/optimization passes (57 tests, 6 test programs). With the original header, the 3 new tests fail and the 4 old tests in that file pass.
  • Random comparison (outside the test suite): 200,000 random WolfeLineSearch() calls (dimension 1–5; smooth functions, quadratic plus an optional quartic term; the same with a jump of height 1e2 to 1e300 along a random direction; the same with a region of failed evaluations; initial alpha 0.01 to 100). Current code: 38 endless loops. Patched: none. All other 199,962 results are identical (return code; for successes also alpha, f, x and the number of evaluations).
  • End-to-end, with a Pathfinder example (linked in Infinite loop in WolfLSZoom when using stan for optimization #3229), in CmdStan 2.39.0 and 2.40.0 with only bfgs_linesearch.hpp replaced:
    • 2.39.0: the example hangs in path 5. With the patch it finishes in 29 s; path 5 reports "LS failed, Hessian reset" at iteration 33, continues and ends. For another seed the output draws are identical to the unpatched build.
    • 2.40.0: the example does not hang. Over seeds 1–1000 (instrumented build), one seed (895) reaches a bracket with no interior double, in both 2.39.0 and 2.40.0, and fails there in both; none hangs. Path 5 of seed 88510002 also reaches such a bracket in 2.40.0 (zoom iteration 53), but the trial step lands on alo, so the unpatched line search fails one iteration later. Patched and unpatched give identical draws for both seeds; the logs differ only in the reported evaluation counts.
  • Versions: unit tests and make cpplint on develop 5dbdb88 (2.40.0) with the patch applied; end-to-end with the CmdStan 2.39.0 and 2.40.0 releases.

Side Effects

None

Documentation

None needed.

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

Aalto University

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.23 0.23 1.0 0.01% faster
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.06 0.06 1.01 0.81% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 6.31 6.46 0.98 -2.36% slower
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.01 0.64% faster
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 8.44 8.41 1.0 0.3% faster
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 4.47 4.52 0.99 -0.99% slower
stat_comp_benchmarks/benchmarks/sir/sir.stan 169.08 169.67 1.0 -0.34% slower
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.89 0.89 1.0 -0.35% slower
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.62 0.62 1.0 -0.31% slower
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 42.84 42.71 1.0 0.3% faster
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.59 0.59 1.0 -0.24% slower
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 21.03 20.98 1.0 0.25% faster
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.11 0.11 0.99 -0.7% slower
stat_comp_benchmarks/benchmarks/arK/arK.stan 3.18 3.19 1.0 -0.21% slower
performance.compilation 394.22 388.31 1.02 1.5% faster
Mean result: 0.9989327613400517

Jenkins Console Log
Jenkins Build Stages
Commit hash: 44a50912ff3f2e71023ea29a6f91adf7c6b1cf87

Machine information
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

CPU:

Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Byte Order:                              Little Endian
Address sizes:                           43 bits physical, 48 bits virtual
CPU(s):                                  256
On-line CPU(s) list:                     0-255
Thread(s) per core:                      2
Core(s) per socket:                      64
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               AuthenticAMD
CPU family:                              23
Model:                                   49
Model name:                              AMD EPYC 7742 64-Core Processor
Stepping:                                0
Frequency boost:                         enabled
CPU MHz:                                 1488.069
CPU max MHz:                             3416.0681
CPU min MHz:                             1500.0000
BogoMIPS:                                4491.85
Virtualization:                          AMD-V
L1d cache:                               4 MiB
L1i cache:                               4 MiB
L2 cache:                                64 MiB
L3 cache:                                512 MiB
NUMA node0 CPU(s):                       0-63,128-191
NUMA node1 CPU(s):                       64-127,192-255
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Old microcode:             Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Mitigation; untrained return thunk; SMT enabled with STIBP protection
Vulnerability Spec rstack overflow:      Mitigation; Safe RET
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Retpolines; IBPB conditional; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sev sev_es

G++:

g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:

clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

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.

Infinite loop in WolfLSZoom when using stan for optimization

2 participants