forked from SciML/RecursiveArrayTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveArrayToolsReverseDiffExt.jl
More file actions
41 lines (36 loc) · 1.08 KB
/
RecursiveArrayToolsReverseDiffExt.jl
File metadata and controls
41 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module RecursiveArrayToolsReverseDiffExt
using RecursiveArrayTools
using ReverseDiff
using Zygote: @adjoint
using RecursiveArrayTools.ArrayInterface
function trackedarraycopyto!(dest, src)
for (i, slice) in zip(eachindex(dest.u), eachslice(src, dims = ndims(src)))
if dest.u[i] isa AbstractArray
dest.u[i] = reshape(ArrayInterface.aos_to_soa(slice), size(dest.u[i]))
elseif dest.u[i] isa Number
dest.u[i] = slice
end
end
return
end
@adjoint function Array(VA::AbstractVectorOfArray{<:ReverseDiff.TrackedReal})
function Array_adjoint(y)
VA = recursivecopy(VA)
trackedarraycopyto!(VA, y)
return (VA,)
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