Repository navigation
Keep HagerZhang's bracket from narrowing onto its own endpoint - #212
Merged
Merged
Conversation
`update!` classifies the trial point with `c < a || c > b`, so a `c` sitting exactly on an endpoint counts as interior. Both escapes below that guard then hand the endpoint back as the bracket's *other* end -- `c == a` with `dphi_c >= 0` returns `(ia, ic)`, and `c == b` with `phi_c <= phi_lim` returns `(ic, ib)` -- so the caller reads back an interval of zero width and its `@assert B > A` fires. A non-finite `c` falls through the same guard, since `NaN < a` and `NaN > b` are both false, and reaches the `bisect!(ia, ic)` tail whose own `@assert b > a` then fails. Which escape fires is decided by a fresh evaluation at `c`, so getting there needs the stored slope or value at that endpoint to disagree with what `ϕdϕ` returns. For `ia == 1` that is ordinary: `secant2!` keeps `slopes[1] = dphi_0`, and Optim computes that as `dot(state.g_x, state.s)` while the `ϕdϕ` it passes takes its slopes from `NLSolversBase.value_jvp!`. On an objective supplying its own `fjvp` those are two implementations of one derivative, and near a stationary point they diverge in relative terms -- measured at up to 20% on a 16-parameter marginal likelihood, where the sign can differ. `c` then only has to round onto the endpoint, which with `a == 0` it does once `|dphi_a|` is negligible against `dphi_b`. Require `c` to be strictly interior. That covers the endpoints and the non-finite case together, and makes `bisect!`'s `b > a` hold by construction. Keeping the bracket is the conservative branch: the caller's `B - A < gamma * (b - a)` test then fails and it bisects, so the line search still makes progress. Not a regression -- the guard reads the same in 7.7.1 and earlier; only the trajectory changed. Fixes #211. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Member
I see |
pkofod
approved these changes
Sep 2, 2026
pkofod
added a commit
that referenced
this pull request
Sep 14, 2026
…e the bracket (#213) * HagerZhang: do not evaluate a secant point that is not strictly inside the bracket `secant2!` evaluated `ϕdϕ(c)` at the secant root before checking where it landed. The root is not finite when the two slopes coincide, which was already handled by bisecting, and it rounds onto an endpoint when the bracket is a few ulps wide or one slope is negligible against the other. The value and slope at an endpoint are already stored, so that evaluation is a wasted objective call, and `update!` (since #212) discards its result anyway. It is also the evaluation that exposed #211: a fresh slope at alpha = 0 that disagreed in sign with the stored `dphi_0`. Treat a non-interior secant root like a non-finite one and bisect instead, and require the second secant point to be strictly interior as well. * Apply suggestion from @pkofod --------- Co-authored-by: Patrick Kofod Mogensen <patrick.mogensen@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #211.
The defect
update!classifies the trial point withc < a || c > b, so acsitting exactly on an endpoint counts as interior. Both escapes below that guard then hand the endpoint back as the bracket's other end:c == awithdphi_c >= 0→return ia, ic, andalphas[ic] == alphas[ia]c == bwithphi_c <= phi_lim→return ic, ib, andalphas[ic] == alphas[ib]The caller reads those indices back and asserts
B > A, which is how this surfaced:A non-finite
cfalls through the same guard, sinceNaN < aandNaN > bare both false. It reaches thebisect!(ia, ic)tail, whose own@assert b > athen fails instead.The entry assertions
slopes[ia] < 0,values[ia] <= phi_limandb > aall hold going in, so the bracket handed toupdate!is well-formed and it isupdate!that degenerates it.How it is reached
Which escape fires is decided by a fresh evaluation at
c, so getting there needs the stored slope or value at that endpoint to disagree with whatϕdϕreturns there. Foria == 1that is ordinary rather than pathological:secant2!keepsslopes[1] = dphi_0, andOptim.perform_linesearch!computes that asdot(state.g_x, state.s)while theϕdϕit passes takes its slopes fromNLSolversBase.value_jvp!. On an objective that supplies its ownfjvpthose are two independent implementations of one derivative.Measured over all 47 line searches of a 16-parameter marginal-likelihood fit under
BFGS(linesearch = HagerZhang(rho = 2.0), initial_stepnorm = 1.0), usingNLSolversBase.gradient!!andjvp!!so that none of it is a cache hit: twojvp!!calls at the samexagree 47/47, twogradient!!calls agree 47/47, anddphi_0matches a forced freshdot(gradient!!(obj, x), s)47/47 — while that gradient contraction andjvp!!(obj, x, s)differ 47/47, by up to4.7e-3absolute and 20.4 % relative. So both routes are individually deterministic and nothing is stale; the gap is only the two implementations. It grows as the slope shrinks, becausedot(g, s)cancels across the components where a pushforward does not, so it is worst exactly where the line search is most delicate, and far enough into convergence the two can differ in sign. Raised separately as JuliaNLSolvers/Optim.jl#1284; this PR is the robustness half, which is worth having either way since a line search should not assert on it.cthen only has to round onto the endpoint. Witha == 0,secant(a, b, dphi_a, dphi_b) = -b * dphi_a / (dphi_b - dphi_a), which rounds to0once|dphi_a|is negligible againstdphi_b.The change
Require
cto be strictly interior. One condition covers the endpoints and the non-finite case, and it makesbisect!'sb > ahold by construction:Keeping the bracket is the conservative branch, not a stall: the caller's
B - A < gamma * (b - a)test then fails and it bisects, so the line search still makes progress.This is not a regression — the guard reads the same in 7.7.1 and earlier. What changed for the workload that found it was only the trajectory: LineSearches 7.7.1 + Optim 2.2.2 was green on the same code, LineSearches 7.8.0 + Optim 2.3.0 walks a few iterations further into convergence and lands on it.
Tests
Three cases in
test/errors.jl, next to the existingcstep outside its bracketinvariant test, callingupdate!directly on a well-formed bracket[0, 1]. On master they return(0.0, 0.0),(1.0, 1.0)and(NaN, 1.0); with the change all three return(0.0, 1.0).ϕdϕis anerror(...)stub, since none of these branches may reach it.Full test suite green.
Not in scope
secant2!'s second secant is guarded bya <= c <= b, so it evaluatesϕdϕat an endpoint and then discards the result onceupdate!declines to narrow. Tightening that toa < c < bwould save the evaluation, but it changes iteration counts, so it seemed better kept separate from a correctness fix.This pull request was written with the assistance of generative AI.