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

fix: fix view adjoints #383

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
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)...)

Check warning on line 32 in ext/RecursiveArrayToolsReverseDiffExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/RecursiveArrayToolsReverseDiffExt.jl#L27-L32

Added lines #L27 - L32 were not covered by tests
end
end
return view(A, I...), view_adjoint

Check warning on line 35 in ext/RecursiveArrayToolsReverseDiffExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/RecursiveArrayToolsReverseDiffExt.jl#L35

Added line #L35 was not covered by tests
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 @@
adj = let VA = VA
function Array_adjoint(y)
VA = recursivecopy(VA)
VA .= y
copyto!(VA, y)

Check warning on line 118 in ext/RecursiveArrayToolsZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/RecursiveArrayToolsZygoteExt.jl#L118

Added line #L118 was not covered by tests
return (VA,)
end
end
Expand All @@ -126,8 +126,8 @@
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)...)

Check warning on line 130 in ext/RecursiveArrayToolsZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/RecursiveArrayToolsZygoteExt.jl#L129-L130

Added lines #L129 - L130 were not covered by tests
end
end
return view(A, I...), view_adjoint
Expand All @@ -139,7 +139,7 @@
A = recursivecopy(A)
recursivefill!(A, zero(eltype(A)))
v = view(A, I...)
v .= y
copyto!(v, y)

Check warning on line 142 in ext/RecursiveArrayToolsZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/RecursiveArrayToolsZygoteExt.jl#L142

Added line #L142 was not covered by tests
return (A, map(_ -> nothing, I)...)
end
end
Expand Down
Loading