Closed JohannesNaegele closed 1 month ago
Ah there's a type unstable union on the inside of the code.
What happens if you try https://enzyme.mit.edu/julia/stable/api/#Enzyme.API.strictAliasing!-Tuple{Any}
using Enzyme
using Graphs, GraphsFlows
# Graph from GraphsFlows.jl demo
flow_graph = Graphs.DiGraph(8) # Create a flow graph
flow_edges = [
(1,2,10),(1,3,5),(1,4,15),(2,3,4),(2,5,9),
(2,6,15),(3,4,4),(3,6,8),(4,7,16),(5,6,15),
(5,8,10),(6,7,15),(6,8,10),(7,3,6),(7,8,10)
]
capacity_matrix = zeros(8, 8) # Create a capacity matrix
for e in flow_edges
u, v, f = e
Graphs.add_edge!(flow_graph, u, v)
capacity_matrix[u,v] = f
end
# Some arbitrary function which depends on the max-flow results
foo(capacity_matrix) = maximum_flow(flow_graph, 1, 8, capacity_matrix, algorithm=DinicAlgorithm())[2][1, 2] # Run Dinic's algorithm
foo(capacity_matrix) # 10.0
Enzyme.API.strictAliasing!(false)
gradient(Forward, foo, capacity_matrix)
It still errors:
julia> show(err)
1-element ExceptionStack:
LoadError: Enzyme execution failed.
Mismatched activity for: %unbox197 = phi i64 [ 0, %L385 ], [ %unionalloca166.sroa.0.0.copyload226, %post_union_move209 ], !dbg !140 const val: i64 0
Type tree: {}
You may be using a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#Activity-of-temporary-storage). If not, please open an issue, and either rewrite this variable to not be conditionally active or use Enzyme.API.runtimeActivity!(true) as a workaround for now
Stacktrace:
[1] iterate
@ ./range.jl:901
[2] fill!
@ ./array.jl:396
[3] zeros
@ ./array.jl:637
[4] zeros
@ ./array.jl:632
[5] dinic_impl
@ ~/.julia/packages/GraphsFlows/bSpFT/src/dinic.jl:18
[6] dinic_impl
@ ~/.julia/packages/SimpleTraits/l1ZsK/src/SimpleTraits.jl:331
[7] maximum_flow
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:93
[8] maximum_flow
@ ~/.julia/packages/SimpleTraits/l1ZsK/src/SimpleTraits.jl:331
[9] #maximum_flow#1
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:179
Stacktrace:
[1] throwerr(cstr::Cstring)
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:1325
[2] iterate
@ ./range.jl:901 [inlined]
[3] fill!
@ ./array.jl:396 [inlined]
[4] zeros
@ ./array.jl:637 [inlined]
[5] zeros
@ ./array.jl:632 [inlined]
[6] dinic_impl
@ ~/.julia/packages/GraphsFlows/bSpFT/src/dinic.jl:18 [inlined]
[7] dinic_impl
@ ~/.julia/packages/SimpleTraits/l1ZsK/src/SimpleTraits.jl:331 [inlined]
[8] maximum_flow
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:93 [inlined]
[9] maximum_flow
@ ~/.julia/packages/SimpleTraits/l1ZsK/src/SimpleTraits.jl:331 [inlined]
[10] #maximum_flow#1
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:179
[11] maximum_flow
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:166 [inlined]
[12] fwddiffe64julia_maximum_flow_3559wrap
@ ~/.julia/packages/GraphsFlows/bSpFT/src/maximum_flow.jl:0
[13] macro expansion
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5719 [inlined]
[14] enzyme_call(::Val{false}, ::Ptr{Nothing}, ::Type{Enzyme.Compiler.ForwardModeThunk{Ptr{Nothing}, Const{typeof(Core.kwcall)}, BatchDuplicated{Tuple{Union{Float64, Int64}, Matrix{Float64}}, 64}, Tuple{Const{@NamedTuple{algorithm::DinicAlgorithm}}, Const{typeof(maximum_flow)}, Const{SimpleDiGraph{Int64}}, Const{Int64}, Const{Int64}, BatchDuplicated{Matrix{Float64}, 64}}, 64, true}}, ::Val{64}, ::Val{true}, ::Type{Tuple{Const{@NamedTuple{algorithm::DinicAlgorithm}}, Const{typeof(maximum_flow)}, Const{SimpleDiGraph{Int64}}, Const{Int64}, Const{Int64}, BatchDuplicated{Matrix{Float64}, 64}}}, ::Type{BatchDuplicated{Tuple{Union{Float64, Int64}, Matrix{Float64}}, 64}}, ::Const{typeof(Core.kwcall)}, ::Type{Nothing}, ::Const{@NamedTuple{algorithm::DinicAlgorithm}}, ::Const{typeof(maximum_flow)}, ::Const{SimpleDiGraph{Int64}}, ::Const{Int64}, ::Const{Int64}, ::BatchDuplicated{Matrix{Float64}, 64})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5385
[15] (::Enzyme.Compiler.ForwardModeThunk{Ptr{Nothing}, Const{typeof(Core.kwcall)}, BatchDuplicated{Tuple{Union{Float64, Int64}, Matrix{Float64}}, 64}, Tuple{Const{@NamedTuple{algorithm::DinicAlgorithm}}, Const{typeof(maximum_flow)}, Const{SimpleDiGraph{Int64}}, Const{Int64}, Const{Int64}, BatchDuplicated{Matrix{Float64}, 64}}, 64, true})(::Const{typeof(Core.kwcall)}, ::Const{@NamedTuple{algorithm::DinicAlgorithm}}, ::Vararg{Any})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5267
[16] macro expansion
@ ~/.julia/packages/Enzyme/srACB/src/rules/jitrules.jl:116 [inlined]
[17] runtime_generic_fwd(::Type{Val{(false, false, false, false, false, false, true)}}, ::Val{64}, ::Val{@NamedTuple{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65}}, ::typeof(Core.kwcall), ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::@NamedTuple{algorithm::DinicAlgorithm}, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::typeof(maximum_flow), ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::SimpleDiGraph{Int64}, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Int64, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Int64, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64}, ::Matrix{Float64})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/rules/jitrules.jl:144
[18] foo
@ ~/Documents/GitHub/foo/test_costs.jl:55 [inlined]
[19] fwddiffe64julia_foo_2274wrap
@ ~/Documents/GitHub/foo/test_costs.jl:0
[20] macro expansion
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5719 [inlined]
[21] enzyme_call
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5385 [inlined]
[22] ForwardModeThunk
@ Enzyme.Compiler ~/.julia/packages/Enzyme/srACB/src/compiler.jl:5267 [inlined]
[23] autodiff(::ForwardMode{FFIABI}, f::Const{typeof(foo)}, ::Type{BatchDuplicatedNoNeed}, args::BatchDuplicated{Matrix{Float64}, 64})
@ Enzyme ~/.julia/packages/Enzyme/srACB/src/Enzyme.jl:399
[24] autodiff
@ ~/.julia/packages/Enzyme/srACB/src/Enzyme.jl:303 [inlined]
[25] gradient(::ForwardMode{FFIABI}, f::Function, x::Matrix{Float64}; shadow::NTuple{64, Matrix{Float64}})
@ Enzyme ~/.julia/packages/Enzyme/srACB/src/Enzyme.jl:1040
[26] gradient(::ForwardMode{FFIABI}, f::Function, x::Matrix{Float64})
@ Enzyme ~/.julia/packages/Enzyme/srACB/src/Enzyme.jl:1036
[27] top-level scope
@ ~/Documents/GitHub/foo/test_costs.jl:59
[28] eval
@ ./boot.jl:385 [inlined]
[29] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:2070
[30] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::@Kwargs{})
@ Base ./essentials.jl:887
[31] invokelatest(::Any, ::Any, ::Vararg{Any})
@ Base ./essentials.jl:884
[32] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:271
[33] (::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:181
[34] withpath(f::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:276
[35] (::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:179
[36] hideprompt(f::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:38
[37] (::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:150
[38] with_logstate(f::Function, logstate::Any)
@ Base.CoreLogging ./logging.jl:515
[39] with_logger
@ ./logging.jl:627 [inlined]
[40] (::VSCodeServer.var"#66#71"{VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:263
[41] #invokelatest#2
@ Base ./essentials.jl:887 [inlined]
[42] invokelatest(::Any)
@ Base ./essentials.jl:884
[43] (::VSCodeServer.var"#64#65")()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:34
in expression starting at /Users/johannes/Documents/GitHub/foo/test_costs.jl:59
This latter error should now be fixed on main, please reopen if it persists.
Hi, here a proper error message for this question. I want to differentiate a GraphsFlows.jl maximum flow function.