Skip to content

fix: fix view adjoints #383

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

Merged
merged 1 commit into from
May 31, 2024
Merged
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
12 changes: 12 additions & 0 deletions ext/RecursiveArrayToolsReverseDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ end
end
return Array(VA), Array_adjoint
end

@adjoint function Base.view(A::AbstractVectorOfArray{<:ReverseDiff.TrackedReal, N}, I::Colon...) where {N}
view_adjoint = let A = A, I = I
function (y)
A = recursivecopy(A)
trackedarraycopyto!(A, y)
(A, map(_ -> nothing, I)...)
end
end
return view(A, I...), view_adjoint
end

end # module
8 changes: 4 additions & 4 deletions ext/RecursiveArrayToolsZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ end
adj = let VA = VA
function Array_adjoint(y)
VA = recursivecopy(VA)
VA .= y
copyto!(VA, y)
return (VA,)
end
end
Expand All @@ -126,8 +126,8 @@ end
view_adjoint = let A = A, I = I
function (y)
A = recursivecopy(A)
A .= y
(A, map(_ -> nothing, I)...)
copyto!(A, y)
return (A, map(_ -> nothing, I)...)
end
end
return view(A, I...), view_adjoint
Expand All @@ -139,7 +139,7 @@ end
A = recursivecopy(A)
recursivefill!(A, zero(eltype(A)))
v = view(A, I...)
v .= y
copyto!(v, y)
return (A, map(_ -> nothing, I)...)
end
end
Expand Down