Skip to content

[Bug] Enzyme-based line search fails when storing dual variables in non-differentiable arrays #19

@sontheteacher

Description

@sontheteacher

Describe the bug”
When using the :enzyme autodiff backend in LineSearch.jl, the solver throws an internal error if a differentiable (dual) variable is stored into a non-differentiable data structure during line search. This leads to a crash with the message “internal error: IR interpretation: encountered an unexpected or internal error."

Expected behavior
I expected the line search to run successfully using Enzyme’s autodiff without crashing, or at least produce a descriptive error about unsupported operations.

Minimal Reproducible Example 👇

I have wrote a test case below demonstrating this bug. Running the test suite also gives a similar bug.

using LineSearch
using LineSearches
using ADTypes
using Enzyme
using Test
using SciMLBase
using CommonSolve

# Define a simple in-place problem
function nlf(dx, x, p)
    dx .= x .^ 2 .- p
    return dx
end

# Create the problem
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0])

# Create the line search setup
ls = LineSearch.LineSearchesJL(
    method = LineSearches.BackTracking(order = 3),
    autodiff = AutoEnzyme()
)

@testset "Enzyme autodiff test" begin
    try
        # Initialize u and function evaluation
        u = copy(nlp.u0)
        fu = similar(u)
        nlp.f(fu, u, nlp.p)
        
        # Initialize the line search cache - using CommonSolve.init instead of LineSearch.init
        ls_cache = CommonSolve.init(nlp, ls, fu, u)
        
        # Define a descent direction
        du = -fu
        
        # Solve the line search step
        ls_sol = CommonSolve.solve!(ls_cache, u, du)
        
        # Check if the step was successful
        @test ls_sol.retcode == ReturnCode.Success
        
        println("Enzyme test passed!")
    catch e
        println("Enzyme test failed with error:")
        println(e)
        rethrow(e)
    end
end

Error & Stacktrace ⚠️

The error manifests in two stack traces, showing both the immediate broadcast operation failure and the full AD context:

First Stack Trace (Broadcast Operation)

Stacktrace:
    [1] size
      @ ./array.jl:194
    [2] axes
      @ ./abstractarray.jl:98
    [3] newindexer
      @ ./broadcast.jl:598
    [4] extrude
      @ ./broadcast.jl:645
    [5] preprocess
      @ ./broadcast.jl:953
    [6] preprocess_args
      @ ./broadcast.jl:956
    [7] preprocess_args
      @ ./broadcast.jl:955
    [8] preprocess
      @ ./broadcast.jl:952
    [9] override_bc_copyto!
      @ ~/.julia/packages/Enzyme/QsaeA/src/compiler/interpreter.jl:798
   [10] copyto!
      @ ./broadcast.jl:925
   [11] materialize!
      @ ./broadcast.jl:883
   [12] materialize!
      @ ./broadcast.jl:880
   [13] nlf
      @ /mnt/c/Users/sonth/OneDrive/Documents/Academia/Random CS Projects/GSOC2025/LineSearch.jl/test/enzyme_test.jl:11

Second Stack Trace (Full AD Context)

Stacktrace:
    [1] mightalias
      @ ./abstractarray.jl:1537 [inlined]
    [2] unalias
      @ ./abstractarray.jl:1500 [inlined]
    [3] broadcast_unalias
      @ ./broadcast.jl:946 [inlined]
    [4] preprocess
      @ ./broadcast.jl:953 [inlined]
    [5] preprocess_args
      @ ./broadcast.jl:956 [inlined]
    [6] preprocess_args
      @ ./broadcast.jl:955 [inlined]
    [7] preprocess
      @ ./broadcast.jl:952 [inlined]
    [8] override_bc_copyto!
      @ ~/.julia/packages/Enzyme/QsaeA/src/compiler/interpreter.jl:798 [inlined]
    [9] copyto!
      @ ./broadcast.jl:925 [inlined]
   [10] materialize!
      @ ./broadcast.jl:883 [inlined]
   [11] materialize!
      @ ./broadcast.jl:880 [inlined]
   [12] nlf
      @ /mnt/c/Users/sonth/OneDrive/Documents/Academia/Random CS Projects/GSOC2025/LineSearch.jl/test/enzyme_test.jl:11
   [13] NonlinearFunction
      @ ~/.julia/packages/SciMLBase/sYmAV/src/scimlfunctions.jl:2469 [inlined]
   [14] NonlinearFunction
      @ ~/.julia/packages/SciMLBase/sYmAV/src/scimlfunctions.jl:0 [inlined]
   [15] diffejulia_NonlinearFunction_18028_inner_3wrap
      @ ~/.julia/packages/SciMLBase/sYmAV/src/scimlfunctions.jl:0
   [21] value_and_pullback!
      @ DifferentiationInterfaceEnzymeExt ~/.julia/packages/DifferentiationInterface/0ZwTi/ext/DifferentiationInterfaceEnzymeExt/reverse_twoarg.jl:123

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
Status `/mnt/c/Users/sonth/OneDrive/Documents/Academia/Random CS Projects/GSOC2025/LineSearch.jl/test/Project.toml`
  [47edcb42] ADTypes v1.14.0
  [38540f10] CommonSolve v0.2.4
  [7da242da] Enzyme v0.13.30
  [6a86dc24] FiniteDiff v2.27.0
  [f6369f11] ForwardDiff v0.10.38
  [87fe0de2] LineSearch v0.1.4
  [d3d80556] LineSearches v7.3.0
  [37e2e3b7] ReverseDiff v1.15.3
  [0bca4576] SciMLBase v2.75.1
  [9f7883ad] Tracker v0.2.37
  [e88e6eb3] Zygote v0.7.4
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `/mnt/c/Users/sonth/OneDrive/Documents/Academia/Random CS Projects/GSOC2025/LineSearch.jl/test/Manifest.toml`
  [47edcb42] ADTypes v1.14.0
  [621f4979] AbstractFFTs v1.5.0
  [7d9f7c33] Accessors v0.1.42
  [79e6a3ab] Adapt v4.3.0
  [4fba245c] ArrayInterface v7.18.0
  [a9b6321e] Atomix v1.1.1
  [fa961155] CEnum v0.5.0
  [082447d4] ChainRules v1.72.3
  [d360d2e6] ChainRulesCore v1.25.1
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.1
  [34da2185] Compat v4.16.0
  [a33af91c] CompositionsBase v0.1.2
  [2569d6c7] ConcreteStructs v0.2.3
  [187b0558] ConstructionBase v1.5.8
  [9a962f9c] DataAPI v1.16.0
  [e2d170a0] DataValueInterfaces v1.0.0
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
  [a0c0ee7d] DifferentiationInterface v0.6.44
  [ffbed154] DocStringExtensions v0.9.3
  [4e289a0a] EnumX v1.0.4
  [7da242da] Enzyme v0.13.30
  [f151be2c] EnzymeCore v0.8.8
  [e2ba6199] ExprTools v0.1.10
  [55351af7] ExproniconLite v0.10.14
  [9aa1b823] FastClosures v0.3.2
  [1a297f60] FillArrays v1.13.0
  [6a86dc24] FiniteDiff v2.27.0
  [f6369f11] ForwardDiff v0.10.38
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [d9f16b24] Functors v0.5.2
  [0c68f7d7] GPUArrays v11.2.2
  [46192b85] GPUArraysCore v0.2.0
  [61eb1bfa] GPUCompiler v1.2.0
  [076d061b] HashArrayMappedTries v0.2.0
  [7869d1d1] IRTools v0.4.14
  [3587e190] InverseFunctions v0.1.17
  [92d709cd] IrrationalConstants v0.2.4
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.7.0
  [ae98c720] Jieko v0.2.1
  [63c18a36] KernelAbstractions v0.9.34
  [929cbde3] LLVM v9.2.0
  [87fe0de2] LineSearch v0.1.4
  [d3d80556] LineSearches v7.3.0
  [2ab3a3ac] LogExpFunctions v0.3.29
  [1914dd2f] MacroTools v0.5.15
  [bb5d69b7] MaybeInplace v0.1.4
  [2e0e35c7] Moshi v0.3.5
  [d41bc354] NLSolversBase v7.8.3
  [872c559c] NNlib v0.9.28
  [77ba4419] NaNMath v1.1.2
  [d8793406] ObjectFile v0.4.3
  [3bd65402] Optimisers v0.4.5
  [bac558e1] OrderedCollections v1.8.0
  [d96e819e] Parameters v0.12.3
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [c1ae055f] RealDot v0.1.0
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.31.1
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.1
  [37e2e3b7] ReverseDiff v1.15.3
  [7e49a35a] RuntimeGeneratedFunctions v0.5.13
  [0bca4576] SciMLBase v2.75.1
  [19f34311] SciMLJacobianOperators v0.1.1
  [c0aeaf25] SciMLOperators v0.3.12
  [53ae85a6] SciMLStructures v1.7.0
  [7e506255] ScopedValues v1.3.0
  [6c6a2e73] Scratch v1.2.1
  [efcf1570] Setfield v1.1.2
  [dc90abb0] SparseInverseSubset v0.1.2
  [276daf66] SpecialFunctions v2.5.0
  [90137ffa] StaticArrays v1.9.13
  [1e83bf80] StaticArraysCore v1.4.3
  [10745b16] Statistics v1.11.1
  [09ab397b] StructArrays v0.7.0
  [53d494c1] StructIO v0.3.1
  [2efcf032] SymbolicIndexingInterface v0.3.38
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.12.0
  [a759f4b9] TimerOutputs v0.5.28
  [9f7883ad] Tracker v0.2.37
  [3a884ed6] UnPack v1.0.2
  [013be700] UnsafeAtomics v0.3.0
  [e88e6eb3] Zygote v0.7.4
  [700de1a5] ZygoteRules v0.2.7
  [7cc45869] Enzyme_jll v0.0.172+0
  [dad2f222] LLVMExtra_jll v0.0.35+0
  [efe28fd5] OpenSpecFun_jll v0.5.6+0
  [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.6.0
  [7b1f6079] FileWatching v1.11.0
  [9fa8497b] Future v1.11.0
  [b77e0a4c] InteractiveUtils v1.11.0
  [4af54fe1] LazyArtifacts v1.11.0
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2 v1.11.0
  [8f399da3] Libdl v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [56ddb016] Logging v1.11.0
  [d6f4376e] Markdown v1.11.0
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.11.0
  [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.11.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [cf7118a7] UUIDs v1.11.0
  [4ec0a83e] Unicode v1.11.0
  [e66e0078] CompilerSupportLibraries_jll v1.1.1+0
  [deac9b47] LibCURL_jll v8.6.0+0
  [e37daf67] LibGit2_jll v1.7.2+0
  [29816b5a] LibSSH2_jll v1.11.0+1
  [c8ffd9c3] MbedTLS_jll v2.28.6+0
  [14a3606d] MozillaCACerts_jll v2023.12.12
  [4536629a] OpenBLAS_jll v0.3.27+1
  [05823500] OpenLibm_jll v0.8.1+4
  [bea87d4a] SuiteSparse_jll v7.7.0+0
  [83775a58] Zlib_jll v1.2.13+1
  [8e850b90] libblastrampoline_jll v5.11.0+0
  [8e850ede] nghttp2_jll v1.59.0+0
  [3f19e933] p7zip_jll v17.4.0+2
  • Output of versioninfo()
Julia Version 1.11.4
Commit 8561cc3d68d (2025-03-10 11:36 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × 11th Gen Intel(R) Core(TM) i9-11900H @ 2.50GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions