-
Notifications
You must be signed in to change notification settings - Fork 79
refactor: move SparseArrays into an extension #2585
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
Open
avik-pal
wants to merge
1
commit into
main
Choose a base branch
from
ap/sparse_arrays_as_ext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−173
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ uuid = "7da242da-08ed-463a-9acd-ee780be4f1d9" | |
| authors = ["William Moses <[email protected]>", "Valentin Churavy <[email protected]>"] | ||
| version = "0.13.75" | ||
|
|
||
|
|
||
| [deps] | ||
| CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82" | ||
| EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" | ||
|
|
@@ -18,13 +17,13 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" | |
| Preferences = "21216c6a-2e73-6563-6e65-726566657250" | ||
| Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
| Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
|
|
||
| [weakdeps] | ||
| BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" | ||
| ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
| GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" | ||
| LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688" | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
| SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" | ||
| StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
|
|
||
|
|
@@ -33,6 +32,7 @@ EnzymeBFloat16sExt = "BFloat16s" | |
| EnzymeChainRulesCoreExt = "ChainRulesCore" | ||
| EnzymeGPUArraysCoreExt = "GPUArraysCore" | ||
| EnzymeLogExpFunctionsExt = "LogExpFunctions" | ||
| EnzymeSparseArraysExt = "SparseArrays" | ||
| EnzymeSpecialFunctionsExt = "SpecialFunctions" | ||
| EnzymeStaticArraysExt = "StaticArrays" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| module EnzymeSparseArraysExt | ||
|
|
||
| using LinearAlgebra: LinearAlgebra | ||
| using SparseArrays: SparseArrays | ||
| using Enzyme | ||
| using EnzymeCore: EnzymeRules | ||
|
|
||
| @inline Enzyme.Compiler.ptreltype(::Type{SparseArrays.CHOLMOD.Dense{T}}) where {T} = T | ||
| @inline Enzyme.Compiler.is_arrayorvararg_ty(::Type{SparseArrays.CHOLMOD.Dense{T}}) where {T} = true | ||
|
|
||
| Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_dense_struct}) = true | ||
| Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_sparse_struct}) = true | ||
| Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_factor_struct}) = true | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfig, | ||
| func::Const{typeof(LinearAlgebra.mul!)}, | ||
| ::Type{RT}, | ||
| C::Annotation{<:StridedVecOrMat}, | ||
| A::Annotation{<:SparseArrays.SparseMatrixCSCUnion}, | ||
| B::Annotation{<:StridedVecOrMat}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number} | ||
| ) where {RT} | ||
|
|
||
| cache_C = !(isa(β, Const)) ? copy(C.val) : nothing | ||
| # Always need to do forward pass otherwise primal may not be correct | ||
| func.val(C.val, A.val, B.val, α.val, β.val) | ||
|
|
||
| primal = if EnzymeRules.needs_primal(config) | ||
| C.val | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
| shadow = if EnzymeRules.needs_shadow(config) | ||
| C.dval | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
|
|
||
| # Check if A is overwritten and B is active (and thus required) | ||
| cache_A = ( | ||
| EnzymeRules.overwritten(config)[5] | ||
| && !(typeof(B) <: Const) | ||
| && !(typeof(C) <: Const) | ||
| ) ? copy(A.val) : nothing | ||
|
|
||
| cache_B = ( | ||
| EnzymeRules.overwritten(config)[6] | ||
| && !(typeof(A) <: Const) | ||
| && !(typeof(C) <: Const) | ||
| ) ? copy(B.val) : nothing | ||
|
|
||
| if !isa(α, Const) | ||
| cache_α = A.val * B.val | ||
| else | ||
| cache_α = nothing | ||
| end | ||
|
|
||
| cache = (cache_C, cache_A, cache_B, cache_α) | ||
|
|
||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfig, | ||
| func::Const{typeof(LinearAlgebra.mul!)}, | ||
| ::Type{RT}, cache, | ||
| C::Annotation{<:StridedVecOrMat}, | ||
| A::Annotation{<:SparseArrays.SparseMatrixCSCUnion}, | ||
| B::Annotation{<:StridedVecOrMat}, | ||
| α::Annotation{<:Number}, | ||
| β::Annotation{<:Number} | ||
| ) where {RT} | ||
|
|
||
| cache_C, cache_A, cache_B, cache_α = cache | ||
| Cval = !isnothing(cache_C) ? cache_C : C.val | ||
| Aval = !isnothing(cache_A) ? cache_A : A.val | ||
| Bval = !isnothing(cache_B) ? cache_B : B.val | ||
|
|
||
| N = EnzymeRules.width(config) | ||
| if !isa(C, Const) | ||
| dCs = C.dval | ||
| dBs = isa(B, Const) ? dCs : B.dval | ||
| dα = if !isa(α, Const) | ||
| if N == 1 | ||
| Enzyme._project(typeof(α.val), conj(LinearAlgebra.dot(C.dval, cache_α))) | ||
| else | ||
| ntuple(Val(N)) do i | ||
| Base.@_inline_meta | ||
| Enzyme._project(typeof(α.val), conj(LinearAlgebra.dot(C.dval[i], cache_α))) | ||
| end | ||
| end | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
| dβ = if !isa(β, Const) | ||
| if N == 1 | ||
| Enzyme._project(typeof(β.val), conj(LinearAlgebra.dot(C.dval, Cval))) | ||
| else | ||
| ntuple(Val(N)) do i | ||
| Base.@_inline_meta | ||
| Enzyme._project(typeof(β.val), conj(LinearAlgebra.dot(C.dval[i], Cval))) | ||
| end | ||
| end | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
| for i in 1:N | ||
| if !isa(A, Const) | ||
| # dA .+= α'dC*B' | ||
| # You need to be careful so that dA sparsity pattern does not change. Otherwise | ||
| # you will get incorrect gradients. So for now we do the slow and bad way of accumulating | ||
| dA = EnzymeRules.width(config) == 1 ? A.dval : A.dval[i] | ||
| dC = EnzymeRules.width(config) == 1 ? C.dval : C.dval[i] | ||
| # Now accumulate to preserve the correct sparsity pattern | ||
| I, J, _ = SparseArrays.findnz(dA) | ||
| for k in eachindex(I, J) | ||
| Ik, Jk = I[k], J[k] | ||
| # May need to widen if the eltype differ | ||
| tmp = zero(promote_type(eltype(dA), eltype(dC))) | ||
| for ti in axes(dC, 2) | ||
| tmp += dC[Ik, ti] * conj(Bval[Jk, ti]) | ||
| end | ||
| dA[Ik, Jk] += Enzyme._project(eltype(dA), conj(α.val) * tmp) | ||
| end | ||
| # mul!(dA, dCs, Bval', α.val, true) | ||
| end | ||
|
|
||
| if !isa(B, Const) | ||
| #dB .+= α*A'*dC | ||
| # Get the type of all arguments since we may need to | ||
| # project down to a smaller type during accumulation | ||
| if N == 1 | ||
| Targs = promote_type(eltype(Aval), eltype(dCs), typeof(α.val)) | ||
| Enzyme._muladdproject!(Targs, dBs, Aval', dCs, conj(α.val)) | ||
| else | ||
| Targs = promote_type(eltype(Aval[i]), eltype(dCs[i]), typeof(α.val)) | ||
| Enzyme._muladdproject!(Targs, dBs[i], Aval', dCs[i], conj(α.val)) | ||
| end | ||
| end | ||
| #dC = dC*conj(β.val) | ||
| if N == 1 | ||
| dCs .*= Enzyme._project(eltype(dCs), conj(β.val)) | ||
| else | ||
| dCs[i] .*= Enzyme._project(eltype(dCs[i]), conj(β.val)) | ||
| end | ||
| end | ||
| else | ||
| # C is constant so there is no gradient information to compute | ||
|
|
||
| dα = if !isa(α, Const) | ||
| if N == 1 | ||
| zero(α.val) | ||
| else | ||
| ntuple(Returns(zero(α.val)), Val(N)) | ||
| end | ||
| else | ||
| nothing | ||
| end | ||
|
|
||
|
|
||
| dβ = if !isa(β, Const) | ||
| if N == 1 | ||
| zero(β.val) | ||
| else | ||
| ntuple(Returns(zero(β.val)), Val(N)) | ||
| end | ||
| else | ||
| nothing | ||
| end | ||
| end | ||
|
|
||
| return (nothing, nothing, nothing, dα, dβ) | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately presently this cannot be moved into an extension (it will not get picked up from generated function shenanigans). the longer term soltn i need to figure out is how to handle this specific edge case of a ccall return, but I haven't gotten to so yet [and it only came up for sparsearrays]
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would need to perform an invoke in world (probably invole_in_world_total) with the world being the job.world