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

logdet differentiation fails #1078

Open
mohamed82008 opened this issue Sep 24, 2023 · 3 comments
Open

logdet differentiation fails #1078

mohamed82008 opened this issue Sep 24, 2023 · 3 comments

Comments

@mohamed82008
Copy link

The following example doesn't work on latest Enzyme release and Julia 1.10.

using LinearAlgebra
import Enzyme

f(x) = logdet([exp(x[1]);;])
f(rand(1))
Enzyme.gradient(Enzyme.Forward, f, rand(1))

The part of the error that I understand is:

No forward mode derivative found for dgetrf_64_
 at context:   call void @dgetrf_64_(i8* noundef nonnull %6, i8* noundef nonnull %8, i64 %23, i8* noundef nonnull %10, i64 %27, i64 noundef %bitcast_coercion42) #40 [ "jl_roots"({} addrspace(10)* null, {} addrspace(10)* %20, {} addrspace(10)* null, {} addrspace(10)* %2, {} addrspace(10)* null, {} addrspace(10)* null) ], !dbg !78

Stacktrace:
 [1] #getrf!#1
   @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lapack.jl:565


Stacktrace:
  [1] throwerr(cstr::Cstring)
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/VS5jo/src/compiler.jl:3066
  [2] #getrf!#1
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lapack.jl:565
  [3] getrf!
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lapack.jl:557 [inlined]
  [4] #lu!#158
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lu.jl:82 [inlined]
  [5] lu!
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lu.jl:81 [inlined]
  [6] #lu#164
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lu.jl:300 [inlined]
  [7] lu (repeats 2 times)
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/lu.jl:299 [inlined]
  [8] logabsdet
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:1676 [inlined]
  [9] logdet
    @ ~/.julia/juliaup/julia-1.10.0-beta2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:1701
 [10] f
    @ ./REPL[7]:1 [inlined]
 [11] f
    @ ./REPL[7]:0 [inlined]
 [12] fwddiffejulia_f_2643_inner_1wrap
    @ ./REPL[7]:0
 [13] macro expansion
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/VS5jo/src/compiler.jl:9774 [inlined]
 [14] enzyme_call
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/VS5jo/src/compiler.jl:9452 [inlined]
 [15] ForwardModeThunk
    @ Enzyme.Compiler ~/.julia/packages/Enzyme/VS5jo/src/compiler.jl:9418 [inlined]
 [16] autodiff(::EnzymeCore.ForwardMode{…}, f::EnzymeCore.Const{…}, ::Type{…}, args::EnzymeCore.BatchDuplicated{…})
    @ Enzyme ~/.julia/packages/Enzyme/VS5jo/src/Enzyme.jl:330
 [17] autodiff
    @ Enzyme ~/.julia/packages/Enzyme/VS5jo/src/Enzyme.jl:222 [inlined]
 [18] gradient(::EnzymeCore.ForwardMode{EnzymeCore.FFIABI}, f::Function, x::Vector{Float64}; shadow::Tuple{Vector{Float64}})
    @ Enzyme ~/.julia/packages/Enzyme/VS5jo/src/Enzyme.jl:866
 [19] gradient(::EnzymeCore.ForwardMode{EnzymeCore.FFIABI}, f::Function, x::Vector{Float64})
    @ Enzyme ~/.julia/packages/Enzyme/VS5jo/src/Enzyme.jl:862
 [20] top-level scope
    @ REPL[13]:1
@wsmoses
Copy link
Member

wsmoses commented Sep 24, 2023

Yeah we don't have a derivative rule for that, want to try adding it?

@wsmoses
Copy link
Member

wsmoses commented Sep 25, 2023

Here's a bunch of samples if you'd like to take a look to get started: FluxML/NNlib.jl#536

I'd probably recommend writing the rule at the logdet function level

@mohamed82008
Copy link
Author

Looking at the documentation, defining Enzyme rules seems somewhat mechanical. I think it might be possible to create a macro that defines an Enzyme rule using ChainRulesCore.rrule or frule. We have a macro like this for ReverseDiff and ForwardDiff.

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

No branches or pull requests

2 participants