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

Port rules for permutedims #523

Closed
mzgubic opened this issue Sep 3, 2021 · 0 comments · Fixed by #559
Closed

Port rules for permutedims #523

mzgubic opened this issue Sep 3, 2021 · 0 comments · Fixed by #559
Labels

Comments

@mzgubic
Copy link
Member

mzgubic commented Sep 3, 2021

We only have the following:

./src/rulesets/Base/nondiff.jl:@non_differentiable permutedims(::AbstractArray{Bool})
./src/rulesets/Base/nondiff.jl:@non_differentiable permutedims(::AbstractArray{Bool}, ::Any)

but we could port the generic permutedims rules from Zygote
https://github.com/FluxML/Zygote.jl/blob/05d0c2ae04f334a2ec61e42decfe1172d0f2e6e8/src/lib/array.jl#L104-L109


I ran into this when trying out

julia> gradient(x -> sum(permutedims(x, (:a, :b))), NamedDimsArray{(:a, :b)}(rand(2, 3)))
ERROR: ArgumentError: argument is not a permutation
Stacktrace:
  [1] (::Base.var"#744#746"{Tuple{Symbol, Symbol}, Val{2}})(i::Int64)
    @ Base ./combinatorics.jl:284
  [2] ntuple
    @ ./ntuple.jl:49 [inlined]
  [3] invperm(P::Tuple{Symbol, Symbol})
    @ Base ./combinatorics.jl:278
  [4] macro expansion
    @ ./show.jl:955 [inlined]
  [5] (::Zygote.var"#pb#1963"{Tuple{Symbol, Symbol}})(Δ::FillArrays.Fill{Float64, 2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}})
    @ Zygote ~/JuliaEnvs/PortfolioNets.jl/dev/Zygote/src/lib/array.jl:113
  [6] (::Zygote.var"#2561#back#1964"{Zygote.var"#pb#1963"{Tuple{Symbol, Symbol}}})(Δ::FillArrays.Fill{Float64, 2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}})
    @ Zygote ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
  [7] Pullback
    @ ./REPL[62]:1 [inlined]
  [8] (::typeof((#35)))(Δ::Float64)
    @ Zygote ~/JuliaEnvs/PortfolioNets.jl/dev/Zygote/src/compiler/interface2.jl:0
  [9] (::Zygote.var"#50#51"{typeof((#35))})(Δ::Float64)
    @ Zygote ~/JuliaEnvs/PortfolioNets.jl/dev/Zygote/src/compiler/interface.jl:41
 [10] gradient(f::Function, args::NamedDimsArray{(:a, :b), Float64, 2, Matrix{Float64}})
    @ Zygote ~/JuliaEnvs/PortfolioNets.jl/dev/Zygote/src/compiler/interface.jl:76
 [11] top-level scope
    @ REPL[62]:1

which is a result of invperm((:a, :b)) call inside an @adjoint for permutedims. Fixed it by defining a more specific adjoint, but would be nice to be able to write chainrules (which would mean that permutedims adjoint gets removed from Zygote, since that takes precedence)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant