How can I make products with (constant) sparse boolean matrices and vectors work with Zygote? Example:
julia> using Zygote, SparseArrays, LinearAlgebra
julia> gradient(v -> dot(sparse([true;false]),v), [1.;0.])
ERROR: MethodError: no method matching zero(::Nothing)
Closest candidates are:
zero(::Union{Type{P}, P}) where P<:Dates.Period
@ Dates ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/Dates/src/periods.jl:51
zero(::AbstractIrrational)
@ Base irrationals.jl:151
zero(::FillArrays.Ones{T, N}) where {T, N}
@ FillArrays ~/.julia/packages/FillArrays/yjfkJ/src/FillArrays.jl:572
...
Stacktrace:
[1] iszero(x::Nothing)
@ Base ./number.jl:42
[2] _iszero(x::Nothing)
@ SparseArrays ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/SparseArrays/src/SparseArrays.jl:37
[3] _noshapecheck_map(::typeof(Zygote.wrap_chainrules_output), ::SparseVector{ChainRulesCore.NoTangent, Int64})
@ SparseArrays.HigherOrderFns ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/SparseArrays/src/higherorderfns.jl:181
[4] map
@ ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/SparseArrays/src/higherorderfns.jl:152 [inlined]
[5] wrap_chainrules_output
@ ~/.julia/packages/Zygote/SuKWp/src/compiler/chainrules.jl:127 [inlined]
[6] wrap_chainrules_output
@ ~/.julia/packages/Zygote/SuKWp/src/compiler/chainrules.jl:110 [inlined]
[7] map
@ ./tuple.jl:275 [inlined]
[8] wrap_chainrules_output
@ ~/.julia/packages/Zygote/SuKWp/src/compiler/chainrules.jl:111 [inlined]
[9] ZBack
@ ~/.julia/packages/Zygote/SuKWp/src/compiler/chainrules.jl:211 [inlined]
[10] Pullback
@ ./REPL[4]:1 [inlined]
[11] (::Zygote.var"#75#76"{Zygote.Pullback{Tuple{var"#9#10", Vector{Float64}}, Tuple{Zygote.ZBack{ChainRules.var"#dot_pullback#1947"{SparseVector{Bool, Int64}, Vector{Float64}, ChainRulesCore.ProjectTo{AbstractArray, NamedTuple{(:element, :axes), Tuple{ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}}, Tuple{Base.OneTo{Int64}}}}}, ChainRulesCore.ProjectTo{SparseVector, NamedTuple{(:element, :nzind, :axes), Tuple{ChainRulesCore.ProjectTo{ChainRulesCore.NoTangent, NamedTuple{(), Tuple{}}}, Vector{Int64}, Tuple{Base.OneTo{Int64}}}}}}}, Zygote.ZBack{ChainRules.var"#vcat_pullback#1415"{Tuple{ChainRulesCore.ProjectTo{ChainRulesCore.NoTangent, NamedTuple{(), Tuple{}}}, ChainRulesCore.ProjectTo{ChainRulesCore.NoTangent, NamedTuple{(), Tuple{}}}}, Tuple{Tuple{}, Tuple{}}, Val{1}}}, Zygote.Pullback{Tuple{typeof(sparse), Vector{Bool}}, Tuple{Zygote.Pullback{Tuple{typeof(sparsevec), Vector{Bool}}, Tuple{Zygote.ZBack{ChainRules.var"#sparse_pullback#2153"}}}}}}}})(Ī::Float64)
@ Zygote ~/.julia/packages/Zygote/SuKWp/src/compiler/interface.jl:45
[12] gradient(f::Function, args::Vector{Float64})
@ Zygote ~/.julia/packages/Zygote/SuKWp/src/compiler/interface.jl:97
[13] top-level scope
@ REPL[4]:1
I did not expect issues because this is just a constant used as a bit mask. The same product works if I change the matrix type to Int64
.