EnzymeAD / Enzyme.jl

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

Make using runtime activity unnecessary for complex numbers #1594

Closed mhauru closed 3 months ago

mhauru commented 3 months ago

As discussed with @wsmoses.

  Expression: ≈(collect(Enzyme.gradient(Enzyme.Forward, f, x)), finitediff, rtol = rtol, atol = atol)
  Enzyme execution failed.
  Mismatched activity for:   ret [2 x double] [double 0.000000e+00, double 1.000000e+00], !dbg !19 const val: [2 x double] [double 0.000000e+00, double 1.000000e+00]
  Type tree: {}
   llvalue=double 0.000000e+00
  You may be using a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#Activity-of-temporary-storage). If not, please open an issue, and either rewrite this variable to not be conditionally active or use Enzyme.API.runtimeActivity!(true) as a workaround for now
mhauru commented 3 months ago

Fixed by JLL bump

mhauru commented 3 months ago

MWE that used to provoke this:

module MWE

using FiniteDifferences
using Enzyme

#Enzyme.API.runtimeActivity!(true)

struct G{T}
    this_field_does_nothing::T
    b::T
end

G() = G{Float64}(0.0, 1.0)

function f(vec)
    x = vec[1]
    empty = []
    d = G(empty...)
    return x ≤ d.b ? x * d.b : zero(x)
end

vec = [0.5]

finitediff = FiniteDifferences.grad(central_fdm(5, 1), f, vec)[1]
reverse_grad = Enzyme.gradient(Enzyme.Reverse, f, vec)
forward_grad = Enzyme.gradient(Enzyme.Forward, f, vec)
@show finitediff
@show reverse_grad
@show forward_grad

end