Skip to content
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
9 changes: 8 additions & 1 deletion src/destructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,20 @@ function _grad!(x, dx, off, flat::AbstractVector)
foreach((xᵢ, dxᵢ, oᵢ) -> _grad!(xᵢ, dxᵢ, oᵢ, flat), x′, dx′, off′)
flat
end
function _grad!(x, dx, off::Integer, flat::AbstractVector)
function _grad!(x::T, dx::T, off::Integer, flat::AbstractVector) where T
@views flat[off .+ (1:length(x))] .+= vec(dx) # must visit all tied nodes
flat
end
_grad!(x, dx::Zero, off, flat::AbstractVector) = dx
_grad!(x, dx::Zero, off::Integer, flat::AbstractVector) = dx # ambiguity

function _grad!(x::T, dx::S, off::Integer, flat::AbstractVector) where {T, S}
flat = similar(dx, length(flat))
@views flat[off .+ (1:length(x))] .+= vec(dx) # must visit all tied nodes
flat
end


# These are only needed for 2nd derivatives:
function ChainRulesCore.rrule(::typeof(_grad!), x, dx, off, flat)
@warn "second derivatives of Restructure may not work yet, sorry!" maxlog=3
Expand Down