EnzymeAD / EnzymeAD/Enzyme.jl

`Reverse` mode incorrectly detecting duplicated returns for a type unstable function

Open
#2,194 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
586
Forks
108
Avg merge
1d 5h
Merged PRs (30d)
44

Description

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:
```julia
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
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
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:
```julia
(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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.