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

Handle StaticArrays? #144

Closed
gdalle opened this issue Jul 31, 2024 · 5 comments
Closed

Handle StaticArrays? #144

gdalle opened this issue Jul 31, 2024 · 5 comments
Labels
array Features regarding array overloads new overloads A new method on tracers is required by a user.

Comments

@gdalle
Copy link
Collaborator

gdalle commented Jul 31, 2024

julia> using StaticArrays, SparseConnectivityTracer

julia> function f(x)
           y = SVector(x[1], x[2])
           return norm(y)
       end
f (generic function with 1 method)

julia> jacobian_sparsity(f, ones(2), TracerSparsityDetector())
ERROR: TypeError: non-boolean (SparseConnectivityTracer.GradientTracer{SparseConnectivityTracer.IndexSetGradientPattern{Int64, BitSet}}) used in boolean context
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/StaticArrays/MSJcA/src/linalg.jl:278 [inlined]
 [2] _norm
   @ ~/.julia/packages/StaticArrays/MSJcA/src/linalg.jl:266 [inlined]
 [3] norm
   @ ~/.julia/packages/StaticArrays/MSJcA/src/linalg.jl:265 [inlined]
 [4] f(x::Vector{SparseConnectivityTracer.GradientTracer{SparseConnectivityTracer.IndexSetGradientPattern{Int64, BitSet}}})
   @ Main ~/Work/GitHub/Julia/DifferentiationInterface.jl/DifferentiationInterface/test/playground.jl:56
 [5] trace_function(::Type{SparseConnectivityTracer.GradientTracer{…}}, f::typeof(f), x::Vector{Float64})
   @ SparseConnectivityTracer ~/.julia/packages/SparseConnectivityTracer/pVwOW/src/interface.jl:33
 [6] _jacobian_sparsity(f::Function, x::Vector{Float64}, ::Type{SparseConnectivityTracer.GradientTracer{…}})
   @ SparseConnectivityTracer ~/.julia/packages/SparseConnectivityTracer/pVwOW/src/interface.jl:59
 [7] jacobian_sparsity(f::Function, x::Vector{…}, ::TracerSparsityDetector{…})
   @ SparseConnectivityTracer ~/.julia/packages/SparseConnectivityTracer/pVwOW/src/adtypes.jl:49
 [8] top-level scope
   @ ~/Work/GitHub/Julia/DifferentiationInterface.jl/DifferentiationInterface/test/playground.jl:59
Some type information was truncated. Use `show(err)` to see complete types.

See JuliaSmoothOptimizers/ADNLPModels.jl#247 (comment)

@adrhill
Copy link
Owner

adrhill commented Aug 1, 2024

Your linked comment points out the issue:

function LinearAlgebra.norm(A::AbstractArray{T}, p::Real=2) where {T<:AbstractTracer}

We overload norm(A::AbstractArray{<:AbstractTracer}), which is less specific than StaticArrays' norm(A:: StaticArray) (source).

This is a general issue with array overloads since LinearAlgebra uses array-level types as traits for dispatch (e.g. Symmetric, Hermitian, ...). See also #133.

We could solve this problem by sticking our array overloads in a big loop over different array types.

@adrhill
Copy link
Owner

adrhill commented Aug 1, 2024

I guess we could start by supporting types form StaticArrays as well as common LinearAlgebra matrix types?

@gdalle
Copy link
Collaborator Author

gdalle commented Aug 1, 2024

Yeah but where does it stop?

@gdalle
Copy link
Collaborator Author

gdalle commented Aug 1, 2024

Also I don't want StaticArrays in the main dependencies so we might have to make the loop parametric and handle static types in a package extension.

@adrhill adrhill added array Features regarding array overloads new overloads A new method on tracers is required by a user. labels Aug 7, 2024
@adrhill
Copy link
Owner

adrhill commented Sep 6, 2024

Closing in favor of the more general discussion in #192.

@adrhill adrhill closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
array Features regarding array overloads new overloads A new method on tracers is required by a user.
Projects
None yet
Development

No branches or pull requests

2 participants