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

inelegant solution to #1821 #1934

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,31 @@ end
return (one(x),)
end

function _is_symmed_index(a::CartesianIndex, b::CartesianIndex)
(a[1] == b[1] && a[2] == b[2]) || (a[1] == b[2] && a[2] == b[1])
end

@inline function _herm_sym_onehot(type, x::AbstractMatrix, start=1, endl=length(x))
idxs = CartesianIndices(x)
ntuple(Val(endl - start + 1)) do i0
Base.@_inline_meta
i = start + i0 - 1
idx = idxs[i]
res = similar(parent(x))
for idx2 ∈ CartesianIndices(x)
@inbounds res[idx2] = _is_symmed_index(idx, idx2) ? 1 : 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the check if symmed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking if it's either the index idx or its transpose. In my testing enzyme would return the wrong result if I did not set both elements (as if I had not wrapped it in Symmetric even though I had). I don't understand why that is.

end
type(res)
end
end

@inline onehot(x::Hermitian) = _herm_sym_onehot(Hermitian, x)
@inline onehot(x::Symmetric) = _herm_sym_onehot(Symmetric, x)

@inline onehot(x::Hermitian, start, endl) = _herm_sym_onehot(Hermitian, x, start, endl)
@inline onehot(x::Symmetric, start, endl) = _herm_sym_onehot(Symmetric, x, start, endl)


"""
gradient(::ReverseMode, f, args...)

Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3191,6 +3191,23 @@ mkarray(sz, args...) = reshape(vcat(args...), sz)
@test_broken Enzyme.gradient(Enzyme.Reverse, x -> OutStruct(x.i1 * x.i2, cos(x.i3) + x.i1, exp(x.i2)), istruct)[1]
@test_broken Enzyme.jacobian(Enzyme.Forward, x -> OutStruct(x.i1 * x.i2, cos(x.i3) + x.i1, exp(x.i2)), istruct)[1]
@test_broken Enzyme.jacobian(Enzyme.Reverse, x -> OutStruct(x.i1 * x.i2, cos(x.i3) + x.i1, exp(x.i2)), istruct)[1]

f0 = x -> x[1,1]^2 + 2*x[1,2]^2 + 3*x[2,1]^2 - x[2,2]^2
f1 = x -> [x[1,1]^2, 2*x[1,2]^2 + 3*x[2,1]^2, x[1,2]^2 + x[2,1]^2, x[2,2]^2]

for x ∈ (Float64[1 2; 0 3], Float64[1 2; 2 3]) # both are [1 2; 2 3]
for T ∈ (Hermitian, Symmetric)
x = T(x)
df = Enzyme.gradient(Enzyme.Forward, f0, x)[1]
@test df ≈ Float64[2 20; 20 -6]

df = Enzyme.gradient(Enzyme.Forward, f1, x)[1]
@test df[:,1,1] ≈ [2,0,0,0]
@test df[:,1,2] ≈ [0,20,8,0]
@test df[:,2,1] ≈ [0,20,8,0]
@test df[:,2,2] ≈ [0,0,0,6]
end
end
end

@testset "Simple Jacobian" begin
Expand Down
Loading