EnzymeAD / Enzyme.jl

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

Uninterpretable error with simple mistake #730

Closed willtebbutt closed 1 year ago

willtebbutt commented 1 year ago

edit: I was doing something silly, but didn't realise, so this is more of a user-friendliness issue than anything else I guess.

Enzyme v0.11.0

julia> versioninfo()
Julia Version 1.9.0-rc2
Commit 72aec423c2a (2023-04-01 10:41 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin21.4.0)
  CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 6 on 12 virtual cores
Environment:
  JULIA_NUM_THREADS = 6

MWE:

using Enzyme

x = randn(6)
autodiff(Reverse, sum, Active(x))

yields

Call parameter type does not match function signature!
  %4 = getelementptr inbounds [1 x [1 x {} addrspace(10)*]], [1 x [1 x {} addrspace(10)*]]* %3, i64 0, i64 0
 {} addrspace(10)*  call fastcc void @diffejulia_mapreduce_impl_1175({} addrspace(10)* %0, [1 x {} addrspace(10)*]* nonnull %4, i64 1, i64 %9, double %1), !dbg !269
in function diffejulia_sum_1172_inner_1wrap
Call parameter type does not match function signature!
  %4 = getelementptr inbounds [1 x [1 x {} addrspace(10)*]], [1 x [1 x {} addrspace(10)*]]* %3, i64 0, i64 0
 {} addrspace(10)*  call fastcc void @diffejulia_mapreduce_impl_2015({} addrspace(10)* %0, [1 x {} addrspace(10)*]* nonnull %4, i64 1, i64 %9, double %1), !dbg !269
in function diffejulia_sum_2012_inner_1wrap
Call parameter type does not match function signature!
  %4 = getelementptr inbounds [1 x [1 x {} addrspace(10)*]], [1 x [1 x {} addrspace(10)*]]* %3, i64 0, i64 0
 {} addrspace(10)*  call fastcc void @diffejulia_mapreduce_impl_2023({} addrspace(10)* %0, [1 x {} addrspace(10)*]* nonnull %4, i64 1, i64 %9, double %1), !dbg !269
in function diffejulia_sum_2020_inner_1wrap
ERROR: LLVM error: Broken function found, compilation aborted!
Stacktrace:
  [1] handle_error(reason::Cstring)
    @ LLVM ~/.julia/packages/LLVM/TLGyi/src/core/context.jl:118
  [2] LLVMRunPassManager
    @ ~/.julia/packages/LLVM/TLGyi/lib/13/libLLVM_h.jl:4898 [inlined]
  [3] run!
    @ ~/.julia/packages/LLVM/TLGyi/src/passmanager.jl:39 [inlined]
  [4] (::Enzyme.Compiler.var"#116#118"{LLVM.Module})(pm::LLVM.ModulePassManager)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/EncRR/src/compiler/optimize.jl:543
  [5] LLVM.ModulePassManager(::Enzyme.Compiler.var"#116#118"{LLVM.Module}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ LLVM ~/.julia/packages/LLVM/TLGyi/src/passmanager.jl:33
  [6] ModulePassManager
    @ ~/.julia/packages/LLVM/TLGyi/src/passmanager.jl:30 [inlined]
  [7] post_optimze!
    @ ~/.julia/packages/Enzyme/EncRR/src/compiler/optimize.jl:540 [inlined]
  [8] post_optimze!(mod::LLVM.Module, tm::LLVM.TargetMachine)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/EncRR/src/compiler/optimize.jl:530
  [9] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, ctx::Nothing, postopt::Bool)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/EncRR/src/compiler.jl:8453
 [10] _thunk
    @ ~/.julia/packages/Enzyme/EncRR/src/compiler.jl:8431 [inlined]
 [11] cached_compilation
    @ ~/.julia/packages/Enzyme/EncRR/src/compiler.jl:8469 [inlined]
 [12] #s286#175
    @ ~/.julia/packages/Enzyme/EncRR/src/compiler.jl:8527 [inlined]
 [13] var"#s286#175"(FA::Any, A::Any, TT::Any, Mode::Any, ModifiedBetween::Any, width::Any, ReturnPrimal::Any, ShadowInit::Any, World::Any, ::Any, ::Any, ::Any, ::Any, tt::Any, ::Any, ::Any, ::Any, ::Any, ::Any)
    @ Enzyme.Compiler ./none:0
 [14] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:602
 [15] thunk
    @ ~/.julia/packages/Enzyme/EncRR/src/compiler.jl:8486 [inlined]
 [16] autodiff
    @ ~/.julia/packages/Enzyme/EncRR/src/Enzyme.jl:199 [inlined]
 [17] autodiff
    @ ~/.julia/packages/Enzyme/EncRR/src/Enzyme.jl:228 [inlined]
 [18] autodiff(mode::EnzymeCore.ReverseMode{false}, f::typeof(sum), args::Active{Vector{Float64}})
    @ Enzyme ~/.julia/packages/Enzyme/EncRR/src/Enzyme.jl:214
 [19] top-level scope
    @ REPL[6]:1

I tried implementing my own summation function:

function my_sum(x)
    v = 0.0
    for n in 1:length(x)
        v += x[n]
    end
    return v
end

autodiff(Reverse, my_sum, Active(x))

but, alas, it segfaults:

[18981] signal (11.1): Segmentation fault: 11
in expression starting at REPL[7]:1
unknown function (ip: 0x1077c6018)
Allocations: 21303179 (Pool: 21275962; Big: 27217); GC: 28
[1]    18980 segmentation fault  julia
theogf commented 1 year ago

You can't call Active on a Vector, you need to use Duplicated

willtebbutt commented 1 year ago

Hmmm I guess I should have known that, must have missed that in the docs.

Would be helpful to have a good error thrown if someone does this though

theogf commented 1 year ago

#active

wsmoses commented 1 year ago

Duplicate of https://github.com/EnzymeAD/Enzyme.jl/issues/318, PR's welcome!