-
-
Notifications
You must be signed in to change notification settings - Fork 67
Linesearch assumes f to be Enzyme compatible #837
Copy link
Copy link
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug 🐞
If you have a function f which is ForwardDiff compatible but not Enzyme compatible, the line search fails after Enzyme has been loaded (works before). It seems like internally its using Enzyme unconditionally, even though autodiff is specified as ForwardDiff specificially.
Ideally I would be able to fix the Enzyme problems by defining a trait like this:
enzyme_use_dupllicated_annotation(::ResidualFun) = trueMinimal Reproducible Example 👇
using Pkg
pkg"activate --temp"
pkg"add NonlinearSolve, PreallocationTools, ForwardDiff, Enzyme"
using NonlinearSolve, PreallocationTools, ForwardDiff
# first we construct a probme which is ForwardDiff comaptible but fails on Enzyme
struct ResidualFun
cache::DiffCache{Vector{Float64}, Vector{Float64}}
end
ResidualFun(n::Int) = ResidualFun(DiffCache(zeros(n)))
function (f::ResidualFun)(res, u, p)
tmp = get_tmp(f.cache, u)
@. tmp = u^2 - p
@. res = tmp - u .+ 1.0
return nothing
end
n = 4
fun = ResidualFun(n)
u0 = ones(n)
p = fill(2.0, n)
prob = NonlinearLeastSquaresProblem(NonlinearFunction(fun, resid_prototype=zeros(n)), u0, p)
alg1 = GaussNewton(; linesearch=BackTracking(), autodiff=AutoForwardDiff())
solve(prob, alg1)
alg2 = TrustRegion(; autodiff=AutoForwardDiff(), radius_update_scheme=RadiusUpdateSchemes.Bastin)
solve(prob, alg2)
using Enzyme
solve(prob, alg1) # Enzyme Error
solve(prob, alg2) # Enzyme Error
ERROR:
If you are using Enzyme by selecting the `AutoEnzyme` object from ADTypes, you may want to try setting the `function_annotation` option as follows:
AutoEnzyme(; function_annotation=Enzyme.Duplicated)
This hint appears because DifferentiationInterface and Enzyme are both loaded. It does not necessarily imply that Enzyme is being called through DifferentiationInterface.
EnzymeMutabilityException: Function argument passed to autodiff cannot be proven readonly.
If the the function argument cannot contain derivative data, instead call autodiff(Mode, Const(f), ...)
See https://enzyme.mit.edu/index.fcgi/julia/stable/faq/#Activity-of-temporary-storage for more information.
The potentially writing call is store ptr addrspace(10) %.fca.0.0.1.extract, ptr %6, align 8, !dbg !33, !tbaa !27, !alias.scope !35, !noalias !38, using %6 = getelementptr inbounds i8, ptr %4, i64 8, !dbg !33
Stacktrace:
[1] NonlinearFunction
@ ~/.julia/packages/SciMLBase/obTYa/src/scimlfunctions.jl:2656 [inlined]
[2] NonlinearFunction
@ ~/.julia/packages/SciMLBase/obTYa/src/scimlfunctions.jl:0 [inlined]
[3] diffejulia_NonlinearFunction_218064_inner_1wrap
@ ~/.julia/packages/SciMLBase/obTYa/src/scimlfunctions.jl:0
[4] macro expansion
@ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6672 [inlined]
[5] enzyme_call
@ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6151 [inlined]
[6] CombinedAdjointThunk
@ ~/.julia/packages/Enzyme/S3nC6/src/compiler.jl:6035 [inlined]
[7] autodiff
@ ~/.julia/packages/Enzyme/S3nC6/src/Enzyme.jl:528 [inlined]
[8] autodiff
@ ~/.julia/packages/Enzyme/S3nC6/src/Enzyme.jl:549 [inlined]
[9] value_and_pullback!(f!::NonlinearFunction{…}, y::Vector{…}, tx::Tuple{…}, prep::DifferentiationInterfaceEnzymeExt.EnzymeReverseTwoArgPullbackPrep{…}, backend::AutoEnzyme{…}, x::Vector{…}, ty::Tuple{…}, contexts::DifferentiationInterface.Constant{…})
@ DifferentiationInterfaceEnzymeExt ~/.julia/packages/DifferentiationInterface/M8gIf/ext/DifferentiationInterfaceEnzymeExt/reverse_twoarg.jl:142
[10] pullback!
@ ~/.julia/packages/DifferentiationInterface/M8gIf/src/first_order/pullback.jl:580 [inlined]
[11] #15
@ ~/.julia/packages/SciMLJacobianOperators/k05eI/src/SciMLJacobianOperators.jl:317 [inlined]
[12] JacobianOperator
@ ~/.julia/packages/SciMLJacobianOperators/k05eI/src/SciMLJacobianOperators.jl:161 [inlined]
[13] mul!
@ ~/.julia/packages/SciMLJacobianOperators/k05eI/src/SciMLJacobianOperators.jl:229 [inlined]
[14] __mul!
@ ~/.julia/packages/MaybeInplace/1tMVs/src/MaybeInplace.jl:307 [inlined]
[15] macro expansion
@ ~/.julia/packages/MaybeInplace/1tMVs/src/MaybeInplace.jl:171 [inlined]
[16] solve!(cache::NonlinearSolveFirstOrder.GenericTrustRegionSchemeCache{…}, J::Matrix{…}, fu::Vector{…}, u::Vector{…}, δu::Vector{…}, descent_stats::@NamedTuple{…})
@ NonlinearSolveFirstOrder ~/.julia/packages/NonlinearSolveFirstOrder/ih1YC/src/trust_region.jl:478
[17] step!(cache::NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithmCache{…}; recompute_jacobian::Nothing)
@ NonlinearSolveFirstOrder ~/.julia/packages/NonlinearSolveFirstOrder/ih1YC/src/solve.jl:344
[18] step!
@ ~/.julia/packages/NonlinearSolveFirstOrder/ih1YC/src/solve.jl:269 [inlined]
[19] #step!#179
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:609 [inlined]
[20] step!
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:603 [inlined]
[21] solve!(cache::NonlinearSolveFirstOrder.GeneralizedFirstOrderAlgorithmCache{…})
@ NonlinearSolveBase ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:304
[22] __solve(::NonlinearLeastSquaresProblem{…}, ::GeneralizedFirstOrderAlgorithm{…}; kwargs::@Kwargs{…})
@ NonlinearSolveBase ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:290
[23] __solve
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:286 [inlined]
[24] #solve_call#149
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:179 [inlined]
[25] solve_call
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:137 [inlined]
[26] solve_up(prob::NonlinearLeastSquaresProblem{…}, sensealg::Nothing, u0::Vector{…}, p::Vector{…}, args::GeneralizedFirstOrderAlgorithm{…}; originator::SciMLBase.ChainRulesOriginator, kwargs::@Kwargs{…})
@ NonlinearSolveBase ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:132
[27] solve_up
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:117 [inlined]
[28] #solve#147
@ ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:89 [inlined]
[29] solve(prob::NonlinearLeastSquaresProblem{…}, args::GeneralizedFirstOrderAlgorithm{…})
@ NonlinearSolveBase ~/.julia/packages/NonlinearSolveBase/1vIgs/src/solve.jl:47
[30] top-level scope
@ REPL[20]:1
[31] top-level scope
@ REPL:1
Some type information was truncated. Use `show(err)` to see complete types.
Not Working Environment (please complete the following information):
- Output of
using Pkg; Pkg.status()
(jl_oInbUj) pkg> st
Status `/tmp/jl_oInbUj/Project.toml`
[7da242da] Enzyme v0.13.129
[f6369f11] ForwardDiff v1.3.2
⌃ [8913a72c] NonlinearSolve v4.15.0
⌃ [d236fae5] PreallocationTools v0.4.34
Info Packages marked with ⌃ have new versions available and may be upgradable.- Output of
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Details
(jl_oInbUj) pkg> st -m
Status `/tmp/jl_oInbUj/Manifest.toml`
[47edcb42] ADTypes v1.21.0
[7d9f7c33] Accessors v0.1.43
[79e6a3ab] Adapt v4.4.0
[4fba245c] ArrayInterface v7.22.0
[70df07ce] BracketingNonlinearSolve v1.7.1
[fa961155] CEnum v0.5.0
[d360d2e6] ChainRulesCore v1.26.0
[38540f10] CommonSolve v0.2.6
[bbf7d656] CommonSubexpressions v0.3.1
[34da2185] Compat v4.18.1
[a33af91c] CompositionsBase v0.1.2
[2569d6c7] ConcreteStructs v0.2.3
[187b0558] ConstructionBase v1.6.0
[163ba53b] DiffResults v1.1.0
[b552c78f] DiffRules v1.15.1
⌃ [a0c0ee7d] DifferentiationInterface v0.7.15
[ffbed154] DocStringExtensions v0.9.5
[4e289a0a] EnumX v1.0.6
[7da242da] Enzyme v0.13.129
[f151be2c] EnzymeCore v0.8.18
[e2ba6199] ExprTools v0.1.10
[55351af7] ExproniconLite v0.10.14
[9aa1b823] FastClosures v0.3.2
[6a86dc24] FiniteDiff v2.29.0
[f6369f11] ForwardDiff v1.3.2
[069b7b12] FunctionWrappers v1.1.3
[77dc65aa] FunctionWrappersWrappers v0.1.3
[46192b85] GPUArraysCore v0.2.0
[61eb1bfa] GPUCompiler v1.8.2
[3587e190] InverseFunctions v0.1.17
[92d709cd] IrrationalConstants v0.2.6
[82899510] IteratorInterfaceExtensions v1.0.0
[692b3bcd] JLLWrappers v1.7.1
[ae98c720] Jieko v0.2.1
[ba0b0d4f] Krylov v0.10.5
[929cbde3] LLVM v9.4.6
[87fe0de2] LineSearch v0.1.6
[7ed4a6bd] LinearSolve v3.58.0
[2ab3a3ac] LogExpFunctions v0.3.29
[e6f89c97] LoggingExtras v1.2.0
[1914dd2f] MacroTools v0.5.16
[bb5d69b7] MaybeInplace v0.1.4
[2e0e35c7] Moshi v0.3.7
[77ba4419] NaNMath v1.1.3
⌃ [8913a72c] NonlinearSolve v4.15.0
⌃ [be0214bd] NonlinearSolveBase v2.11.2
⌅ [5959db7a] NonlinearSolveFirstOrder v1.11.1
[9a2c21bd] NonlinearSolveQuasiNewton v1.12.0
[26075421] NonlinearSolveSpectralMethods v1.6.0
[d8793406] ObjectFile v0.5.0
⌃ [d236fae5] PreallocationTools v0.4.34
[aea7be01] PrecompileTools v1.3.3
[21216c6a] Preferences v1.5.1
[3cdcf5f2] RecipesBase v1.3.4
[731186ca] RecursiveArrayTools v3.48.0
[189a3867] Reexport v1.2.2
[ae029012] Requires v1.3.1
[7e49a35a] RuntimeGeneratedFunctions v0.5.17
⌃ [0bca4576] SciMLBase v2.138.1
[19f34311] SciMLJacobianOperators v0.1.12
⌃ [a6db7da4] SciMLLogging v1.8.0
[c0aeaf25] SciMLOperators v1.15.1
[431bcebd] SciMLPublic v1.0.1
[53ae85a6] SciMLStructures v1.10.0
[6c6a2e73] Scratch v1.3.0
[efcf1570] Setfield v1.1.2
[727e6d20] SimpleNonlinearSolve v2.10.0
[276daf66] SpecialFunctions v2.7.1
[1e83bf80] StaticArraysCore v1.4.4
[10745b16] Statistics v1.11.1
[53d494c1] StructIO v0.3.1
[2efcf032] SymbolicIndexingInterface v0.3.46
[a759f4b9] TimerOutputs v0.5.29
[e689c965] Tracy v0.1.6
[7cc45869] Enzyme_jll v0.0.249+0
[1d5cc7b8] IntelOpenMP_jll v2025.2.0+0
[dad2f222] LLVMExtra_jll v0.0.38+0
[ad6e5548] LibTracyClient_jll v0.13.1+0
[856f044c] MKL_jll v2025.2.0+0
[efe28fd5] OpenSpecFun_jll v0.5.6+0
[1317d2d5] oneTBB_jll v2022.0.0+1
[0dad84c5] ArgTools v1.1.2
[56f22d72] Artifacts v1.11.0
[2a0f44e3] Base64 v1.11.0
[ade2ca70] Dates v1.11.0
[8ba89e20] Distributed v1.11.0
[f43a241f] Downloads v1.7.0
[7b1f6079] FileWatching v1.11.0
[9fa8497b] Future v1.11.0
[b77e0a4c] InteractiveUtils v1.11.0
[ac6e5ff7] JuliaSyntaxHighlighting v1.12.0
[4af54fe1] LazyArtifacts v1.11.0
[b27032c2] LibCURL v0.6.4
[76f85450] LibGit2 v1.11.0
[8f399da3] Libdl v1.11.0
[37e2e46d] LinearAlgebra v1.12.0
[56ddb016] Logging v1.11.0
[d6f4376e] Markdown v1.11.0
[ca575930] NetworkOptions v1.3.0
[44cfe95a] Pkg v1.12.1
[de0858da] Printf v1.11.0
[9a3f8284] Random v1.11.0
[ea8e919c] SHA v0.7.0
[9e88b42a] Serialization v1.11.0
[6462fe0b] Sockets v1.11.0
[2f01184e] SparseArrays v1.12.0
[f489334b] StyledStrings v1.11.0
[fa267f1f] TOML v1.0.3
[a4e569a6] Tar v1.10.0
[cf7118a7] UUIDs v1.11.0
[4ec0a83e] Unicode v1.11.0
[e66e0078] CompilerSupportLibraries_jll v1.3.0+1
[deac9b47] LibCURL_jll v8.15.0+0
[e37daf67] LibGit2_jll v1.9.0+0
[29816b5a] LibSSH2_jll v1.11.3+1
[14a3606d] MozillaCACerts_jll v2025.11.4
[4536629a] OpenBLAS_jll v0.3.29+0
[05823500] OpenLibm_jll v0.8.7+0
[458c3c95] OpenSSL_jll v3.5.4+0
[bea87d4a] SuiteSparse_jll v7.8.3+2
[83775a58] Zlib_jll v1.3.1+2
[8e850b90] libblastrampoline_jll v5.15.0+0
[8e850ede] nghttp2_jll v1.64.0+1
[3f19e933] p7zip_jll v17.7.0+0
Info Packages marked with ⌃ and ⌅ have new versions available. Those with ⌃ may be upgradable, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated -m`- Output of
versioninfo()
Julia Version 1.12.4
Commit 01a2eadb047 (2026-01-06 16:56 UTC)
Build Info:
Official https://julialang.org release
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: 16 × AMD Ryzen 9 8945HS w/ Radeon 780M Graphics
WORD_SIZE: 64
LLVM: libLLVM-18.1.7 (ORCJIT, znver3)
GC: Built with stock GC
Threads: 16 default, 1 interactive, 16 GC (on 16 virtual cores)
Environment:
JULIA_PKG_PRESERVE_TIERED_INSTALLED = true
JULIA_PKG_PRECOMPILE_AUTO = 0
JULIA_NUM_THREADS = auto
JULIA_EDITOR = emacsclientReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working