EnzymeAD / Enzyme.jl

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

Forward mode gradient error with argument of limited mutability #1821

Open mhauru opened 2 weeks ago

mhauru commented 2 weeks ago
julia> f(x) = sum(x)
f (generic function with 1 method)

julia> x = Hermitian(randn(2,2))
2×2 Hermitian{Float64, Matrix{Float64}}:
  0.656689  -0.377647
 -0.377647  -0.505356

julia> Enzyme.gradient(Enzyme.Forward, Enzyme.Const(f), x)
ERROR: ArgumentError: Cannot set a non-diagonal index in a Hermitian matrix
Stacktrace:
 [1] setindex!
   @ ~/.julia/juliaup/julia-1.10.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/symmetric.jl:264 [inlined]
 [2] _setindex!
   @ ./abstractarray.jl:1431 [inlined]
 [3] setindex!
   @ ./abstractarray.jl:1396 [inlined]
 [4] #93
   @ ~/.julia/packages/Enzyme/TiboG/src/Enzyme.jl:967 [inlined]
 [5] macro expansion
   @ ./ntuple.jl:72 [inlined]
 [6] ntuple(f::Enzyme.var"#93#94"{Hermitian{Float64, Matrix{Float64}}, Int64}, ::Val{4})
   @ Base ./ntuple.jl:69
 [7] onehot
   @ ~/.julia/packages/Enzyme/TiboG/src/Enzyme.jl:963 [inlined]
 [8] gradient(::EnzymeCore.ForwardMode{…}, f::EnzymeCore.Const{…}, x::Hermitian{…})
   @ Enzyme ~/.julia/packages/Enzyme/TiboG/src/Enzyme.jl:1132
 [9] top-level scope
   @ REPL[60]:1
Some type information was truncated. Use `show(err)` to see complete types.

Types like Hermitian and Symmetric break the assumption made in onehot that all elements of the array are independent variables that can be setindex!ed freely. Not sure if there's some general solution to this, but maybe catering for stdlib types would be enough?

wsmoses commented 1 day ago

@ExpandingMan would you be interested in taking a stab at this?

ExpandingMan commented 1 day ago

There's a complicated but efficient solution in which onehot is smart enough only to take the independent basis elements. The real difficulty I see with that is that the output side of gradient would have to know about it to combine the results, which can potentially significantly increase its complexity.