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

adjoints for cumsum and cumprod #294

Closed
Closed
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
117 changes: 94 additions & 23 deletions src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ using Base.Broadcast: broadcasted, broadcast_shape

@adjoint (::Type{T})(::UndefInitializer, args...) where T<:Array = T(undef, args...), Δ -> nothing

@adjoint Array(xs::AbstractArray) = Array(xs), -> (,)
@adjoint Array(xs::AbstractArray) = Array(xs), ȳ -> (ȳ,)

@nograd size, length, eachindex, Colon(), findfirst, randn, ones, zeros, one, zero,
print, println, any, all


@adjoint Base.vect(xs...) = Base.vect(xs...), Δ -> (Δ...,)

@adjoint copy(x::AbstractArray) = copy(x), -> (,)
@adjoint copy(x::AbstractArray) = copy(x), ȳ -> (ȳ,)

# Array Constructors
@adjoint (::Type{T})(x::T) where T<:Array = T(x), -> (,)
@adjoint (::Type{T})(x::T) where T<:Array = T(x), ȳ -> (ȳ,)
@adjoint (::Type{T})(x::Number, sz) where {T <: Fill} = Fill(x, sz), Δ -> (sum(Δ), nothing)
@adjoint (::Type{T})(sz) where {T<:Zeros} = Zeros(sz), Δ->(nothing,)
@adjoint (::Type{T})(sz) where {T<:Ones} = Ones(sz), Δ->(nothing,)
Expand Down Expand Up @@ -63,7 +63,7 @@ end
Δ -> (reshape(Δ, size(xs)),map(_->nothing,dims)...)

@adjoint function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number
hvcat(rows, xs...), -> (nothing, permutedims()...)
hvcat(rows, xs...), ȳ -> (nothing, permutedims(ȳ)...)
end

pull_block_vert(sz, Δ, A::AbstractVector) = Δ[sz-length(A)+1:sz]
Expand Down Expand Up @@ -125,8 +125,8 @@ end

function _forward(cx::AContext, ::typeof(collect), g::Base.Generator)
y, back = ∇map(cx, g.f, g.iter)
y, function ()
f̄, x̄ = back()
y, function (ȳ)
f̄, x̄ = back(ȳ)
(nothing, (f = f̄, iter = x̄),)
end
end
Expand All @@ -145,7 +145,7 @@ end

function _forward(cx::AContext, ::typeof(sum), f, xs::AbstractArray)
y, back = forward(cx, (xs -> sum(f.(xs))), xs)
y, -> (nothing, nothing, back()...)
y, ȳ -> (nothing, nothing, back(ȳ)...)
end

@adjoint function sum(::typeof(abs2), X::AbstractArray; dims = :)
Expand All @@ -163,7 +163,7 @@ end

function _forward(cx::AContext, ::typeof(prod), f, xs::AbstractArray)
y, back = forward(cx, (xs -> prod(f.(xs))), xs)
y, -> (nothing, nothing, back()...)
y, ȳ -> (nothing, nothing, back(ȳ)...)
end

@adjoint function maximum(xs; dims = :)
Expand Down Expand Up @@ -224,7 +224,7 @@ end
return x', back
end

@adjoint parent(x::LinearAlgebra.Adjoint) = parent(x), -> (LinearAlgebra.Adjoint(),)
@adjoint parent(x::LinearAlgebra.Adjoint) = parent(x), ȳ -> (LinearAlgebra.Adjoint(ȳ),)

@adjoint dot(x::AbstractArray, y::AbstractArray) = dot(x, y), Δ->(Δ .* y, Δ .* x)

Expand Down Expand Up @@ -278,17 +278,17 @@ end

@adjoint function \(A::Union{Diagonal, AbstractTriangular}, B::AbstractVecOrMat)
Y = A \ B
return Y, function()
B̄ = A' \
return Y, function(Ȳ)
B̄ = A' \ Ȳ
return (-B̄ * Y', B̄)
end
end

@adjoint function /(A::AbstractMatrix, B::Union{Diagonal, AbstractTriangular})
Y = A / B
return Y, function()
= / B'
return (, -Y' * )
return Y, function(Ȳ)
Ā = Ȳ / B'
return (Ā, -Y' * Ā)
end
end

Expand Down Expand Up @@ -321,9 +321,9 @@ end
# This is basically a hack while we don't have a working `ldiv!`.
@adjoint function \(A::Cholesky, B::AbstractVecOrMat)
Y, back = Zygote.forward((U, B)->U \ (U' \ B), A.U, B)
return Y, function()
Ā_factors, B̄ = back()
return ((uplo=nothing, status=nothing, factors=Ā_factors), B̄)
return Y, function(Ȳ)
Ā_factors, B̄ = back(Ȳ)
return ((uplo=nothing, status=nothing, factors=Ā_factors), B̄)
end
end

Expand All @@ -349,8 +349,8 @@ end
@adjoint function cholesky(Σ::Union{StridedMatrix, Symmetric{<:Real, <:StridedMatrix}})
C = cholesky(Σ)
return C, function(Δ::NamedTuple)
U, = C.U, Δ.factors
Σ̄ = * U'
U, Ū = C.U, Δ.factors
Σ̄ = Ū * U'
Σ̄ = copytri!(Σ̄, 'U')
Σ̄ = ldiv!(U, Σ̄)
BLAS.trsm!('R', 'U', 'T', 'N', one(eltype(Σ)), U.data, Σ̄)
Expand All @@ -365,8 +365,8 @@ end
X = lyap(A, C)
return X, function (X̄)
C̄ = lyap(collect(A'), X̄)
= C̄*X' + C̄'*X
return (, C̄)
Ā = C̄*X' + C̄'*X
return (Ā, C̄)
end
end

Expand All @@ -380,8 +380,8 @@ end
X = [i==j ? ew[i] : (ew[i]-ew[j])/(w[i]-w[j]) for i in 1:n,j=1:n]
VT = transpose(E.vectors)
VTF = factorize(collect(VT))
= real.(VTF\(VT*F̄/VTF.*X)*VT)
(, )
Ā = real.(VTF\(VT*F̄/VTF.*X)*VT)
(Ā, )
end

Zygote.@adjoint function LinearAlgebra.tr(x::AbstractMatrix)
Expand Down Expand Up @@ -488,3 +488,74 @@ end
back(Δ::AbstractArray) = (nothing, getindex.(_back.(Δ), 1))
return Fill(y, size(r)), back
end


# Scan

reversesumscan_(x::AbstractArray; dims::Integer) =
reverse(cumsum(reverse(x, dims=dims), dims=dims), dims=dims)


@adjoint function cumsum(A::AbstractArray, dims::Integer)
return cumsum(A, dims), function(∆)
(reversesumscan_(∆, dims=dims), nothing)
end
end

@adjoint function cumprod(A::AbstractArray; dims::Integer)
return cumprod(A; dims=dims), function (∆)
dim_size = size(A, dims)
if dim_size == 1
return (∆, nothing)
end
ndims = length(size(A))

# Simple case with nonzero elements in the input
if all(A .!= 0)
output = cumprod(A, dims=dims)
return (reversesumscan_(∆ .* output, dims=dims) ./ A, nothing)
end

grad_input = similar(A)

pre_idx = Any[Colon() for _=1:dims-1]
post_idx = [Colon() for _=dims+1:ndims]
for k=1:dim_size

if k == 1
idx_kplus1 = vcat(pre_idx, [k+1:dim_size], post_idx)
prods_from_k_plus_1 = cumprod(A[idx_kplus1...], dims=dims)

# We add 1s to the omitted_products of the first element
ones_size = [size(A)...]
ones_size[dims] = 1
ones_ = ones(eltype(A), ones_size...)

omitted_products = cat(ones_, prods_from_k_plus_1, dims=dims)

elseif k == dim_size
idx_kminus1 = vcat(pre_idx, [1:k-1], post_idx)
prods_until_k = prod(A[idx_kminus1...], dims=dims)
omitted_products = prods_until_k

else
idx_kplus1 = vcat(pre_idx, [k+1:dim_size], post_idx)
prods_from_k_plus_1 = cumprod(A[idx_kplus1...], dims=dims)

idx_kminus1 = vcat(pre_idx, [1:k-1], post_idx)
prods_until_k = prod(A[idx_kminus1...], dims=dims)

omitted_products = prods_until_k .* prods_from_k_plus_1
omitted_products = cat(prods_until_k, omitted_products, dims=dims)
end

@assert size(omitted_products, dims) == dim_size - k + 1 "$(size(omitted_products, dims)) != $(dim_size - k - 1)"

idx_ktodimsize = vcat(pre_idx, [k:dim_size], post_idx)
idx_k = vcat(pre_idx, [k:k], post_idx)

grad_input[idx_k...] .= sum(∆[idx_ktodimsize...] .* omitted_products, dims=dims)
end
return (grad_input, nothing)
end
end