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

Forward mode gradient error with argument of limited mutability #1821

Open
mhauru opened this issue Sep 13, 2024 · 2 comments
Open

Forward mode gradient error with argument of limited mutability #1821

mhauru opened this issue Sep 13, 2024 · 2 comments

Comments

@mhauru
Copy link
Contributor

mhauru commented Sep 13, 2024

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
Copy link
Member

wsmoses commented Sep 28, 2024

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

@ExpandingMan
Copy link
Contributor

ExpandingMan commented Sep 28, 2024

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.

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

3 participants