Skip to content

Commit

Permalink
handle permutedims rrule with ZeroTangent (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengchingwen authored Dec 3, 2022
1 parent 1597bcc commit 024704e
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/rulesets/Base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,21 @@ end
function rrule(::typeof(permutedims), x::AbstractVector)
project = ProjectTo(x)
permutedims_pullback_1(dy) = (NoTangent(), project(permutedims(unthunk(dy))))
permutedims_pullback_1(::ZeroTangent) = (NoTangent(), ZeroTangent())
return permutedims(x), permutedims_pullback_1
end

function rrule(::typeof(permutedims), x::AbstractArray, perm)
pr = ProjectTo(x) # projection restores e.g. transpose([1,2,3])
permutedims_back_2(dy) = (NoTangent(), pr(permutedims(unthunk(dy), invperm(perm))), NoTangent())
permutedims_back_2(::ZeroTangent) = (NoTangent(), ZeroTangent(), NoTangent())
return permutedims(x, perm), permutedims_back_2
end

function rrule(::Type{<:PermutedDimsArray}, x::AbstractArray, perm)
pr = ProjectTo(x)
permutedims_back_3(dy) = (NoTangent(), pr(permutedims(unthunk(dy), invperm(perm))), NoTangent())
permutedims_back_3(::ZeroTangent) = (NoTangent(), ZeroTangent(), NoTangent())
return PermutedDimsArray(x, perm), permutedims_back_3
end

Expand Down

0 comments on commit 024704e

Please sign in to comment.