|
| 1 | +module RecursiveArrayToolsZygoteExt |
| 2 | + |
| 3 | +using RecursiveArrayTools |
| 4 | + |
| 5 | +if isdefined(Base, :get_extension) |
| 6 | + using Zygote |
| 7 | + using Zygote: FillArrays, ChainRulesCore, literal_getproperty, @adjoint |
| 8 | +else |
| 9 | + using ..Zygote |
| 10 | + using ..Zygote: FillArrays, ChainRulesCore, literal_getproperty, @adjoint |
| 11 | +end |
| 12 | + |
| 13 | +# Define a new species of projection operator for this type: |
| 14 | +ChainRulesCore.ProjectTo(x::VectorOfArray) = ChainRulesCore.ProjectTo{VectorOfArray}() |
| 15 | + |
| 16 | +function ChainRulesCore.rrule(T::Type{<:RecursiveArrayTools.GPUArraysCore.AbstractGPUArray}, |
| 17 | + xs::AbstractVectorOfArray) |
| 18 | + T(xs), ȳ -> (ChainRulesCore.NoTangent(), ȳ) |
| 19 | +end |
| 20 | + |
| 21 | +@adjoint function getindex(VA::AbstractVectorOfArray, i::Int) |
| 22 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 23 | + Δ′ = [(i == j ? Δ : Fill(zero(eltype(x)), size(x))) |
| 24 | + for (x, j) in zip(VA.u, 1:length(VA))] |
| 25 | + (VectorOfArray(Δ′), nothing) |
| 26 | + end |
| 27 | + VA[i], AbstractVectorOfArray_getindex_adjoint |
| 28 | +end |
| 29 | + |
| 30 | +@adjoint function getindex(VA::AbstractVectorOfArray, |
| 31 | + i::Union{BitArray, AbstractArray{Bool}}) |
| 32 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 33 | + Δ′ = [(i[j] ? Δ[j] : Fill(zero(eltype(x)), size(x))) |
| 34 | + for (x, j) in zip(VA.u, 1:length(VA))] |
| 35 | + (VectorOfArray(Δ′), nothing) |
| 36 | + end |
| 37 | + VA[i], AbstractVectorOfArray_getindex_adjoint |
| 38 | +end |
| 39 | + |
| 40 | +@adjoint function getindex(VA::AbstractVectorOfArray, i::AbstractArray{Int}) |
| 41 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 42 | + iter = 0 |
| 43 | + Δ′ = [(j ∈ i ? Δ[iter += 1] : Fill(zero(eltype(x)), size(x))) |
| 44 | + for (x, j) in zip(VA.u, 1:length(VA))] |
| 45 | + (VectorOfArray(Δ′), nothing) |
| 46 | + end |
| 47 | + VA[i], AbstractVectorOfArray_getindex_adjoint |
| 48 | +end |
| 49 | + |
| 50 | +@adjoint function getindex(VA::AbstractVectorOfArray, |
| 51 | + i::Union{Int, AbstractArray{Int}}) |
| 52 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 53 | + Δ′ = [(i[j] ? Δ[j] : Fill(zero(eltype(x)), size(x))) |
| 54 | + for (x, j) in zip(VA.u, 1:length(VA))] |
| 55 | + (VectorOfArray(Δ′), nothing) |
| 56 | + end |
| 57 | + VA[i], AbstractVectorOfArray_getindex_adjoint |
| 58 | +end |
| 59 | + |
| 60 | +@adjoint function getindex(VA::AbstractVectorOfArray, i::Colon) |
| 61 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 62 | + (VectorOfArray(Δ), nothing) |
| 63 | + end |
| 64 | + VA[i], AbstractVectorOfArray_getindex_adjoint |
| 65 | +end |
| 66 | + |
| 67 | +@adjoint function getindex(VA::AbstractVectorOfArray, i::Int, |
| 68 | + j::Union{Int, AbstractArray{Int}, CartesianIndex, |
| 69 | + Colon, BitArray, AbstractArray{Bool}}...) |
| 70 | + function AbstractVectorOfArray_getindex_adjoint(Δ) |
| 71 | + Δ′ = VectorOfArray([zero(x) for (x, j) in zip(VA.u, 1:length(VA))]) |
| 72 | + Δ′[i, j...] = Δ |
| 73 | + (Δ′, nothing, map(_ -> nothing, j)...) |
| 74 | + end |
| 75 | + VA[i, j...], AbstractVectorOfArray_getindex_adjoint |
| 76 | +end |
| 77 | + |
| 78 | +@adjoint function ArrayPartition(x::S, |
| 79 | + ::Type{Val{copy_x}} = Val{false}) where { |
| 80 | + S <: |
| 81 | + Tuple, |
| 82 | + copy_x |
| 83 | + } |
| 84 | + function ArrayPartition_adjoint(_y) |
| 85 | + y = Array(_y) |
| 86 | + starts = vcat(0, cumsum(reduce(vcat, length.(x)))) |
| 87 | + ntuple(i -> reshape(y[(starts[i] + 1):starts[i + 1]], size(x[i])), length(x)), |
| 88 | + nothing |
| 89 | + end |
| 90 | + |
| 91 | + ArrayPartition(x, Val{copy_x}), ArrayPartition_adjoint |
| 92 | +end |
| 93 | + |
| 94 | +@adjoint function VectorOfArray(u) |
| 95 | + VectorOfArray(u), |
| 96 | + y -> (VectorOfArray([y[ntuple(x -> Colon(), ndims(y) - 1)..., i] |
| 97 | + for i in 1:size(y)[end]]),) |
| 98 | +end |
| 99 | + |
| 100 | +@adjoint function DiffEqArray(u, t) |
| 101 | + DiffEqArray(u, t), |
| 102 | + y -> (DiffEqArray([y[ntuple(x -> Colon(), ndims(y) - 1)..., i] for i in 1:size(y)[end]], |
| 103 | + t), nothing) |
| 104 | +end |
| 105 | + |
| 106 | +@adjoint function literal_getproperty(A::ArrayPartition, ::Val{:x}) |
| 107 | + function literal_ArrayPartition_x_adjoint(d) |
| 108 | + (ArrayPartition((isnothing(d[i]) ? zero(A.x[i]) : d[i] for i in 1:length(d))...),) |
| 109 | + end |
| 110 | + A.x, literal_ArrayPartition_x_adjoint |
| 111 | +end |
| 112 | + |
| 113 | +end |
0 commit comments