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

Errors from AbstractSparseMatrix{Bool} #1428

Open
mcabbott opened this issue May 29, 2023 · 0 comments
Open

Errors from AbstractSparseMatrix{Bool} #1428

mcabbott opened this issue May 29, 2023 · 0 comments
Labels
bug Something isn't working ChainRules adjoint -> rrule, and further integration

Comments

@mcabbott
Copy link
Member

As reported here, these should agree:

julia> using SparseArrays, Zygote

julia> gradient(x -> dot([1,2.0], x, [3,4.0]), diagm([true, false]))
(nothing,)

julia> gradient(x -> dot([1,2.0], x, [3,4.0]), spdiagm([true, false]))
ERROR: MethodError: no method matching zero(::Nothing)
Stacktrace:
  [1] iszero(x::Nothing)
    @ Base ./number.jl:42
...
  [4] map(::typeof(Zygote.wrap_chainrules_output), ::SparseMatrixCSC{ChainRulesCore.NoTangent, Int64})
    @ SparseArrays.HigherOrderFns ~/.julia/dev/julia/usr/share/julia/stdlib/v1.10/SparseArrays/src/higherorderfns.jl:1187 [inlined]
  [5] wrap_chainrules_output(dxs::SparseMatrixCSC{ChainRulesCore.NoTangent, Int64})
    @ Zygote ~/.julia/packages/Zygote/HTsWj/src/compiler/chainrules.jl:127 [inlined]
  [6] wrap_chainrules_output(x::ChainRulesCore.Thunk{ChainRules.var"#1950#1954"{…}})
    @ Zygote ~/.julia/packages/Zygote/HTsWj/src/compiler/chainrules.jl:110 [inlined]
...

Could be fixed at many levels:

  • wrap_chainrules_output(::AbstractArray{NoTangent) should give nothing, not call map.
  • In rule for dot here, ProjectTo should not make an array of eltype NoTangent.
julia> using ChainRulesCore

julia> ProjectTo(diagm([true, false]))
ProjectTo{NoTangent}()

julia> ProjectTo(spdiagm([true, false]))
ProjectTo{SparseMatrixCSC}(element = ProjectTo{NoTangent}(), axes = (Base.OneTo(2), Base.OneTo(2)), rowval = [1, 2], nzranges = UnitRange{Int64}[1:1, 2:2], colptr = [1, 2, 3])
  • In fact, the rule should not compute anything before discarding it. Perhaps it should call ProjectTo{NoTangent}(::Thunk) to discard the thunk immediately.
@mcabbott mcabbott added bug Something isn't working ChainRules adjoint -> rrule, and further integration labels May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ChainRules adjoint -> rrule, and further integration
Projects
None yet
Development

No branches or pull requests

1 participant