Skip to content

_project error #1078

@willtebbutt

Description

@willtebbutt

The problem occurs when one tries to take the Zygote.gradient of a function an AbstractArray with a structural tangent.

ProjectTo(my_array) produces a ProjectTo{AbstractArray}, and Zygote winds up producing a Tangent{Any} for it. This means that this method doesn't apply.

MWE:

julia> using ChainRules: Tangent, ProjectTo, NoTangent

julia> using KernelFunctions: MOInputIsotopicByFeatures

julia> x = MOInputIsotopicByFeatures(randn(3), 2);

julia> dx = Tangent{Any}(x=randn(3), out_dim=NoTangent());

julia> ProjectTo(x)(dx)
ERROR: MethodError: no method matching (::ProjectTo{AbstractArray, NamedTuple{(:elements, :axes), Tuple{Vector{typeof(identity)}, Tuple{Base.OneTo{Int64}}}}})(::Tangent{Any, NamedTuple{(:x, :out_dim), Tuple{Vector{Float64}, NoTangent}}})
Closest candidates are:
  (::ProjectTo{T, D} where D<:NamedTuple)(::Tangent{var"#s15", T} where {var"#s15"<:T, T}) where T at /Users/willtebbutt/.julia/packages/ChainRulesCore/ChM7X/src/projection.jl:139
  (::ProjectTo{AbstractArray, D} where D<:NamedTuple)(::Union{LinearAlgebra.Adjoint{T, var"#s832"}, LinearAlgebra.Transpose{T, var"#s832"}} where {T, var"#s832"<:(AbstractVector{T} where T)}) at /Users/willtebbutt/.julia/packages/ChainRulesCore/ChM7X/src/projection.jl:239
  (::ProjectTo{AbstractArray, D} where D<:NamedTuple)(::Number) at /Users/willtebbutt/.julia/packages/ChainRulesCore/ChM7X/src/projection.jl:245
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

What is the appropriate fix @mcabbott @oxinabox @mzgubic ? I can work around this by using pullback or _pullback directly, but it's obviously sub-optimal not to be able to use gradient.

Where's the discussion about adding the _project call here? I feel like I've seen it somewhere, but I'm not sure where...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions