Open
Description
This example arose from using GAMSFiles to parse a bunch of files used as a test suite for optimization problems (specifically convex3_10_1.gms
). I've set this up so you don't need GAMSFiles to run this demo, although there are commented-out references to that package. (In my local checkout of that package I defined a @zero_adjoint
for GAMSFiles.checkaxes
, and that fixed one problem, woohoo. But this is different.)
Unless I'm missing something, the only operations in this objective are +
, -
, and *
. This is with Mooncake v0.4.129 and DI v0.7.1.
using Mooncake
using DifferentiationInterface
module MooncakeDemo
# using GAMSFiles
function objective(x)
# begin
# (GAMSFiles.checkaxes)(x, (Base.OneTo(10),))
objvar = (((((((((((((((((((((((((((x[1] * x[1] + x[10] * x[10]) * (x[1] * x[1] + x[10] * x[10]) - 4 * x[1]) + 3) + (x[2] * x[2] + x[10] * x[10]) * (x[2] * x[2] + x[10] * x[10])) - 4 * x[2]) + 3) + (x[3] * x[3] + x[10] * x[10]) * (x[3] * x[3] + x[10] * x[10])) - 4 * x[3]) + 3) + (x[4] * x[4] + x[10] * x[10]) * (x[4] * x[4] + x[10] * x[10])) - 4 * x[4]) + 3) + (x[5] * x[5] + x[10] * x[10]) * (x[5] * x[5] + x[10] * x[10])) - 4 * x[5]) + 3) + (x[6] * x[6] + x[10] * x[10]) * (x[6] * x[6] + x[10] * x[10])) - 4 * x[6]) + 3) + (x[7] * x[7] + x[10] * x[10]) * (x[7] * x[7] + x[10] * x[10])) - 4 * x[7]) + 3) + (x[8] * x[8] + x[10] * x[10]) * (x[8] * x[8] + x[10] * x[10])) - 4 * x[8]) + 3) + (x[9] * x[9] + x[10] * x[10]) * (x[9] * x[9] + x[10] * x[10])) - 4 * x[9]) + 3) - 0
return objvar
# end
end
end
x0 = [0.0; fill(1.0, 9)]
f = MooncakeDemo.objective
f(x0) # works
backend = AutoMooncake(; config=nothing)
prepg = prepare_gradient(f, backend, x0)
∇f(x) = gradient(f, prepg, backend, x)
@show isa(∇f(x0), AbstractVector{<:Real})
preph = prepare_hessian(f, backend, x0)
∇²f(x) = hessian(f, preph, backend, x)
@show isa(∇²f(x0), AbstractMatrix{<:Real})
This fails with
julia> include("mooncakebug.jl")
WARNING: replacing module MooncakeDemo.
∇f(x0) isa AbstractVector{<:Real} = true
ERROR: LoadError: MooncakeRuleCompilationError: an error occured while Mooncake was compiling a rule to differentiate something. If the `caused by` error message below does not make it clear to you how the problem can be fixed, please open an issue at github.com/chalk-lab/Mooncake.jl describing your problem.
To replicate this error run the following:
Mooncake.build_rrule(Mooncake.MooncakeInterpreter(), Tuple{typeof(Mooncake.tuple_splat), typeof(Core.kwcall), Tuple{@NamedTuple{debug_mode::Bool, silence_debug_messages::Bool}, typeof(Mooncake.build_rrule), typeof(Main.MooncakeDemo.objective), Vector{Float64}}}; debug_mode=false)
Note that you may need to `using` some additional packages if not all of the names printed in the above signature are available currently in your environment.
Stacktrace:
[1] build_rrule(interp::Mooncake.MooncakeInterpreter{Mooncake.DefaultCtx}, sig_or_mi::Type; debug_mode::Bool, silence_debug_messages::Bool)
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1136
[2] build_rrule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1077 [inlined]
[3] (::Mooncake.DynamicDerivedRule{Dict{…}})(::Mooncake.CoDual{typeof(Mooncake.tuple_splat), Mooncake.NoFData}, ::Mooncake.CoDual{typeof(Core.kwcall), Mooncake.NoFData}, ::Mooncake.CoDual{Tuple{…}, Tuple{…}})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1736
[4] (::Mooncake.RRuleZeroWrapper{…})(::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:302
[5] _apply_iterate_equivalent
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\rrules\builtins.jl:508 [inlined]
[6] (::Tuple{…})(none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…})
@ Base.Experimental .\<missing>:0
[7] (::Mooncake.DerivedRule{…})(::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:966
[8] (::Mooncake.DynamicDerivedRule{…})(::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1739
[9] (::Mooncake.RRuleZeroWrapper{…})(::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:302
[10] #prepare_gradient_cache#668
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interface.jl:483 [inlined]
[11] (::Tuple{…})(none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual)
@ Base.Experimental .\<missing>:0
[12] DerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:966 [inlined]
[13] _build_rule!(rule::Mooncake.LazyDerivedRule{Tuple{…}, Mooncake.DerivedRule{…}}, args::Tuple{Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1827
[14] LazyDerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1822 [inlined]
[15] RRuleZeroWrapper
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:302 [inlined]
[16] prepare_gradient_nokwarg
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\ext\DifferentiationInterfaceMooncakeExt\onearg.jl:112 [inlined]
[17] (::Tuple{…})(none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…})
@ Base.Experimental .\<missing>:0
[18] DerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:966 [inlined]
[19] _build_rule!(rule::Mooncake.LazyDerivedRule{Tuple{…}, Mooncake.DerivedRule{…}}, args::Tuple{Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1827
[20] LazyDerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1822 [inlined]
[21] RRuleZeroWrapper
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:302 [inlined]
[22] gradient
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\first_order\gradient.jl:62 [inlined]
[23] (::Tuple{…})(none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…})
@ Base.Experimental .\<missing>:0
[24] DerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:966 [inlined]
[25] _build_rule!(rule::Mooncake.LazyDerivedRule{Tuple{…}, Mooncake.DerivedRule{…}}, args::Tuple{Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}, Mooncake.CoDual{…}})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1827
[26] LazyDerivedRule
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:1822 [inlined]
[27] RRuleZeroWrapper
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:302 [inlined]
[28] shuffled_gradient
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\first_order\gradient.jl:158 [inlined]
[29] (::Tuple{…})(none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…}, none::Mooncake.CoDual{…})
@ Base.Experimental .\<missing>:0
[30] (::Mooncake.DerivedRule{…})(::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…}, ::Mooncake.CoDual{…})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interpreter\s2s_reverse_mode_ad.jl:966
[31] prepare_pullback_cache(::Function, ::Vararg{Any}; kwargs::@Kwargs{debug_mode::Bool, silence_debug_messages::Bool})
@ Mooncake C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interface.jl:414
[32] prepare_pullback_cache
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\interface.jl:407 [inlined]
[33] prepare_pullback_nokwarg(::Val{…}, ::typeof(DifferentiationInterface.shuffled_gradient), ::AutoMooncake{…}, ::Vector{…}, ::Tuple{…}, ::DifferentiationInterface.FunctionContext{…}, ::Constant{…}, ::Constant{…})
@ DifferentiationInterfaceMooncakeExt C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\ext\DifferentiationInterfaceMooncakeExt\onearg.jl:14
[34] _prepare_hvp_aux(::Val{…}, ::DifferentiationInterface.ReverseOverReverse, ::DifferentiationInterface.DontPrepareInner, ::typeof(Main.MooncakeDemo.objective), ::AutoMooncake{…}, ::Vector{…}, ::Tuple{…})
@ DifferentiationInterface C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hvp.jl:663
[35] prepare_hvp_nokwarg
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hvp.jl:125 [inlined]
[36] _prepare_hessian_aux(::Val{true}, ::DifferentiationInterface.BatchSizeSettings{1, false, true}, ::typeof(Main.MooncakeDemo.objective), ::AutoMooncake{Nothing}, ::Vector{Float64})
@ DifferentiationInterface C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hessian.jl:122
[37] prepare_hessian_nokwarg
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hessian.jl:104 [inlined]
[38] #prepare_hessian#78
@ C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hessian.jl:11 [inlined]
[39] prepare_hessian(::typeof(Main.MooncakeDemo.objective), ::AutoMooncake{Nothing}, ::Vector{Float64})
@ DifferentiationInterface C:\Users\timho\.julia\packages\DifferentiationInterface\alBlj\src\second_order\hessian.jl:8
[40] top-level scope
@ C:\Users\timho\OneDrive\Documents\publications\diquadraticoptim\src\testproblems\gams\mooncakebug.jl:23
[41] include(fname::String)
@ Base.MainInclude .\client.jl:494
[42] top-level scope
@ REPL[10]:1
in expression starting at C:\Users\timho\OneDrive\Documents\publications\diquadraticoptim\src\testproblems\gams\mooncakebug.jl:23
caused by: StackOverflowError:
Stacktrace:
[1] macro expansion
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\tangents.jl:439 [inlined]
[2] macro expansion
@ .\none:0 [inlined]
[3] tangent_type(::Type{Core.CodeInstance})
@ Mooncake .\none:0
--- the last 3 lines are repeated 79982 more times ---
[239950] macro expansion
@ C:\Users\timho\.julia\packages\Mooncake\Bo7Z0\src\tangents.jl:439 [inlined]
[239951] macro expansion
@ .\none:0 [inlined]
Some type information was truncated. Use `show(err)` to see complete types.
I suspect the tangent_type(::Type{Core.CodeInstance})
is unexpected?