-
Notifications
You must be signed in to change notification settings - Fork 69
Add Enzyme rules #243
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
Add Enzyme rules #243
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bbda095
Add Enzyme rules
kshyatt a3ebb5d
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 03851f9
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 5d7419f
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 42f4b5c
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 48186e0
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 8fad856
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt db094a9
Fix cache and simplify tests
kshyatt 83ac936
Re-enable all tests
kshyatt 100bc25
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt eb3754b
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 3a6d6ae
Update ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
kshyatt 12cf697
Remove irrelevant Mooncake test
kshyatt 36d0a3d
Merge branch 'master' into ksh/enzyme
kshyatt 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
187 changes: 187 additions & 0 deletions
187
ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
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,187 @@ | ||
| module TensorOperationsEnzymeExt | ||
|
|
||
| using TensorOperations | ||
| using TensorOperations: AbstractBackend, DefaultAllocator, CUDAAllocator, ManualAllocator | ||
| using VectorInterface | ||
| using TupleTools | ||
| using Enzyme, ChainRulesCore | ||
| using Enzyme.EnzymeCore | ||
| using Enzyme.EnzymeCore: EnzymeRules | ||
|
|
||
| @inline EnzymeRules.inactive(::typeof(TensorOperations.tensorfree!), ::Any) = true | ||
| Enzyme.@import_rrule(typeof(TensorOperations.tensoralloc), Any, Any, Any, Any) | ||
|
|
||
| @inline EnzymeRules.inactive_type(v::Type{<:AbstractBackend}) = true | ||
| @inline EnzymeRules.inactive_type(v::Type{DefaultAllocator}) = true | ||
| @inline EnzymeRules.inactive_type(v::Type{<:CUDAAllocator}) = true | ||
| @inline EnzymeRules.inactive_type(v::Type{ManualAllocator}) = true | ||
| @inline EnzymeRules.inactive_type(v::Type{<:Index2Tuple}) = true | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorOperations.tensorcontract!)}, | ||
| ::Type{RT}, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| pA_dpA::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| B_dB::Annotation{<:AbstractArray{TB}}, | ||
| pB_dpB::Const{<:Index2Tuple}, | ||
| conjB_dconjB::Const{Bool}, | ||
| pAB_dpAB::Const{<:Index2Tuple}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number} | ||
| # form caches if needed | ||
| cache_A = EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing | ||
| cache_B = EnzymeRules.overwritten(config)[6] ? copy(B_dB.val) : nothing | ||
| cache_C = !iszero(β_dβ.val) ? copy(C_dC.val) : C_dC.val | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| TensorOperations.tensorcontract!(C_dC.val, A_dA.val, pA_dpA.val, conjA_dconjA.val, B_dB.val, pB_dpB.val, conjB_dconjB.val, pAB_dpAB.val, α_dα.val, β_dβ.val, ba...) | ||
| primal = EnzymeRules.needs_primal(config) ? C_dC.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_B, cache_C)) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof(TensorOperations.tensorcontract!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| pA_dpA::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| B_dB::Annotation{<:AbstractArray{TB}}, | ||
| pB_dpB::Const{<:Index2Tuple}, | ||
| conjB_dconjB::Const{Bool}, | ||
| pAB_dpAB::Const{<:Index2Tuple}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number} | ||
| cache_A, cache_B, cache_C = cache | ||
| Aval = something(cache_A, A_dA.val) | ||
| Bval = something(cache_B, B_dB.val) | ||
| Cval = cache_C | ||
| # good way to check that we don't use it accidentally when we should not be needing it? | ||
| dC = C_dC.dval | ||
| dA = A_dA.dval | ||
| dB = B_dB.dval | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| α = α_dα.val | ||
| β = β_dβ.val | ||
| pA, pB, pAB, conjA, conjB = getfield.((pA_dpA, pB_dpB, pAB_dpAB, conjA_dconjA, conjB_dconjB), :val) | ||
| dC, dA, dB, dα, dβ = TensorOperations.tensorcontract_pullback!(dC, dA, dB, Cval, Aval, pA, conjA, Bval, pB, conjB, pAB, α, β, ba...) | ||
| return nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... | ||
| end | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| ::Annotation{typeof(tensoradd!)}, | ||
| ::Type{RT}, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| pA_dpA::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} | ||
| # form caches if needed | ||
| cache_A = EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing | ||
| cache_C = !iszero(β_dβ.val) ? copy(C_dC.val) : C_dC.val | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| α = α_dα.val | ||
| β = β_dβ.val | ||
| conjA = conjA_dconjA.val | ||
| TensorOperations.tensoradd!(C_dC.val, A_dA.val, pA_dpA.val, conjA, α, β, ba...) | ||
| primal = EnzymeRules.needs_primal(config) ? C_dC.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_C)) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| ::Annotation{typeof(tensoradd!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| pA_dpA::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} | ||
| cache_A, cache_C = cache | ||
| Aval = something(cache_A, A_dA.val) | ||
| Cval = cache_C | ||
| pA = pA_dpA.val | ||
| conjA = conjA_dconjA.val | ||
| α = α_dα.val | ||
| β = β_dβ.val | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| dC = C_dC.dval | ||
| dA = A_dA.dval | ||
| dC, dA, dα, dβ = TensorOperations.tensoradd_pullback!(dC, dA, Cval, Aval, pA, conjA, α, β, ba...) | ||
| return nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... | ||
| end | ||
|
|
||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| ::Annotation{typeof(tensortrace!)}, | ||
| ::Type{RT}, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| p_dp::Const{<:Index2Tuple}, | ||
| q_dq::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} | ||
| # form caches if needed | ||
| cache_A = EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing | ||
| cache_C = !iszero(β_dβ.val) ? copy(C_dC.val) : C_dC.val | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| α = α_dα.val | ||
| β = β_dβ.val | ||
| conjA = conjA_dconjA.val | ||
| TensorOperations.tensortrace!(C_dC.val, A_dA.val, p_dp.val, q_dq.val, conjA, α, β, ba...) | ||
| primal = EnzymeRules.needs_primal(config) ? C_dC.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_C)) | ||
| end | ||
|
|
||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| ::Annotation{typeof(tensortrace!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| C_dC::Annotation{<:AbstractArray{TC}}, | ||
| A_dA::Annotation{<:AbstractArray{TA}}, | ||
| p_dp::Const{<:Index2Tuple}, | ||
| q_dq::Const{<:Index2Tuple}, | ||
| conjA_dconjA::Const{Bool}, | ||
| α_dα::Annotation{Tα}, | ||
| β_dβ::Annotation{Tβ}, | ||
| ba_dba::Const..., | ||
| ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} | ||
| cache_A, cache_C = cache | ||
| Aval = something(cache_A, A_dA.val) | ||
| Cval = cache_C | ||
| p = p_dp.val | ||
| q = q_dq.val | ||
| conjA = conjA_dconjA.val | ||
| α = α_dα.val | ||
| β = β_dβ.val | ||
| ba = map(ba_ -> getfield(ba_, :val), ba_dba) | ||
| dC = C_dC.dval | ||
| dA = A_dA.dval | ||
| dC, dA, dα, dβ = TensorOperations.tensortrace_pullback!(dC, dA, Cval, Aval, p, q, conjA, α, β, ba...) | ||
| return nothing, nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using TensorOperations, VectorInterface | ||
| using Enzyme, ChainRulesCore, EnzymeTestUtils | ||
|
|
||
| @testset "tensorcontract!" begin | ||
| pAB = ((3, 2, 4, 1), ()) | ||
| pA = ((2, 4, 5), (1, 3)) | ||
| pB = ((2, 1), (3,)) | ||
| @testset "($T₁, $T₂)" for (T₁, T₂) in ( | ||
| (Float64, Float64), | ||
| (Float32, Float64), | ||
| (ComplexF64, ComplexF64), | ||
| (Float64, ComplexF64), | ||
| (ComplexF64, Float64), | ||
| ) | ||
| T = promote_type(T₁, T₂) | ||
| atol = max(precision(T₁), precision(T₂)) | ||
| rtol = max(precision(T₁), precision(T₂)) | ||
|
|
||
| A = rand(T₁, (2, 3, 4, 2, 5)) | ||
| B = rand(T₂, (4, 2, 3)) | ||
| C = rand(T, (5, 2, 3, 3)) | ||
| zero_αβs = ((Zero(), Zero()), (randn(T), Zero()), (Zero(), randn(T))) | ||
| αβs = (T == T₁ == T₂ == Float64) ? vcat(zero_αβs..., (randn(T), randn(T))) : ((randn(T), randn(T)),) | ||
| # test zeros only once to avoid wasteful tests | ||
| @testset for (α, β) in αβs | ||
| Tα = α === Zero() ? Const : Active | ||
| Tβ = β === Zero() ? Const : Active | ||
| test_reverse(tensorcontract!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (B, Duplicated), (pB, Const), (false, Const), (pAB, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
| test_reverse(tensorcontract!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (B, Duplicated), (pB, Const), (true, Const), (pAB, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
| test_reverse(tensorcontract!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (true, Const), (B, Duplicated), (pB, Const), (true, Const), (pAB, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
|
|
||
| test_reverse(tensorcontract!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (B, Duplicated), (pB, Const), (false, Const), (pAB, Const), (α, Tα), (β, Tβ), (StridedBLAS(), Const); atol, rtol) | ||
| test_reverse(tensorcontract!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (true, Const), (B, Duplicated), (pB, Const), (true, Const), (pAB, Const), (α, Tα), (β, Tβ), (StridedNative(), Const); atol, rtol) | ||
|
|
||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "tensoradd!" begin | ||
| pA = ((2, 1, 4, 3, 5), ()) | ||
| @testset "($T₁, $T₂)" for (T₁, T₂) in ( | ||
| (Float64, Float64), | ||
| (Float32, Float64), | ||
| (ComplexF64, ComplexF64), | ||
| (Float64, ComplexF64), | ||
| ) | ||
| T = promote_type(T₁, T₂) | ||
| atol = max(precision(T₁), precision(T₂)) | ||
| rtol = max(precision(T₁), precision(T₂)) | ||
|
|
||
| A = rand(T₁, (2, 3, 4, 2, 1)) | ||
| C = rand(T₂, size.(Ref(A), pA[1])) | ||
| zero_αβs = ((Zero(), Zero()), (randn(T), Zero()), (Zero(), randn(T))) | ||
| αβs = (T == T₁ == T₂ == Float64) ? vcat(zero_αβs..., (randn(T), randn(T))) : ((randn(T), randn(T)),) | ||
| # test zeros only once to avoid wasteful tests | ||
| @testset for (α, β) in αβs | ||
| Tα = α === Zero() ? Const : Active | ||
| Tβ = β === Zero() ? Const : Active | ||
| test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
| test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (true, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
|
|
||
| test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (false, Const), (α, Tα), (β, Tβ), (StridedBLAS(), Const); atol, rtol) | ||
| test_reverse(tensoradd!, Duplicated, (C, Duplicated), (A, Duplicated), (pA, Const), (true, Const), (α, Tα), (β, Tβ), (StridedNative(), Const); atol, rtol) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "tensortrace!" begin | ||
| p = ((3, 5, 2), ()) | ||
| q = ((1,), (4,)) | ||
| @testset "($T₁, $T₂)" for (T₁, T₂) in | ||
| ( | ||
| (Float64, Float64), | ||
| (Float32, Float64), | ||
| (ComplexF64, ComplexF64), | ||
| (Float64, ComplexF64), | ||
| ) | ||
| T = promote_type(T₁, T₂) | ||
| atol = max(precision(T₁), precision(T₂)) | ||
| rtol = max(precision(T₁), precision(T₂)) | ||
|
|
||
| A = rand(T₁, (2, 3, 4, 2, 5)) | ||
| C = rand(T₂, size.(Ref(A), p[1])) | ||
| zero_αβs = ((Zero(), Zero()), (randn(T), Zero()), (Zero(), randn(T))) | ||
| αβs = (T == T₁ == T₂ == Float64) ? vcat(zero_αβs..., (randn(T), randn(T))) : ((randn(T), randn(T)),) | ||
| # test zeros only once to avoid wasteful tests | ||
| @testset for (α, β) in αβs | ||
| Tα = α === Zero() ? Const : Active | ||
| Tβ = β === Zero() ? Const : Active | ||
| test_reverse(tensortrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (false, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
| test_reverse(tensortrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (true, Const), (α, Tα), (β, Tβ); atol, rtol) | ||
|
|
||
| test_reverse(tensortrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (true, Const), (α, Tα), (β, Tβ), (StridedBLAS(), Const); atol, rtol) | ||
| test_reverse(tensortrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (false, Const), (α, Tα), (β, Tβ), (StridedNative(), Const); atol, rtol) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "tensorscalar ($T)" for T in (Float32, Float64, ComplexF64) | ||
| atol = precision(T) | ||
| rtol = precision(T) | ||
|
|
||
| C = Array{T, 0}(undef, ()) | ||
| fill!(C, rand(T)) | ||
| test_reverse(tensorscalar, Active, (C, Duplicated); atol, rtol) | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.