Skip to content

Commit

Permalink
Handle ChainRulesCore via Pkg extension (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
oschulz authored Jun 7, 2023
1 parent 50ace9c commit f522a8f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
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=VERSIONv"1.7")
end

include("linearmaps.jl")
Expand Down

2 comments on commit f522a8f

@dkarrasch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/85070

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.10.1 -m "<description of version>" f522a8f8b4f050f90639f8ab68344259ee392963
git push origin v3.10.1

Please sign in to comment.