Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse mode incorrectly detecting duplicated returns for a type unstable function #2194

Open
MasonProtter opened this issue Dec 11, 2024 · 2 comments

Comments

@MasonProtter
Copy link

MasonProtter commented Dec 11, 2024

Whatever Enzyme is doing to detect if there are duplicated returns appears to be confused by type instabilities. Not sure if this is intentional or not, but here is a MWE:

using Enzyme

f_unstable = asin
f_stable(x) = asin(x)

function foo_unstable(x)
    y = sin(x)
    z = f_unstable(y) # untyped global variable
    cos(z)
end;

function foo_stable(x)
    y = sin(x)
    z = f_stable(y) # global const
    cos(z)
end;
julia> autodiff(Reverse, foo_stable, Active(1.0))
((-0.8414709848078965,),)

julia> autodiff(Reverse, foo_unstable, Active(1.0))
ERROR: Duplicated Returns not yet handled
Stacktrace:
 [1] autodiff
   @ ~/.julia/packages/Enzyme/haqjK/src/Enzyme.jl:409 [inlined]
 [2] autodiff
   @ ~/.julia/packages/Enzyme/haqjK/src/Enzyme.jl:544 [inlined]
 [3] autodiff(mode::ReverseMode{false, false, FFIABI, false, false}, f::typeof(foo_unstable), args::Active{Float64})
   @ Enzyme ~/.julia/packages/Enzyme/haqjK/src/Enzyme.jl:516
 [4] top-level scope
   @ REPL[29]:1

Note that Enzyme can differentiate this function even with the instability:

julia> let x = 1.0, ydy = Duplicated(Ref(0.0), Ref(0.0))
           autodiff(Reverse, ydy, Active(x)) do y, x
               y[] = foo_unstable(x[])
               y[] 
           end
       end
((nothing, -0.8414709848078965),)

system info:

(jl_GwHTSw) pkg> st
Status `/tmp/jl_GwHTSw/Project.toml`
  [7da242da] Enzyme v0.13.22

julia> versioninfo()
Julia Version 1.11.2
Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 7 7840U w/ Radeon  780M Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver4)
Threads: 8 default, 0 interactive, 4 GC (on 16 virtual cores)
Environment:
  JULIA_NUM_THREADS = 8
  JULIA_EDITOR = emacsclient
@MasonProtter
Copy link
Author

MasonProtter commented Dec 12, 2024

Okay, so update based on what I learned from @wsmoses's comment in #2195, this is only a problem if I don't assert the returned activity:

julia> autodiff(Reverse, foo_unstable, Active, Active(1.0))
((-0.8414709848078965,),)

Maybe we should at least give a hint in the error message as to what's going on here. E.g. something like

The function being differentiated has an inferred return type of Any, but autodiff(Reverse currently only handles functions which return isbits subtypes of Real.

@wsmoses
Copy link
Member

wsmoses commented Dec 13, 2024

Yeah for sure! The relevant part of the code is here:

throw(ErrorException("Duplicated Returns not yet handled"))
(and we can even see in rt what the deduced return type was)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants