Skip to content

Handle ChainRulesCore via Pkg extension #206

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

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name = "LinearMaps"
uuid = "7a12625a-238d-50fd-b39a-03d52299707e"
version = "3.10.0"
version = "3.10.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

[extensions]
LinearMapsChainRulesCoreExt = "ChainRulesCore"

[compat]
ChainRulesCore = "1"
julia = "1.6"
10 changes: 10 additions & 0 deletions src/chainrules.jl → ext/LinearMapsChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module LinearMapsChainRulesCoreExt

using ChainRulesCore: unthunk, NoTangent, @thunk, @not_implemented
import ChainRulesCore: rrule

using LinearMaps

function rrule(::typeof(*), A::LinearMap, x::AbstractVector)
y = A*x
function pullback(dy)
Expand All @@ -15,3 +22,6 @@ function rrule(A::LinearMap, x::AbstractVector)
end
return y, pullback
end


end # module ChainRulesCore
8 changes: 4 additions & 4 deletions src/LinearMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ using SparseArrays

import Statistics: mean

using ChainRulesCore: unthunk, NoTangent, @thunk, @not_implemented
import ChainRulesCore: rrule

using Base: require_one_based_indexing

abstract type LinearMap{T} end
Expand Down Expand Up @@ -354,7 +351,6 @@ include("conversion.jl") # conversion of linear maps to matrices
include("show.jl") # show methods for LinearMap objects
include("getindex.jl") # getindex functionality
include("inversemap.jl")
include("chainrules.jl") # AD rules through ChainRulesCore

"""
LinearMap(A::LinearMap; kwargs...)::WrappedMap
Expand Down Expand Up @@ -424,4 +420,8 @@ LinearMap(A::MapOrVecOrMat, dims::Dims{2}; offset::Dims{2}) =
LinearMap{T}(A::MapOrVecOrMat; kwargs...) where {T} = WrappedMap{T}(A; kwargs...)
LinearMap{T}(f, args...; kwargs...) where {T} = FunctionMap{T}(f, args...; kwargs...)

@static if !isdefined(Base, :get_extension)
include("../ext/LinearMapsChainRulesCoreExt.jl")
end

end # module
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Test, LinearMaps, Aqua

@testset "code quality" begin
Aqua.test_all(LinearMaps)
Aqua.test_all(LinearMaps, project_toml_formatting=VERSION≥v"1.7")
end

include("linearmaps.jl")
Expand Down