Repository navigation
Bug: axpy!/axpby! fail with MissingPrimalError (iszero check) #317
Copy link
Copy link
Open
Labels
agent 🤖Agentically generated issueAgentically generated issuearrayFeatures regarding array overloadsFeatures regarding array overloadsnew overloadsA new method on tracers is required by a user.A new method on tracers is required by a user.
Description
Description
LinearAlgebra.axpy! and LinearAlgebra.axpby! fail with TracerSparsityDetector (global sparsity) when the scalar coefficient is a tracer. This is because the generic implementation checks iszero(α) to short-circuit when the coefficient is zero.
MWE
using SparseConnectivityTracer
using LinearAlgebra
detector = TracerSparsityDetector()
f(x) = begin
y = copy(x[1:3])
axpy!(x[4], x[5:7], y) # y = y + x[4] * x[5:7]
y
end
jacobian_sparsity(f, rand(7), detector)
# ERROR: Function iszero requires primal value(s).Stacktrace
Function iszero requires primal value(s).
A dual-number tracer for local sparsity detection can be used via `TracerLocalSparsityDetector`.
Stacktrace:
[1] iszero(t::SparseConnectivityTracer.GradientTracer{Int64, BitSet})
@ SparseConnectivityTracer ~/dev/SparseConnectivityTracer.jl/src/overloads/is_functions.jl:13
[2] axpy!(α::SparseConnectivityTracer.GradientTracer{Int64, BitSet}, x::Vector{SparseConnectivityTracer.GradientTracer{Int64, BitSet}}, y::Vector{SparseConnectivityTracer.GradientTracer{Int64, BitSet}})
@ LinearAlgebra /nix/store/.../LinearAlgebra/src/generic.jl:1636
Root Cause
LinearAlgebra.axpy! in generic.jl calls iszero(α) on the scalar coefficient to optimize the case when α=0. This fails with global tracers that don't have primal values.
The same issue affects axpby! which also checks iszero.
Workaround
Use TracerLocalSparsityDetector instead:
local_detector = TracerLocalSparsityDetector()
jacobian_sparsity(f, rand(7), local_detector) # Works!Potential Fix
Add overloads for axpy! and axpby! that work with tracers, avoiding the iszero check. The overload could simply perform the operation unconditionally since the sparsity pattern shouldn't depend on whether the coefficient happens to be zero.
Environment
julia> using Pkg; Pkg.status()
Project SparseConnectivityTracer v1.2.1
Status `~/dev/SparseConnectivityTracer.jl/Project.toml`
[47edcb42] ADTypes v1.22.0
[37e2e46d] LinearAlgebra v1.12.0
...
julia> versioninfo()
Julia Version 1.12.5
Commit 5fe89b8ddc1 (2026-02-09 16:05 UTC)
Platform Info:
OS: Linux (x86_64-linux-gnu)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
agent 🤖Agentically generated issueAgentically generated issuearrayFeatures regarding array overloadsFeatures regarding array overloadsnew overloadsA new method on tracers is required by a user.A new method on tracers is required by a user.
Activity
adrhill commentedon May 18, 2026
For context:
webbrain-one commentedon Aug 2, 2026
Thanks for the detailed report! I’ve opened a PR to address this: #326. It adds custom overloads for
axpy!andaxpby!that skip theiszero(α)short-circuit, allowing them to work smoothly with global sparsity tracers. Please take a look and let me know if it covers your use case or if you spot any edge cases. Thanks again!