Enzyme.jl icon indicating copy to clipboard operation
Enzyme.jl copied to clipboard

Captured `x` breaks autodiff

Open vchuravy opened this issue 3 years ago • 4 comments

 let 
        function loadsin(xp)
            x = @inbounds xp[1]
            @inbounds xp[1] = 0.0
            sin(x)
        end
        global invsin
        function invsin(xp)
            xp = Base.invokelatest(convert, Vector{Float64}, xp)
            loadsin(xp)
        end
        x = [2.0]
    end
    x = [2.0]
    dx = [0.0]
    Enzyme.autodiff(invsin, Active, Duplicated(x, dx))

vchuravy avatar Jan 31 '22 16:01 vchuravy

The particular bug:

ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] unsafe_convert
   @ ~/.julia/packages/Enzyme/di3zM/src/compiler.jl:4277 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/Enzyme/di3zM/src/compiler.jl:4520 [inlined]
 [3] enzyme_call
   @ ~/.julia/packages/Enzyme/di3zM/src/compiler.jl:4316 [inlined]
 [4] AugmentedForwardThunk
   @ ~/.julia/packages/Enzyme/di3zM/src/compiler.jl:4307 [inlined]
 [5] autodiff
   @ ~/.julia/packages/Enzyme/di3zM/src/Enzyme.jl:272 [inlined]
 [6] autodiff(f::typeof(invsin), #unused#::Type{Active}, args::Duplicated{Vector{Float64}})
   @ Enzyme ~/.julia/packages/Enzyme/di3zM/src/Enzyme.jl:411
 [7] top-level scope
   @ REPL[74]:1

wsmoses avatar Aug 06 '22 17:08 wsmoses

Present duplicate of https://github.com/EnzymeAD/Enzyme.jl/issues/293

wsmoses avatar Aug 08 '22 18:08 wsmoses

Fixed by #408

vchuravy avatar Aug 19 '22 17:08 vchuravy

On 1.6:

truct return: Error During Test at /home/runner/work/Enzyme.jl/Enzyme.jl/test/runtests.jl:257
  Test threw exception
  Expression: Enzyme.autodiff(invsin, Active, Duplicated(x, dx)) == ()
  TypeError: in typeassert, expected Enzyme.Compiler.Tape, got a value of type Tuple{Core.Compiler.Pair{Core.Compiler.Pair{Int64, Int64}, Any}, Tuple{Int64, Int64}}
Struct return: Test Failed at /home/runner/work/Enzyme.jl/Enzyme.jl/test/runtests.jl:258
  Expression: dx[1] == -0.4161468365471424
   Evaluated: 0.0 == -0.4161468365471424
Stacktrace:
 [1] macro expansion
   @ ~/work/Enzyme.jl/Enzyme.jl/test/runtests.jl:258 [inlined]
 [2] macro expansion
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Test/src/Test.jl:1151 [inlined]
 [3] top-level scope
   @ ~/work/Enzyme.jl/Enzyme.jl/test/runtests.jl:255
Test Summary: | Fail  Error  Total
Struct return |    1      1      2

vchuravy avatar Aug 19 '22 17:08 vchuravy