EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
450 stars 64 forks source link

Segfault with function splatting #1942

Closed gdalle closed 2 weeks ago

gdalle commented 3 weeks ago

EDIT: pure Enyzme MWE is below

Here's an MWE for the bug I uncovered in the JuMP docs PR (https://github.com/jump-dev/JuMP.jl/pull/3836). It seems to be due to splatting, which is only necessary because JuMP doesn't use vector arguments:

using ADTypes
import DifferentiationInterface as DI
using Enzyme: Enzyme
using ForwardDiff: ForwardDiff
using Test: Test

f(x::T...) where {T} = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
f_nosplat(x::AbstractVector) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2

function analytic_hessian(x::AbstractVector)
    H = zeros(2, 2)
    H[1, 1] = 1200 * x[1]^2 - 400 * x[2] + 2
    ## H[1, 2] = -400 * x[1] <-- not needed because Hessian is symmetric
    H[2, 1] = H[1, 2] = -400 * x[1]
    H[2, 2] = 200.0
    return H
end

x = [2.0, 3.0]
analytic_hessian(x)

backend = AutoForwardDiff()
DI.hessian(f_nosplat, backend, x)  # works
DI.hessian(splat(f), backend, x)  # works

backend = AutoEnzyme()
DI.hessian(f_nosplat, backend, x)  # works
DI.hessian(splat(f), backend, x)  # segfaults

And here's the error log (on 1.10):

``` [28996] signal (11.1): Segmentation fault in expression starting at /home/guillaume/Work/GitHub/Julia/Sandbox/test.jl:122 _ZNK4llvm10DataLayout12getAlignmentEPNS_4TypeEb at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so (unknown line) LLVMBuildStore at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so (unknown line) LLVMBuildStore at /home/guillaume/.julia/packages/LLVM/joxPv/lib/15/libLLVM.jl:5940 [inlined] store! at /home/guillaume/.julia/packages/LLVM/joxPv/src/irbuilder.jl:341 [inlined] create_abi_wrapper at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:5460 enzyme! at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:4764 unknown function (ip: 0x7b578d742cb8) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #codegen#19000 at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:7801 codegen at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:6638 [inlined] _thunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8909 _thunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8909 [inlined] cached_compilation at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8950 [inlined] thunkbase at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:9082 unknown function (ip: 0x7b578d336439) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #s2067#19055 at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:9219 [inlined] #s2067#19055 at ./none:0 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 GeneratedFunctionStub at ./boot.jl:602 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_call_staged at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/method.c:540 ijl_code_for_staged at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/method.c:593 get_staged at ./compiler/utilities.jl:123 retrieve_code_info at ./compiler/utilities.jl:135 [inlined] InferenceState at ./compiler/inferencestate.jl:430 typeinf_edge at ./compiler/typeinfer.jl:920 abstract_call_method at ./compiler/abstractinterpretation.jl:629 abstract_call_gf_by_type at ./compiler/abstractinterpretation.jl:95 abstract_call_known at ./compiler/abstractinterpretation.jl:2087 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_call at ./compiler/abstractinterpretation.jl:2162 abstract_call at ./compiler/abstractinterpretation.jl:2354 abstract_eval_call at ./compiler/abstractinterpretation.jl:2370 abstract_eval_statement_expr at ./compiler/abstractinterpretation.jl:2380 abstract_eval_statement at ./compiler/abstractinterpretation.jl:2624 abstract_eval_basic_statement at ./compiler/abstractinterpretation.jl:2913 typeinf_local at ./compiler/abstractinterpretation.jl:3098 typeinf_nocycle at ./compiler/abstractinterpretation.jl:3186 _typeinf at ./compiler/typeinfer.jl:247 typeinf at ./compiler/typeinfer.jl:216 typeinf_edge at ./compiler/typeinfer.jl:930 abstract_call_method at ./compiler/abstractinterpretation.jl:629 abstract_call_gf_by_type at ./compiler/abstractinterpretation.jl:95 abstract_call_known at ./compiler/abstractinterpretation.jl:2087 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_apply at ./compiler/abstractinterpretation.jl:1612 abstract_call_known at ./compiler/abstractinterpretation.jl:2004 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_call at ./compiler/abstractinterpretation.jl:2162 abstract_call at ./compiler/abstractinterpretation.jl:2354 abstract_eval_call at ./compiler/abstractinterpretation.jl:2370 abstract_eval_statement_expr at ./compiler/abstractinterpretation.jl:2380 abstract_eval_statement at ./compiler/abstractinterpretation.jl:2624 abstract_eval_basic_statement at ./compiler/abstractinterpretation.jl:2913 typeinf_local at ./compiler/abstractinterpretation.jl:3098 typeinf_nocycle at ./compiler/abstractinterpretation.jl:3186 _typeinf at ./compiler/typeinfer.jl:247 typeinf at ./compiler/typeinfer.jl:216 typeinf_ext at ./compiler/typeinfer.jl:1051 typeinf_ext_toplevel at ./compiler/typeinfer.jl:1082 typeinf_ext_toplevel at ./compiler/typeinfer.jl:1078 jfptr_typeinf_ext_toplevel_35703.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] jl_type_infer at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:394 jl_generate_fptr_impl at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jitlayers.cpp:504 jl_compile_method_internal at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2481 [inlined] jl_compile_method_internal at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2368 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2887 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 runtime_iterate_augfwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:1336 [inlined] runtime_iterate_augfwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:0 [inlined] fwddiffe16julia_runtime_iterate_augfwd_3919_inner_1wrap at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:0 macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] ForwardModeThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8194 [inlined] macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:305 [inlined] runtime_generic_fwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:337 unknown function (ip: 0x7b578d32cb7f) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 Splat at ./operators.jl:1271 [inlined] diffejulia_Splat_3539wrap at ./operators.jl:0 [inlined] macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] CombinedAdjointThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8178 [inlined] autodiff_deferred at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:781 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:512 [inlined] gradient at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/ext/DifferentiationInterfaceEnzymeExt/reverse_onearg.jl:223 inner_gradient at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/second_order/hvp.jl:113 [inlined] fwddiffe16julia_inner_gradient_3533wrap at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/second_order/hvp.jl:0 macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] ForwardModeThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8194 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:647 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:537 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:504 [inlined] pushforward at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/ext/DifferentiationInterfaceEnzymeExt/forward_onearg.jl:75 [inlined] hvp at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/second_order/hvp.jl:187 [inlined] #61 at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/second_order/hessian.jl:115 iterate at ./generator.jl:47 [inlined] _collect at ./array.jl:854 collect_similar at ./array.jl:763 [inlined] map at ./abstractarray.jl:3285 [inlined] hessian at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/second_order/hessian.jl:114 hessian at /home/guillaume/.julia/packages/DifferentiationInterface/gPcSq/src/fallbacks/no_prep.jl:102 [inlined] #di_∇²f#16 at /home/guillaume/Work/GitHub/Julia/Sandbox/test.jl:110 di_∇²f at /home/guillaume/Work/GitHub/Julia/Sandbox/test.jl:109 unknown function (ip: 0x7b578d7e5a96) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] do_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/builtins.c:768 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] do_call at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:126 eval_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:223 eval_stmt_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:174 [inlined] eval_body at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:617 jl_interpret_toplevel_thunk at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:775 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:934 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 ijl_toplevel_eval_in at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:985 eval at ./boot.jl:385 [inlined] include_string at ./loading.jl:2076 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 _include at ./loading.jl:2136 include at ./client.jl:489 unknown function (ip: 0x7b57a6b0b705) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] do_call at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:126 eval_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:223 eval_stmt_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:174 [inlined] eval_body at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:617 jl_interpret_toplevel_thunk at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:775 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:934 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 ijl_toplevel_eval_in at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:985 eval at ./boot.jl:385 [inlined] eval_user_input at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:150 repl_backend_loop at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:246 #start_repl_backend#46 at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:231 start_repl_backend at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:228 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #run_repl#59 at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:389 run_repl at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:375 jfptr_run_repl_91805.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #1013 at ./client.jl:432 jfptr_YY.1013_82772.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] jl_f__call_latest at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/builtins.c:812 #invokelatest#2 at ./essentials.jl:892 [inlined] invokelatest at ./essentials.jl:889 [inlined] run_main_repl at ./client.jl:416 exec_options at ./client.jl:333 _start at ./client.jl:552 jfptr__start_82798.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] true_main at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jlapi.c:582 jl_repl_entrypoint at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jlapi.c:731 main at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/cli/loader_exe.c:58 unknown function (ip: 0x7b57a7c29d8f) __libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line) unknown function (ip: 0x4010b8) Allocations: 53405623 (Pool: 53287855; Big: 117768); GC: 59 Segmentation fault (core dumped) ```
gdalle commented 3 weeks ago

Here's a pure-Enzyme reproducer:

using Enzyme
f(x::T...) where {T} = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
x = [2.0, 3.0]
hvp(splat(f), x, zero(x))

Error log:

``` [44569] signal (11.1): Segmentation fault in expression starting at /home/guillaume/Work/GitHub/Julia/DifferentiationInterface.jl/DifferentiationInterface/test/playground.jl:4 _ZNK4llvm10DataLayout12getAlignmentEPNS_4TypeEb at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so (unknown line) LLVMBuildStore at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so (unknown line) LLVMBuildStore at /home/guillaume/.julia/packages/LLVM/joxPv/lib/15/libLLVM.jl:5940 [inlined] store! at /home/guillaume/.julia/packages/LLVM/joxPv/src/irbuilder.jl:341 [inlined] create_abi_wrapper at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:5460 enzyme! at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:4764 unknown function (ip: 0x706de37b99c8) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #codegen#19000 at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:7801 codegen at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:6638 [inlined] _thunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8909 _thunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8909 [inlined] cached_compilation at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8950 [inlined] thunkbase at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:9082 unknown function (ip: 0x706de37c0409) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #s2067#19055 at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:9219 [inlined] #s2067#19055 at ./none:0 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 GeneratedFunctionStub at ./boot.jl:602 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_call_staged at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/method.c:540 ijl_code_for_staged at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/method.c:593 get_staged at ./compiler/utilities.jl:123 retrieve_code_info at ./compiler/utilities.jl:135 [inlined] InferenceState at ./compiler/inferencestate.jl:430 typeinf_edge at ./compiler/typeinfer.jl:920 abstract_call_method at ./compiler/abstractinterpretation.jl:629 abstract_call_gf_by_type at ./compiler/abstractinterpretation.jl:95 abstract_call_known at ./compiler/abstractinterpretation.jl:2087 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_call at ./compiler/abstractinterpretation.jl:2162 abstract_call at ./compiler/abstractinterpretation.jl:2354 abstract_eval_call at ./compiler/abstractinterpretation.jl:2370 abstract_eval_statement_expr at ./compiler/abstractinterpretation.jl:2380 abstract_eval_statement at ./compiler/abstractinterpretation.jl:2624 abstract_eval_basic_statement at ./compiler/abstractinterpretation.jl:2913 typeinf_local at ./compiler/abstractinterpretation.jl:3098 typeinf_nocycle at ./compiler/abstractinterpretation.jl:3186 _typeinf at ./compiler/typeinfer.jl:247 typeinf at ./compiler/typeinfer.jl:216 typeinf_edge at ./compiler/typeinfer.jl:930 abstract_call_method at ./compiler/abstractinterpretation.jl:629 abstract_call_gf_by_type at ./compiler/abstractinterpretation.jl:95 abstract_call_known at ./compiler/abstractinterpretation.jl:2087 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_apply at ./compiler/abstractinterpretation.jl:1612 abstract_call_known at ./compiler/abstractinterpretation.jl:2004 abstract_call at ./compiler/abstractinterpretation.jl:2169 abstract_call at ./compiler/abstractinterpretation.jl:2162 abstract_call at ./compiler/abstractinterpretation.jl:2354 abstract_eval_call at ./compiler/abstractinterpretation.jl:2370 abstract_eval_statement_expr at ./compiler/abstractinterpretation.jl:2380 abstract_eval_statement at ./compiler/abstractinterpretation.jl:2624 abstract_eval_basic_statement at ./compiler/abstractinterpretation.jl:2913 typeinf_local at ./compiler/abstractinterpretation.jl:3098 typeinf_nocycle at ./compiler/abstractinterpretation.jl:3186 _typeinf at ./compiler/typeinfer.jl:247 typeinf at ./compiler/typeinfer.jl:216 typeinf_ext at ./compiler/typeinfer.jl:1051 typeinf_ext_toplevel at ./compiler/typeinfer.jl:1082 typeinf_ext_toplevel at ./compiler/typeinfer.jl:1078 jfptr_typeinf_ext_toplevel_35703.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] jl_type_infer at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:394 jl_generate_fptr_impl at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jitlayers.cpp:504 jl_compile_method_internal at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2481 [inlined] jl_compile_method_internal at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2368 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2887 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 runtime_iterate_augfwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:1336 [inlined] runtime_iterate_augfwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:0 [inlined] fwddiffejulia_runtime_iterate_augfwd_2717_inner_1wrap at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:0 macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] ForwardModeThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8194 [inlined] runtime_generic_fwd at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/rules/jitrules.jl:305 unknown function (ip: 0x706de37ae3ff) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 Splat at ./operators.jl:1271 [inlined] diffejulia_Splat_2301wrap at ./operators.jl:0 [inlined] macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] CombinedAdjointThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8178 [inlined] autodiff_deferred at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:781 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:512 [inlined] gradient! at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:1772 [inlined] fwddiffejulia_gradient__1913wrap at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:0 macro expansion at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8839 [inlined] enzyme_call at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8405 [inlined] ForwardModeThunk at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/compiler.jl:8194 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:647 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:537 [inlined] autodiff at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:504 [inlined] hvp! at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:2350 [inlined] hvp at /home/guillaume/.julia/packages/Enzyme/8GSlk/src/Enzyme.jl:2317 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] do_call at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:126 eval_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:223 eval_stmt_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:174 [inlined] eval_body at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:617 jl_interpret_toplevel_thunk at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:775 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:934 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 ijl_toplevel_eval_in at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:985 eval at ./boot.jl:385 [inlined] include_string at ./loading.jl:2076 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 _include at ./loading.jl:2136 include at ./client.jl:489 unknown function (ip: 0x706dfb90b705) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] do_call at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:126 eval_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:223 eval_stmt_value at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:174 [inlined] eval_body at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:617 jl_interpret_toplevel_thunk at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/interpreter.c:775 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:934 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 jl_toplevel_eval_flex at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:877 ijl_toplevel_eval_in at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/toplevel.c:985 eval at ./boot.jl:385 [inlined] eval_user_input at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:150 repl_backend_loop at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:246 #start_repl_backend#46 at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:231 start_repl_backend at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:228 _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #run_repl#59 at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:389 run_repl at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:375 jfptr_run_repl_91805.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 #1013 at ./client.jl:432 jfptr_YY.1013_82772.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] jl_f__call_latest at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/builtins.c:812 #invokelatest#2 at ./essentials.jl:892 [inlined] invokelatest at ./essentials.jl:889 [inlined] run_main_repl at ./client.jl:416 exec_options at ./client.jl:333 _start at ./client.jl:552 jfptr__start_82798.1 at /home/guillaume/.julia/juliaup/julia-1.10.5+0.x64.linux.gnu/lib/julia/sys.so (unknown line) _jl_invoke at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:2895 [inlined] ijl_apply_generic at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gf.c:3077 jl_apply at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined] true_main at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jlapi.c:582 jl_repl_entrypoint at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/jlapi.c:731 main at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/cli/loader_exe.c:58 unknown function (ip: 0x706dfca29d8f) __libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line) unknown function (ip: 0x4010b8) Allocations: 33256952 (Pool: 33186039; Big: 70913); GC: 49 Segmentation fault (core dumped) ```
gdalle commented 2 weeks ago

Note that with the latest changes to DI's handling of StaticArrays, switching the Vector for an SVector removes the segfault (because splatting of an SVector is type-stable):

import DifferentiationInterface as DI
using Enzyme: Enzyme
using StaticArrays

f(x::T...) where {T} = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
f_nosplat(x::AbstractVector) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2

xs = SVector(2.0, 3.0)

backend = AutoEnzyme()
DI.hessian(f_nosplat, backend, xs)  # works
DI.hessian(splat(f), backend, xs)  # works
wsmoses commented 2 weeks ago

Resolved by https://github.com/EnzymeAD/Enzyme.jl/pull/1975

gdalle commented 1 week ago

I don't think this is completely solved? With Enzyme v0.13.11 I don't get a segfault but I do get an error:

julia> Enzyme.hvp(splat(f), x, zero(x))
ERROR: Enzyme execution failed.
Enzyme: Not yet implemented, mixed activity for jl_new_struct constants=Bool[1, 1, 1, 1, 1, 1, 0, 1, 1]   %16 = call noalias nonnull "enzyme_type"="{[-1]:Pointer}" {} addrspace(10)* ({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)*, {} addrspace(10)*, ...) @julia.call({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)* noundef nonnull @jl_f_tuple, {} addrspace(10)* noundef null, {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135706086968176 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135706049721088 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135706086968176 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135703490524368 to {}*) to {} addrspace(10)*), {} addrspace(10)* nonnull %15, {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135703919856144 to {}*) to {} addrspace(10)*), {} addrspace(10)* nonnull %11, {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135705534029648 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135706276003848 to {}*) to {} addrspace(10)*)) #17, !dbg !58 Tuple{Bool, Any, Any}[(1, Val{false}, GPUCompiler.BITS_REF), (1, Val{1}, GPUCompiler.BITS_REF), (1, Val{false}, GPUCompiler.BITS_REF), (1, Type{@NamedTuple{1, 2, 3}}, GPUCompiler.BITS_REF), (0, nothing, nothing), (1, Type{typeof(f)}, GPUCompiler.BITS_REF), (0, nothing, nothing), (1, typeof(f), GPUCompiler.BITS_REF), (1, Nothing, GPUCompiler.BITS_REF)] LLVM.User[LLVM.ConstantExpr(0x0000000019ed5530), LLVM.ConstantExpr(0x000000001affa930), LLVM.ConstantExpr(0x0000000019ed5530), LLVM.ConstantExpr(0x000000001df82d30), LLVM.CallInst(%15 = call nonnull "enzyme_type"="{[-1]:Pointer}" {} addrspace(10)* ({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)*, {} addrspace(10)*, ...) @julia.call({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)* noundef nonnull @ijl_apply_generic, {} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 135706048967360 to {}*) to {} addrspace(10)*), {} addrspace(10)* nonnull %14) #16, !dbg !58), LLVM.ConstantExpr(0x000000001b075fb0), LLVM.CallInst(%11 = call nonnull "enzyme_type"="{[-1]:Pointer}" {} addrspace(10)* ({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)*, {} addrspace(10)*, ...) @julia.call({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)* noundef nonnull @jl_f__apply_iterate, {} addrspace(10)* noundef null, {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135706084209456 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 135702402236400 to {}*) to {} addrspace(10)*), {} addrspace(10)* nonnull %10) #15, !dbg !55), LLVM.ConstantExpr(0x0000000014b05f30), LLVM.ConstantExpr(0x0000000012df5ff0)]

Stacktrace:
  [1] runtime_iterate_augfwd
    @ ~/.julia/packages/Enzyme/vgArw/src/rules/jitrules.jl:77 [inlined]
  [2] runtime_iterate_augfwd
    @ ~/.julia/packages/Enzyme/vgArw/src/rules/jitrules.jl:0 [inlined]
  [3] fwddiffejulia_runtime_iterate_augfwd_29831_inner_1wrap
    @ ~/.julia/packages/Enzyme/vgArw/src/rules/jitrules.jl:0
  [4] macro expansion
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:8136 [inlined]
  [5] enzyme_call
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7702 [inlined]
  [6] ForwardModeThunk
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7491 [inlined]
  [7] runtime_generic_fwd(activity::Type{…}, runtimeActivity::Val{…}, width::Val{…}, RT::Val{…}, f::typeof(Enzyme.Compiler.runtime_iterate_augfwd), df::Nothing, primal_1::Type{…}, shadow_1_1::Nothing, primal_2::Val{…}, shadow_2_1::Nothing, primal_3::Val{…}, shadow_3_1::Nothing, primal_4::Val{…}, shadow_4_1::Nothing, primal_5::Val{…}, shadow_5_1::Nothing, primal_6::typeof(f), shadow_6_1::Nothing, primal_7::Nothing, shadow_7_1::Nothing, primal_8::Vector{…}, shadow_8_1::Vector{…}, primal_9::Vector{…}, shadow_9_1::Vector{…})
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/vgArw/src/rules/jitrules.jl:305
  [8] Splat
    @ ./operators.jl:1271 [inlined]
  [9] diffejulia_Splat_30897wrap
    @ ./operators.jl:0 [inlined]
 [10] macro expansion
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:8136 [inlined]
 [11] enzyme_call
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7702 [inlined]
 [12] CombinedAdjointThunk
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7475 [inlined]
 [13] autodiff_deferred
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:781 [inlined]
 [14] autodiff
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:512 [inlined]
 [15] gradient!
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:1786 [inlined]
 [16] fwddiffejulia_gradient__30894wrap
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:0
 [17] macro expansion
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:8136 [inlined]
 [18] enzyme_call
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7702 [inlined]
 [19] ForwardModeThunk
    @ ~/.julia/packages/Enzyme/vgArw/src/compiler.jl:7491 [inlined]
 [20] autodiff
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:647 [inlined]
 [21] autodiff
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:537 [inlined]
 [22] autodiff
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:504 [inlined]
 [23] hvp!
    @ ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:2497 [inlined]
 [24] hvp(f::Base.Splat{typeof(f)}, x::Vector{Float64}, v::Vector{Float64})
    @ Enzyme ~/.julia/packages/Enzyme/vgArw/src/Enzyme.jl:2464
 [25] top-level scope
    @ REPL[50]:1
Some type information was truncated. Use `show(err)` to see complete types.
wsmoses commented 1 week ago

Yup that's a different issue from the segfault (which comes from LLVM.jl segfauting when called).

You can open another issue for this if you ike, but also it's super type unstable and not a high priority to resolve that cause.