From 3afb3bb1ef6248300a2289c710181a4bbd97d4eb Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 12:14:25 -0400 Subject: [PATCH 01/10] Make RobustNonMonotoneLineSearch traceable under Reactant Under ReactantCore.within_compile(), solve! runs a bounded @trace while backtracking loop and selects the step and return code with ifelse; the non-monotone history is a fixed-size traced vector updated with a mask, and nsteps is traced so a traced solver loop can carry the cache. The host path is unchanged. LineSearchSolution.retcode becomes a type parameter so it can hold a traced return code. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- Project.toml | 2 + src/LineSearch.jl | 5 ++- src/robust_non_monotone.jl | 75 ++++++++++++++++++++++++++++++++++++-- src/utils.jl | 3 ++ 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 9b6d60d..31e08a7 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ FastClosures = "9aa1b823-49e4-5ca5-8b0f-3971ec8bab6a" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MaybeInplace = "bb5d69b7-63fc-4a16-80bd-7e42200c7bdb" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +ReactantCore = "a3311ec8-5e00-46d5-b541-4f83e724a433" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" SciMLJacobianOperators = "19f34311-ddf3-4b8b-af20-060888a46c0e" StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" @@ -34,6 +35,7 @@ LineSearches = "7.3.0" LinearAlgebra = "1.10" MaybeInplace = "0.1.4" PrecompileTools = "1.2.1" +ReactantCore = "0.1.21" ReverseDiff = "1.15.3" SafeTestsets = "0.1" SciMLBase = "3.0" diff --git a/src/LineSearch.jl b/src/LineSearch.jl index cd3e506..31d1b82 100644 --- a/src/LineSearch.jl +++ b/src/LineSearch.jl @@ -6,6 +6,7 @@ using ConcreteStructs: @concrete using FastClosures: @closure using LinearAlgebra: norm, dot using MaybeInplace: @bb +using ReactantCore: ReactantCore using SciMLBase: SciMLBase, AbstractNonlinearProblem, OptimizationProblem, ReturnCode, NonlinearFunction using SciMLJacobianOperators: VecJacOperator, JacVecOperator @@ -57,7 +58,7 @@ The result returned by a line-search solve. - `step_size`: accepted step length for the current search direction. - `retcode`: a `SciMLBase.ReturnCode` describing whether the line search found - an acceptable step. + an acceptable step. Inside a Reactant compilation it is a traced return code. - `ϕ`: merit value at `step_size`, or `nothing` if the algorithm did not report it. Returning it lets the caller reuse the accepted point instead of re-evaluating the objective, which for an AD-defined problem is a full @@ -77,7 +78,7 @@ sol.retcode """ @concrete struct LineSearchSolution step_size - retcode::ReturnCode.T + retcode ϕ dϕ end diff --git a/src/robust_non_monotone.jl b/src/robust_non_monotone.jl index f631147..1ef59bc 100644 --- a/src/robust_non_monotone.jl +++ b/src/robust_non_monotone.jl @@ -32,6 +32,11 @@ Robust NonMonotone Line Search is a derivative free line search method from DF S - `maxiters`: the maximum number of iterations allowed for the inner loop of the algorithm. Defaults to `100`. +The line search can be traced by `Reactant.@jit`/`@compile`, including inside a traced +solver loop that carries its cache. The compiled search accepts the same step as the host +one, but it evaluates both trial points of each iteration and does not count function +evaluations in `stats`. + # Examples ```julia @@ -65,7 +70,7 @@ end M::Int τ_min τ_max - nsteps::Int + nsteps η_strategy n_exp::Int stats <: Union{SciMLBase.NLStats, Nothing} @@ -79,6 +84,11 @@ function CommonSolve.init( @bb u_cache = similar(u) @bb fu_cache = similar(fu) T = promote_type(eltype(fu), eltype(u)) + if ReactantCore.within_compile() + # `@bb similar` returns a traced array itself, and a traced solver loop carrying + # this cache rejects that aliasing. + u_cache, fu_cache = u .+ zero(eltype(u)), fu .+ zero(eltype(fu)) + end ϕ = @closure ( f, p, u, du, α, u_cache, @@ -95,13 +105,14 @@ function CommonSolve.init( return RobustNonMonotoneLineSearchCache( prob.f, prob.p, ϕ, u_cache, fu_cache, T(1), alg.maxiters, fill(fn₁, alg.M), - T(alg.gamma), T(alg.sigma_1), alg.M, T(alg.tau_min), T(alg.tau_max), 0, η_strategy, - alg.n_exp, stats, alg + T(alg.gamma), T(alg.sigma_1), alg.M, T(alg.tau_min), T(alg.tau_max), + maybe_traced(0), η_strategy, alg.n_exp, stats, alg ) end function CommonSolve.solve!(cache::RobustNonMonotoneLineSearchCache, u, du) T = promote_type(eltype(du), eltype(u)) + ReactantCore.within_compile() && return solve_traced(cache, u, du, T) ϕ = @closure α -> cache.ϕ(cache.f, cache.p, u, du, α, cache.u_cache, cache.fu_cache) f_norm_old = ϕ(zero(T)) @@ -138,6 +149,15 @@ function CommonSolve.solve!(cache::RobustNonMonotoneLineSearchCache, u, du) end function callback_into_cache!(cache::RobustNonMonotoneLineSearchCache, fu) + if ReactantCore.within_compile() + # `mod(n - 1, M) + 1 == mod1(n, M)` without branching on the traced `n` + slot = mod(cache.nsteps - 1, cache.M) + 1 + cache.history = ifelse.( + (1:(cache.M)) .== slot, norm(fu)^cache.n_exp, cache.history + ) + cache.nsteps += 1 + return + end cache.history[mod1(cache.nsteps, cache.M)] = norm(fu)^cache.n_exp cache.nsteps += 1 return @@ -152,7 +172,54 @@ function SciMLBase.reinit!( cache.M = oftype(cache.M, cache.alg.M) cache.τ_min = oftype(cache.τ_min, cache.alg.tau_min) cache.τ_max = oftype(cache.τ_max, cache.alg.tau_max) - cache.nsteps = 0 + cache.nsteps = maybe_traced(0) # NOTE: Don't zero out the stats here, since we don't own it return cache end + +# Both trial points are evaluated in every iteration; the step the host loop would return +# is then selected with `ifelse`. `stats` is not updated: the loop body is traced once. +function solve_traced(cache::RobustNonMonotoneLineSearchCache, u, du, T) + (; f, p, n_exp, γ, τ_min, τ_max, maxiters) = cache + fu_buf = cache.fu_cache + f_norm_old = traced_merit(f, p, u, du, zero(T), fu_buf, n_exp) + η = cache.η_strategy(cache.nsteps, u, f_norm_old) + f_bar = maximum(cache.history) + + σ₁ = T(cache.σ₁) + # `σ₁` is the traced scalar stored in the cache, and `@trace` writes its results back + # into the objects it carries, so the carried step lengths must be fresh values. + α₊ = σ₁ + zero(σ₁) + α₋ = σ₁ + zero(σ₁) + step = -α₋ + accepted = ReactantCore.promote_to_traced(false) + iter = ReactantCore.promote_to_traced(0) + + ReactantCore.@trace track_numbers = false while (iter < maxiters) & !accepted + f₊ = traced_merit(f, p, u, du, α₊, fu_buf, n_exp) + accept₊ = f₊ ≤ f_bar + η - γ * α₊ * f_norm_old + f₋ = traced_merit(f, p, u, du, -α₋, fu_buf, n_exp) + accept₋ = f₋ ≤ f_bar + η - γ * α₋ * f_norm_old + + step = ifelse(accept₊, α₊, -α₋) + accepted = accept₊ | accept₋ + + α₊ = α₊ * clamp( + α₊ * f_norm_old / (f₊ + (2 * α₊ - 1) * f_norm_old), τ_min, τ_max + ) + α₋ = α₋ * clamp( + α₋ * f_norm_old / (f₋ + (2 * α₋ - 1) * f_norm_old), τ_min, τ_max + ) + iter = iter + 1 + end + + return LineSearchSolution( + ifelse(accepted, step, σ₁), + ifelse(accepted, ReturnCode.Success, ReturnCode.Failure) + ) +end + +function traced_merit(f, p, u, du, α, fu_buf, n_exp) + fu = evaluate_f!!(f, similar(fu_buf), u .+ α .* du, p) + return norm(fu)^n_exp +end diff --git a/src/utils.jl b/src/utils.jl index 7063836..554405e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -80,3 +80,6 @@ function residual_jv_cache(jvp_op, vjp_op, fu, u) @bb jv = similar(u) return jv end + +# Loop-carried scalars must already be traced when a Reactant `@trace while` loop starts. +maybe_traced(x) = ReactantCore.within_compile() ? ReactantCore.promote_to_traced(x) : x From ea64a18f58bc17bcd8941bfe54da84bf66b88238 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 12:14:25 -0400 Subject: [PATCH 02/10] Add Reactant test group for RobustNonMonotoneLineSearch Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- test/reactant/Project.toml | 20 ++++++ test/reactant/reactant_tests.jl | 111 ++++++++++++++++++++++++++++++++ test/test_groups.toml | 3 + 3 files changed, 134 insertions(+) create mode 100644 test/reactant/Project.toml create mode 100644 test/reactant/reactant_tests.jl diff --git a/test/reactant/Project.toml b/test/reactant/Project.toml new file mode 100644 index 0000000..4aa8a7e --- /dev/null +++ b/test/reactant/Project.toml @@ -0,0 +1,20 @@ +[deps] +CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" +LineSearch = "87fe0de2-c867-4266-b59a-2f0a94fc965b" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Reactant = "3c362404-f566-11ee-1572-e11a4b42c853" +ReactantCore = "a3311ec8-5e00-46d5-b541-4f83e724a433" +SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[sources] +LineSearch = {path = "../.."} + +[compat] +CommonSolve = "0.2.4" +LinearAlgebra = "1.10" +Reactant = "0.2.289" +ReactantCore = "0.1.21" +SciMLBase = "3.0" +Test = "1.10" +julia = "1.10" diff --git a/test/reactant/reactant_tests.jl b/test/reactant/reactant_tests.jl new file mode 100644 index 0000000..a66d345 --- /dev/null +++ b/test/reactant/reactant_tests.jl @@ -0,0 +1,111 @@ +using CommonSolve: init, solve! +using LineSearch, LinearAlgebra, Reactant, ReactantCore, SciMLBase, Test + +cube(u, p) = u .^ 3 .- 2 +cube!(fu, u, p) = (fu .= u .^ 3 .- 2; nothing) +trig(u, p) = sin.(u) .+ u ./ 2 .- p + +function residual(f, u, p) + SciMLBase.isinplace(NonlinearProblem(f, u, p)) || return f(u, p) + fu = similar(u) + f(fu, u, p) + return fu +end + +traced_if_compiling(x) = ReactantCore.within_compile() ? ReactantCore.promote_to_traced(x) : x + +# One line search after a recorded history entry (`nsteps = 1`, so `η` is finite and the +# unit step can be rejected). +function line_search(f, u, du, p, alg = RobustNonMonotoneLineSearch()) + prob = NonlinearProblem(f, u, p) + fu = residual(f, u, p) + cache = init(prob, alg, fu, u) + LineSearch.callback_into_cache!(cache, fu) + sol = solve!(cache, u, du) + return sol.step_size, sol.retcode == ReturnCode.Success +end + +spectral_coefficient(Δu, Δf) = clamp_spectral(dot(Δu, Δu) / dot(Δu, Δf), Δf) +function clamp_spectral(σ, fu) + in_bounds = (1.0e-10 ≤ abs(σ)) & (abs(σ) ≤ 1.0e10) + return ifelse(in_bounds, σ, clamp(inv(norm(fu)), 1.0, 1.0e5)) +end + +# DF-SANE (La Cruz, Martínez, Raydan 2006) outer loop, as in NonlinearSolve's `DFSane`. +# `unit_step = true` takes the spectral step with `α = 1` and no line search. +function dfsane(f, u0, p, maxiters; unit_step = false, abstol = 1.0e-10) + prob = NonlinearProblem(f, u0, p) + u = u0 + fu = f(u, p) + ls = init(prob, RobustNonMonotoneLineSearch(), fu, u) + σ = clamp_spectral(dot(u, u) / dot(u, fu), fu) + nsteps = traced_if_compiling(0) + done = traced_if_compiling(false) + ReactantCore.@trace track_numbers = false while (nsteps < maxiters) & !done + du = -σ .* fu + sol = solve!(ls, u, du) + α = ifelse(unit_step, one(σ), sol.step_size) + u_new = u .+ α .* du + fu_new = f(u_new, p) + σ = spectral_coefficient(u_new .- u, fu_new .- fu) + u = u_new + fu = fu_new + LineSearch.callback_into_cache!(ls, fu) + done = maximum(abs, fu) ≤ abstol + nsteps = nsteps + 1 + end + return u, nsteps, done +end + +# Expected steps and return codes are those of the host loop in LineSearch v0.1.19. +@testset "compiled line search matches the host" begin + u_cube = [3.0, -2.0] + u_trig = collect(range(-2.0, 2.0; length = 5)) + default = RobustNonMonotoneLineSearch() + cases = [ + "unit step rejected" => (cube, u_cube, nothing, default, 0.1, true), + "unit step rejected, in place" => (cube!, u_cube, nothing, default, 0.1, true), + "unit step accepted" => (trig, u_trig, 0.3, default, 1.0, true), + "maxiters reached" => ( + cube, u_cube, nothing, RobustNonMonotoneLineSearch(; maxiters = 1), 1.0, false, + ), + "maxiters reached, sigma_1 = 2" => ( + cube, u_cube, nothing, RobustNonMonotoneLineSearch(; maxiters = 1, sigma_1 = 2), + 2.0, false, + ), + ] + @testset "$name" for (name, (f, u, p, alg, α_expected, ok_expected)) in cases + du = -residual(f, u, p) + α_host, ok_host = line_search(f, u, du, p, alg) + @test α_host == α_expected + @test ok_host == ok_expected + α_jit, ok_jit = @jit line_search( + f, Reactant.to_rarray(u), Reactant.to_rarray(du), p, alg + ) + @test Float64(α_jit) ≈ α_expected + @test Bool(ok_jit) == ok_expected + end +end + +@testset "compiled DF-SANE on u.^3 .- 2" begin + u0 = [3.0, -2.0] + maxiters = 50 + u_host, steps_host, done_host = dfsane(cube, u0, nothing, maxiters) + @test done_host + # `solve(NonlinearProblem(cube, u0), DFSane()).stats.nsteps` with NonlinearSolve 4.32 + @test steps_host == 12 + @test u_host ≈ fill(cbrt(2.0), 2) + + u_jit, steps_jit, done_jit = @jit dfsane(cube, Reactant.to_rarray(u0), nothing, maxiters) + @test Bool(done_jit) + @test Int(steps_jit) == steps_host + @test Array(u_jit) ≈ u_host + + # the unit spectral step has not converged within the same number of steps + _, _, done_unit = dfsane(cube, u0, nothing, steps_host; unit_step = true) + @test !done_unit + _, _, done_unit_jit = @jit dfsane( + cube, Reactant.to_rarray(u0), nothing, steps_host; unit_step = true + ) + @test !Bool(done_unit_jit) +end diff --git a/test/test_groups.toml b/test/test_groups.toml index e0bd0fd..054aba0 100644 --- a/test/test_groups.toml +++ b/test/test_groups.toml @@ -10,3 +10,6 @@ versions = ["1", "lts"] [QA] versions = ["lts", "1"] in_all = false + +[Reactant] +versions = ["1"] From f7d08f3bbb74be10bbacd3110c603364b790d751 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 19:30:50 -0400 Subject: [PATCH 03/10] Use a scaled norm on the compiled RobustNonMonotone path Reactant lowers norm to sqrt(dot(x, x)), which underflows to 0 or overflows to Inf for finite residuals and changed accepted steps and return codes. The compiled path scales by maximum(abs, x) for the initial merit, the history and the trial merits; the host path still calls norm. Adds compiled regression tests for small Float64/Float32 and large finite merits. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- src/LineSearch.jl | 5 ++--- src/robust_non_monotone.jl | 40 +++++++++++++++++++++++++++------ test/reactant/reactant_tests.jl | 31 ++++++++++++++++++++++--- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/LineSearch.jl b/src/LineSearch.jl index 31d1b82..cd3e506 100644 --- a/src/LineSearch.jl +++ b/src/LineSearch.jl @@ -6,7 +6,6 @@ using ConcreteStructs: @concrete using FastClosures: @closure using LinearAlgebra: norm, dot using MaybeInplace: @bb -using ReactantCore: ReactantCore using SciMLBase: SciMLBase, AbstractNonlinearProblem, OptimizationProblem, ReturnCode, NonlinearFunction using SciMLJacobianOperators: VecJacOperator, JacVecOperator @@ -58,7 +57,7 @@ The result returned by a line-search solve. - `step_size`: accepted step length for the current search direction. - `retcode`: a `SciMLBase.ReturnCode` describing whether the line search found - an acceptable step. Inside a Reactant compilation it is a traced return code. + an acceptable step. - `ϕ`: merit value at `step_size`, or `nothing` if the algorithm did not report it. Returning it lets the caller reuse the accepted point instead of re-evaluating the objective, which for an AD-defined problem is a full @@ -78,7 +77,7 @@ sol.retcode """ @concrete struct LineSearchSolution step_size - retcode + retcode::ReturnCode.T ϕ dϕ end diff --git a/src/robust_non_monotone.jl b/src/robust_non_monotone.jl index 1ef59bc..36a5157 100644 --- a/src/robust_non_monotone.jl +++ b/src/robust_non_monotone.jl @@ -33,9 +33,12 @@ Robust NonMonotone Line Search is a derivative free line search method from DF S algorithm. Defaults to `100`. The line search can be traced by `Reactant.@jit`/`@compile`, including inside a traced -solver loop that carries its cache. The compiled search accepts the same step as the host -one, but it evaluates both trial points of each iteration and does not count function -evaluations in `stats`. +solver loop that carries its cache. The compiled search follows the same acceptance rule +and step-length updates as the host one, but it evaluates both trial points of each +iteration, does not count function evaluations in `stats`, and returns a +`LineSearch.TracedLineSearchSolution` with traced `step_size` and `retcode`. Its +floating-point results can differ from the host's in the last bits, which can change the +accepted step when a trial merit lies at the acceptance threshold. # Examples @@ -100,7 +103,7 @@ function CommonSolve.init( return @fastmath norm(fu_cache)^alg.n_exp end - fn₁ = norm(fu)^alg.n_exp + fn₁ = merit_norm(fu)^alg.n_exp η_strategy = @closure (n, xₙ, fₙ) -> alg.η_strategy(fn₁, n, xₙ, fₙ) return RobustNonMonotoneLineSearchCache( @@ -153,7 +156,7 @@ function callback_into_cache!(cache::RobustNonMonotoneLineSearchCache, fu) # `mod(n - 1, M) + 1 == mod1(n, M)` without branching on the traced `n` slot = mod(cache.nsteps - 1, cache.M) + 1 cache.history = ifelse.( - (1:(cache.M)) .== slot, norm(fu)^cache.n_exp, cache.history + (1:(cache.M)) .== slot, scaled_norm(fu)^cache.n_exp, cache.history ) cache.nsteps += 1 return @@ -213,7 +216,7 @@ function solve_traced(cache::RobustNonMonotoneLineSearchCache, u, du, T) iter = iter + 1 end - return LineSearchSolution( + return TracedLineSearchSolution( ifelse(accepted, step, σ₁), ifelse(accepted, ReturnCode.Success, ReturnCode.Failure) ) @@ -221,5 +224,28 @@ end function traced_merit(f, p, u, du, α, fu_buf, n_exp) fu = evaluate_f!!(f, similar(fu_buf), u .+ α .* du, p) - return norm(fu)^n_exp + return scaled_norm(fu)^n_exp +end + +# Reactant lowers `norm` to `sqrt(dot(x, x))`, which underflows and overflows for finite +# inputs, so the compiled path scales by the largest entry as `LinearAlgebra` does. +merit_norm(x) = ReactantCore.within_compile() ? scaled_norm(x) : norm(x) + +function scaled_norm(x) + s = maximum(abs, x) + unscalable = iszero(s) | !isfinite(s) + scaled = s * sqrt(sum(abs2, x ./ ifelse(unscalable, one(s), s))) + return ifelse(unscalable, s, scaled) +end + +""" + TracedLineSearchSolution(step_size, retcode) + +What `solve!` of [`RobustNonMonotoneLineSearch`](@ref) returns inside a Reactant +compilation: the fields of [`LineSearchSolution`](@ref) without `ϕ` and `dϕ`, with a +traced `step_size` and a traced `SciMLBase.ReturnCode`. +""" +struct TracedLineSearchSolution{S, R} + step_size::S + retcode::R end diff --git a/test/reactant/reactant_tests.jl b/test/reactant/reactant_tests.jl index a66d345..6a45970 100644 --- a/test/reactant/reactant_tests.jl +++ b/test/reactant/reactant_tests.jl @@ -4,6 +4,7 @@ using LineSearch, LinearAlgebra, Reactant, ReactantCore, SciMLBase, Test cube(u, p) = u .^ 3 .- 2 cube!(fu, u, p) = (fu .= u .^ 3 .- 2; nothing) trig(u, p) = sin.(u) .+ u ./ 2 .- p +linear(u, p) = u .- p function residual(f, u, p) SciMLBase.isinplace(NonlinearProblem(f, u, p)) || return f(u, p) @@ -22,7 +23,7 @@ function line_search(f, u, du, p, alg = RobustNonMonotoneLineSearch()) cache = init(prob, alg, fu, u) LineSearch.callback_into_cache!(cache, fu) sol = solve!(cache, u, du) - return sol.step_size, sol.retcode == ReturnCode.Success + return sol.step_size, sol.retcode == ReturnCode.Success, cache.history end spectral_coefficient(Δu, Δf) = clamp_spectral(dot(Δu, Δu) / dot(Δu, Δf), Δf) @@ -76,10 +77,10 @@ end ] @testset "$name" for (name, (f, u, p, alg, α_expected, ok_expected)) in cases du = -residual(f, u, p) - α_host, ok_host = line_search(f, u, du, p, alg) + α_host, ok_host, _ = line_search(f, u, du, p, alg) @test α_host == α_expected @test ok_host == ok_expected - α_jit, ok_jit = @jit line_search( + α_jit, ok_jit, _ = @jit line_search( f, Reactant.to_rarray(u), Reactant.to_rarray(du), p, alg ) @test Float64(α_jit) ≈ α_expected @@ -87,6 +88,30 @@ end end end +# Merits whose `sqrt(dot(x, x))` underflows to zero or overflows to `Inf`. Expected steps, +# return codes and history are those of the host loop in LineSearch v0.1.19. +@testset "compiled line search with finite extreme merits" begin + alg = RobustNonMonotoneLineSearch(; n_exp = 1, maxiters = 3) + cases = [ + "Float64, small" => ([1.0e-200, 1.0e-200], [1.0e-199, 1.0e-199], 0.0, -0.1), + "Float32, small" => (Float32[1.0f-30, 1.0f-30], Float32[1.0f-29, 1.0f-29], 0.0f0, -0.1f0), + "Float64, large" => ([1.0e200, 1.0e200], [-1.0e200, -1.0e200], 0.0, 1.0), + ] + @testset "$name" for (name, (u, du, p, α_expected)) in cases + history_expected = fill(sqrt(2one(eltype(u))) * u[1], 10) + α_host, ok_host, history_host = line_search(linear, u, du, p, alg) + @test α_host == α_expected + @test ok_host + @test history_host ≈ history_expected + α_jit, ok_jit, history_jit = @jit line_search( + linear, Reactant.to_rarray(u), Reactant.to_rarray(du), p, alg + ) + @test eltype(u)(α_jit) ≈ α_expected + @test Bool(ok_jit) + @test Array(history_jit) ≈ history_expected + end +end + @testset "compiled DF-SANE on u.^3 .- 2" begin u0 = [3.0, -2.0] maxiters = 50 From ba705b3064f7804a7146db3228a6b96dd5851585 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 19:30:50 -0400 Subject: [PATCH 04/10] Keep LineSearchSolution unchanged; return TracedLineSearchSolution when compiled Making LineSearchSolution.retcode a type parameter broke dispatch on LineSearchSolution{Float64, Nothing, Nothing}. The compiled solve! of RobustNonMonotoneLineSearch returns an internal TracedLineSearchSolution with the same step_size and retcode fields instead. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- src/LineSearch.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LineSearch.jl b/src/LineSearch.jl index cd3e506..73616b6 100644 --- a/src/LineSearch.jl +++ b/src/LineSearch.jl @@ -6,6 +6,7 @@ using ConcreteStructs: @concrete using FastClosures: @closure using LinearAlgebra: norm, dot using MaybeInplace: @bb +using ReactantCore: ReactantCore using SciMLBase: SciMLBase, AbstractNonlinearProblem, OptimizationProblem, ReturnCode, NonlinearFunction using SciMLJacobianOperators: VecJacOperator, JacVecOperator From 3cde820a5aeee59a74df5f7332b3efd556c7a4e4 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 21:30:37 -0400 Subject: [PATCH 05/10] Keep the Reactant test group out of "All" SciMLTesting runs the "All" groups in one process. In the Downgrade job (GROUP=All, Julia lts) the root test environment loads LineSearch with the downgraded ReactantCore 0.1.21 before test/reactant is activated, so the latest Reactant there fails to precompile (`@annotate` not defined). in_all = false is SciMLTesting's setting for environment-specific groups; CI still runs GROUP=Reactant as its own job on Julia 1. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- test/test_groups.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_groups.toml b/test/test_groups.toml index 054aba0..f9ce8d1 100644 --- a/test/test_groups.toml +++ b/test/test_groups.toml @@ -11,5 +11,8 @@ versions = ["1", "lts"] versions = ["lts", "1"] in_all = false +# Own environment with the latest Reactant; it must not share a process with the root +# test environment (which may hold an older ReactantCore), so it is not part of "All". [Reactant] versions = ["1"] +in_all = false From 4fea03194abd397bee16be37e3b7d78c24da2dff Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 21:30:37 -0400 Subject: [PATCH 06/10] Pin the LineSearchSolution{Float64, Nothing, Nothing} host type in a test Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- test/objective_merit_tests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/objective_merit_tests.jl b/test/objective_merit_tests.jl index 5f011a3..1761db8 100644 --- a/test/objective_merit_tests.jl +++ b/test/objective_merit_tests.jl @@ -120,6 +120,7 @@ import ForwardDiff @test sol.retcode == ReturnCode.Success @test sol.ϕ === nothing @test sol.dϕ === nothing + @test typeof(sol) === LineSearchSolution{Float64, Nothing, Nothing} end # ------------------------------------------------ gradient arity handling From 1eb89a7d4ea1adf7a5d8690113ad7a4146f1a42e Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Thu, 8 Oct 2026 08:48:41 -0400 Subject: [PATCH 07/10] Support scalar residuals in the compiled RobustNonMonotone line search traced_merit called similar on the residual buffer, which has no method for a traced scalar. Only in-place residuals need a fresh buffer. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- src/robust_non_monotone.jl | 4 +++- test/reactant/reactant_tests.jl | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/robust_non_monotone.jl b/src/robust_non_monotone.jl index 36a5157..56992f7 100644 --- a/src/robust_non_monotone.jl +++ b/src/robust_non_monotone.jl @@ -223,7 +223,9 @@ function solve_traced(cache::RobustNonMonotoneLineSearchCache, u, du, T) end function traced_merit(f, p, u, du, α, fu_buf, n_exp) - fu = evaluate_f!!(f, similar(fu_buf), u .+ α .* du, p) + # A fresh buffer per evaluation; out-of-place (and scalar) residuals need none. + buf = SciMLBase.isinplace(f) ? similar(fu_buf) : fu_buf + fu = evaluate_f!!(f, buf, u .+ α .* du, p) return scaled_norm(fu)^n_exp end diff --git a/test/reactant/reactant_tests.jl b/test/reactant/reactant_tests.jl index 6a45970..f0b06e5 100644 --- a/test/reactant/reactant_tests.jl +++ b/test/reactant/reactant_tests.jl @@ -112,6 +112,23 @@ end end end +# Expected values are those of the host loop in LineSearch v0.1.19. +@testset "compiled line search on a scalar residual: $name" for (name, alg, α_expected, ok_expected) in ( + ("unit step rejected", RobustNonMonotoneLineSearch(; n_exp = 1), 0.1, true), + ("maxiters reached", RobustNonMonotoneLineSearch(; n_exp = 1, maxiters = 1), 1.0, false), + ) + u, du, p = 3.0, -60.0, 0.0 + α_host, ok_host, history_host = line_search(linear, u, du, p, alg) + @test α_host == α_expected + @test ok_host == ok_expected + α_jit, ok_jit, history_jit = @jit line_search( + linear, Reactant.ConcreteRNumber(u), Reactant.ConcreteRNumber(du), p, alg + ) + @test Float64(α_jit) == α_expected + @test Bool(ok_jit) == ok_expected + @test Array(history_jit) == history_host +end + @testset "compiled DF-SANE on u.^3 .- 2" begin u0 = [3.0, -2.0] maxiters = 50 From fb7c7463cd769371aa5cea8448d88c46848a8636 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Thu, 8 Oct 2026 08:51:45 -0400 Subject: [PATCH 08/10] Test the compiled return code with SciMLBase.successful_retcode Covers successful and failed searches checked inside @jit and after return, and makes the DF-SANE test loop stop on a failed line search as NonlinearSolve's DFSane does. Needs a SciMLBase release with successful_retcode for traced return codes. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Agent-Harness: Claude Code 2.1.292 Agent-Model: opus Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu --- test/reactant/reactant_tests.jl | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/test/reactant/reactant_tests.jl b/test/reactant/reactant_tests.jl index f0b06e5..1243fd7 100644 --- a/test/reactant/reactant_tests.jl +++ b/test/reactant/reactant_tests.jl @@ -45,17 +45,18 @@ function dfsane(f, u0, p, maxiters; unit_step = false, abstol = 1.0e-10) ReactantCore.@trace track_numbers = false while (nsteps < maxiters) & !done du = -σ .* fu sol = solve!(ls, u, du) - α = ifelse(unit_step, one(σ), sol.step_size) + ls_failed = !unit_step & !SciMLBase.successful_retcode(sol.retcode) + α = ifelse(unit_step, one(σ), ifelse(ls_failed, zero(σ), sol.step_size)) u_new = u .+ α .* du fu_new = f(u_new, p) σ = spectral_coefficient(u_new .- u, fu_new .- fu) u = u_new fu = fu_new LineSearch.callback_into_cache!(ls, fu) - done = maximum(abs, fu) ≤ abstol + done = (maximum(abs, fu) ≤ abstol) | ls_failed nsteps = nsteps + 1 end - return u, nsteps, done + return u, nsteps, maximum(abs, fu) ≤ abstol end # Expected steps and return codes are those of the host loop in LineSearch v0.1.19. @@ -129,6 +130,31 @@ end @test Array(history_jit) == history_host end +# NonlinearSolve's DFSane checks the line search with `SciMLBase.successful_retcode`. +function checked_line_search(f, u, du, p, alg) + prob = NonlinearProblem(f, u, p) + fu = residual(f, u, p) + cache = init(prob, alg, fu, u) + LineSearch.callback_into_cache!(cache, fu) + sol = solve!(cache, u, du) + return sol, SciMLBase.successful_retcode(sol.retcode) +end + +@testset "successful_retcode of the compiled return code: $name" for (name, alg, ok_expected) in ( + ("success", RobustNonMonotoneLineSearch(), true), + ("failure", RobustNonMonotoneLineSearch(; maxiters = 1), false), + ) + u = [3.0, -2.0] + du = -cube(u, nothing) + sol_host, ok_host = checked_line_search(cube, u, du, nothing, alg) + @test ok_host == ok_expected + sol_jit, ok_jit = @jit checked_line_search( + cube, Reactant.to_rarray(u), Reactant.to_rarray(du), nothing, alg + ) + @test Bool(ok_jit) == ok_expected + @test SciMLBase.successful_retcode(sol_jit.retcode) == ok_expected +end + @testset "compiled DF-SANE on u.^3 .- 2" begin u0 = [3.0, -2.0] maxiters = 50 From cd04c7b282d0f7a62d9eb4431dcd5534e5f89d4e Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sat, 17 Oct 2026 12:42:15 -0400 Subject: [PATCH 09/10] Require SciMLBase 3.58 for compiled return-code checks The Reactant extension in SciMLBase 3.58 supports successful_retcode on traced return codes. Enforce that floor in the package and Reactant env. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.163.0 Agent-Model: gpt-6.1-sol Agent-Session: local job 112-codex (transcript kept on the agent host) --- Project.toml | 2 +- test/reactant/Project.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 31e08a7..85d08bf 100644 --- a/Project.toml +++ b/Project.toml @@ -38,7 +38,7 @@ PrecompileTools = "1.2.1" ReactantCore = "0.1.21" ReverseDiff = "1.15.3" SafeTestsets = "0.1" -SciMLBase = "3.0" +SciMLBase = "3.58" SciMLJacobianOperators = "0.1" SciMLTesting = "2.4" StaticArrays = "1" diff --git a/test/reactant/Project.toml b/test/reactant/Project.toml index 4aa8a7e..b13abd1 100644 --- a/test/reactant/Project.toml +++ b/test/reactant/Project.toml @@ -15,6 +15,6 @@ CommonSolve = "0.2.4" LinearAlgebra = "1.10" Reactant = "0.2.289" ReactantCore = "0.1.21" -SciMLBase = "3.0" +SciMLBase = "3.58" Test = "1.10" julia = "1.10" From 72ec7d33ed925fc73fffcafd20c931839c3a7be9 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sat, 17 Oct 2026 12:42:15 -0400 Subject: [PATCH 10/10] Preserve NaN interpolation ratios in compiled backtracking Select NaN ratios explicitly before XLA clamp so invalid trials propagate as on the host. The regression fails twice before the guard and passes with it; the complete pinned Reactant test file passes 66 assertions. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.163.0 Agent-Model: gpt-6.1-sol Agent-Session: local job 112-codex (transcript kept on the agent host) --- src/robust_non_monotone.jl | 11 +++++------ test/reactant/reactant_tests.jl | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/robust_non_monotone.jl b/src/robust_non_monotone.jl index 56992f7..6d998fd 100644 --- a/src/robust_non_monotone.jl +++ b/src/robust_non_monotone.jl @@ -207,12 +207,11 @@ function solve_traced(cache::RobustNonMonotoneLineSearchCache, u, du, T) step = ifelse(accept₊, α₊, -α₋) accepted = accept₊ | accept₋ - α₊ = α₊ * clamp( - α₊ * f_norm_old / (f₊ + (2 * α₊ - 1) * f_norm_old), τ_min, τ_max - ) - α₋ = α₋ * clamp( - α₋ * f_norm_old / (f₋ + (2 * α₋ - 1) * f_norm_old), τ_min, τ_max - ) + r₊ = α₊ * f_norm_old / (f₊ + (2 * α₊ - 1) * f_norm_old) + r₋ = α₋ * f_norm_old / (f₋ + (2 * α₋ - 1) * f_norm_old) + # XLA's clamp maps NaN to a bound; Julia's clamp propagates it. + α₊ = α₊ * ifelse(isnan(r₊), r₊, clamp(r₊, τ_min, τ_max)) + α₋ = α₋ * ifelse(isnan(r₋), r₋, clamp(r₋, τ_min, τ_max)) iter = iter + 1 end diff --git a/test/reactant/reactant_tests.jl b/test/reactant/reactant_tests.jl index 1243fd7..7b35ad7 100644 --- a/test/reactant/reactant_tests.jl +++ b/test/reactant/reactant_tests.jl @@ -89,6 +89,20 @@ end end end +@testset "compiled line search with NaN trial merits" begin + nan_residual(u, p) = ifelse.(u .< 0, NaN, u .^ 2 .- 1) + u, du = [0.5, 0.2], [-10.0, 3.0] + alg = RobustNonMonotoneLineSearch(; maxiters = 3) + α_host, ok_host, _ = line_search(nan_residual, u, du, nothing, alg) + @test α_host == 1.0 + @test !ok_host + α_jit, ok_jit, _ = @jit line_search( + nan_residual, Reactant.to_rarray(u), Reactant.to_rarray(du), nothing, alg + ) + @test Float64(α_jit) == 1.0 + @test !Bool(ok_jit) +end + # Merits whose `sqrt(dot(x, x))` underflows to zero or overflows to `Inf`. Expected steps, # return codes and history are those of the host loop in LineSearch v0.1.19. @testset "compiled line search with finite extreme merits" begin