Skip to content
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
119 changes: 119 additions & 0 deletions ext/TensorOperationsEnzymeExt/TensorOperationsEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Enzyme.EnzymeCore: EnzymeRules
@inline EnzymeRules.inactive_type(v::Type{<:CUDAAllocator}) = true
@inline EnzymeRules.inactive_type(v::Type{ManualAllocator}) = true
@inline EnzymeRules.inactive_type(v::Type{<:Index2Tuple}) = true
@inline EnzymeRules.inactive_type(v::Type{<:IndexTuple}) = true

function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
Expand Down Expand Up @@ -126,6 +127,46 @@ function EnzymeRules.reverse(
return nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end

function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(TensorOperations.tensorcontract!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
pA_dpA::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
B_dB::Annotation{<:AbstractArray{TB}},
pB_dpB::Annotation{<:Index2Tuple},
conjB_dconjB::Const{Bool},
pAB_dpAB::Annotation{<:Index2Tuple},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number}
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)

if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(α_dα, Const)
tensorcontract!(C_dC.dval, A_dA.val, pA, conjA, B_dB.val, pB, conjB, pAB, α_dα.dval, One(), ba...)
end
if !isa(A_dA, Const)
tensorcontract!(C_dC.dval, A_dA.dval, pA, conjA, B_dB.val, pB, conjB, pAB, α, One(), ba...)
end
if !isa(B_dB, Const)
tensorcontract!(C_dC.dval, A_dA.val, pA, conjA, B_dB.dval, pB, conjB, pAB, α, One(), ba...)
end
end
TensorOperations.tensorcontract!(C_dC.val, A_dA.val, pA, conjA, B_dB.val, pB, conjB, pAB, α, β, ba...)
return C_dC
end

function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
::Annotation{typeof(tensoradd!)},
Expand Down Expand Up @@ -198,6 +239,45 @@ function EnzymeRules.reverse(
return nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end

function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
::Annotation{typeof(tensoradd!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
pA_dpA::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
pA = pA_dpA.val
conjA = conjA_dconjA.val
α = α_dα.val
β = β_dβ.val
ba = map(ba_ -> getfield(ba_, :val), ba_dba)

# D = α * A + β *C

# dD = dα * A + α * dA + β dC + dβ * C

# dC′ = β dC + dβ * C
if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(A_dA, Const)
TensorOperations.tensoradd!(C_dC.dval, A_dA.dval, pA, conjA, α, One(), ba...)
end
if !isa(α_dα, Const)
TensorOperations.tensoradd!(C_dC.dval, A_dA.val, pA, conjA, α_dα.dval, One(), ba...)
end
end
TensorOperations.tensoradd!(C_dC.val, A_dA.val, pA, conjA, α, β, ba...)
return C_dC
end

function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
::Annotation{typeof(tensortrace!)},
Expand Down Expand Up @@ -273,4 +353,43 @@ function EnzymeRules.reverse(
return nothing, nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end

function EnzymeRules.forward(
config::EnzymeRules.RevConfigWidth{1},
::Annotation{typeof(tensortrace!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
p_dp::Annotation{<:Index2Tuple},
q_dq::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
p = p_dp.val
q = q_dq.val
conjA = conjA_dconjA.val
α = α_dα.val
β = β_dβ.val
ba = map(ba_ -> getfield(ba_, :val), ba_dba)

# dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC
# dC1 = dβ * C + β * dC
if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(α_dα, Const)
TensorOperations.tensortrace!(C_dC.dval, A_dA.val, p, q, conjA, α_dα.dval, One(), ba...)
end
if !isa(A_dA, Const)
TensorOperations.tensortrace!(C_dC.dval, A_dA.dval, p, q, conjA, α, One(), ba...)
end
end
# D = α * tr(A) + β * C
TensorOperations.tensortrace!(C_dC.val, A_dA.val, p, q, conjA, α, β, ba...)
return C_dC
end

end
119 changes: 115 additions & 4 deletions ext/TensorOperationsMooncakeExt/TensorOperationsMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using TensorOperations
using Mooncake, Mooncake.CRC
using TensorOperations: AbstractBackend, DefaultAllocator, CUDAAllocator, ManualAllocator
using TensorOperations: tensoralloc, tensoradd!, tensorcontract!, tensortrace!
using Mooncake: ReverseMode, DefaultCtx, CoDual, NoRData, arrayify, @zero_derivative, primal, tangent
using Mooncake: ReverseMode, DefaultCtx, Dual, CoDual, NoRData, arrayify, @zero_derivative, primal, tangent
using VectorInterface, TupleTools

Mooncake.tangent_type(::Type{Index2Tuple}) = Mooncake.NoTangent
Expand All @@ -29,7 +29,7 @@ Mooncake.tangent_type(::Type{ManualAllocator}) = Mooncake.NoTangent
Mooncake.@from_rrule Mooncake.DefaultCtx Tuple{typeof(TensorOperations.tensorfree!), Any}
Mooncake.@from_rrule Mooncake.DefaultCtx Tuple{typeof(TensorOperations.tensoralloc), Any, Any, Any, Any}

Mooncake.@is_primitive DefaultCtx ReverseMode Tuple{typeof(tensorcontract!), AbstractArray, AbstractArray, Index2Tuple, Bool, AbstractArray, Index2Tuple, Bool, Index2Tuple, Number, Number, Vararg{Any}}
Mooncake.@is_primitive DefaultCtx Tuple{typeof(tensorcontract!), AbstractArray, AbstractArray, Index2Tuple, Bool, AbstractArray, Index2Tuple, Bool, Index2Tuple, Number, Number, Vararg{Any}}
function Mooncake.rrule!!(
::CoDual{typeof(tensorcontract!)},
C_dC::CoDual{<:AbstractArray{TC}},
Expand Down Expand Up @@ -67,7 +67,47 @@ function Mooncake.rrule!!(
return C_dC, contract_pb
end

Mooncake.@is_primitive DefaultCtx ReverseMode Tuple{typeof(tensoradd!), AbstractArray, AbstractArray, Index2Tuple, Bool, Number, Number, Vararg{Any}}
function Mooncake.frule!!(
::Dual{typeof(tensorcontract!)},
C_dC::Dual{<:AbstractArray{TC}},
A_dA::Dual{<:AbstractArray{TA}},
pA_dpA::Dual{<:Index2Tuple},
conjA_dconjA::Dual{Bool},
B_dB::Dual{<:AbstractArray{TB}},
pB_dpB::Dual{<:Index2Tuple},
conjB_dconjB::Dual{Bool},
pAB_dpAB::Dual{<:Index2Tuple},
α_dα::Dual{Tα},
β_dβ::Dual{Tβ},
ba_dba::Dual...,
) where {Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number}
C, dC = arrayify(C_dC)
A, dA = arrayify(A_dA)
B, dB = arrayify(B_dB)
pA = primal(pA_dpA)
pB = primal(pB_dpB)
pAB = primal(pAB_dpAB)
conjA = primal(conjA_dconjA)
conjB = primal(conjB_dconjB)
α, dα = Mooncake.extract(α_dα)
β, dβ = Mooncake.extract(β_dβ)
ba = primal.(ba_dba)

# ΔC′ = ΔC*β + C*Δβ + A*B*Δα + ΔA*B*α + A*ΔB*α
scale!(dC, β)
if !isa(dβ, Mooncake.NoTangent)
add!(dC, C, dβ)
end
if !isa(dα, Mooncake.NoTangent)
tensorcontract!(dC, A, pA, conjA, B, pB, conjB, pAB, dα, One(), ba...)
end
tensorcontract!(dC, dA, pA, conjA, B, pB, conjB, pAB, α, One(), ba...)
tensorcontract!(dC, A, pA, conjA, dB, pB, conjB, pAB, α, One(), ba...)
TensorOperations.tensorcontract!(C, A, pA, conjA, B, pB, conjB, pAB, α, β, ba...)
return C_dC
end

Mooncake.@is_primitive DefaultCtx Tuple{typeof(tensoradd!), AbstractArray, AbstractArray, Index2Tuple, Bool, Number, Number, Vararg{Any}}
function Mooncake.rrule!!(
::CoDual{typeof(tensoradd!)},
C_dC::CoDual{<:AbstractArray{TC}},
Expand Down Expand Up @@ -97,7 +137,43 @@ function Mooncake.rrule!!(
return C_dC, add_pb
end

Mooncake.@is_primitive DefaultCtx ReverseMode Tuple{typeof(tensortrace!), AbstractArray, AbstractArray, Index2Tuple, Index2Tuple, Bool, Number, Number, Vararg{Any}}
function Mooncake.frule!!(
::Dual{typeof(tensoradd!)},
C_dC::Dual{<:AbstractArray{TC}},
A_dA::Dual{<:AbstractArray{TA}},
pA_dpA::Dual{<:Index2Tuple},
conjA_dconjA::Dual{Bool},
α_dα::Dual{Tα},
β_dβ::Dual{Tβ},
ba_dba::Dual...,
) where {Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
C, dC = arrayify(C_dC)
A, dA = arrayify(A_dA)
pA = primal(pA_dpA)
conjA = primal(conjA_dconjA)
α = primal(α_dα)
dα = tangent(α_dα)
β = primal(β_dβ)
dβ = tangent(β_dβ)
ba = primal.(ba_dba)
# D = α * A + β *C

# dD = dα * A + α * dA + β dC + dβ * C

# dC′ = β dC + dβ * C
scale!(dC, β)
if !isa(dβ, Mooncake.NoTangent)
add!(dC, C, dβ)
end
TensorOperations.tensoradd!(dC, dA, pA, conjA, α, One(), ba...)
if !isa(dα, Mooncake.NoTangent)
TensorOperations.tensoradd!(dC, A, pA, conjA, dα, One(), ba...)
end
TensorOperations.tensoradd!(C, A, pA, conjA, α, β, ba...)
return C_dC
end

Mooncake.@is_primitive DefaultCtx Tuple{typeof(tensortrace!), AbstractArray, AbstractArray, Index2Tuple, Index2Tuple, Bool, Number, Number, Vararg{Any}}
function Mooncake.rrule!!(
::CoDual{typeof(tensortrace!)},
C_dC::CoDual{<:AbstractArray{TC}},
Expand Down Expand Up @@ -129,4 +205,39 @@ function Mooncake.rrule!!(
return C_dC, trace_pb
end

function Mooncake.frule!!(
::Dual{typeof(tensortrace!)},
C_dC::Dual{<:AbstractArray{TC}},
A_dA::Dual{<:AbstractArray{TA}},
p_dp::Dual{<:Index2Tuple},
q_dq::Dual{<:Index2Tuple},
conjA_dconjA::Dual{Bool},
α_dα::Dual{Tα},
β_dβ::Dual{Tβ},
ba_dba::Dual...,
) where {Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
C, dC = arrayify(C_dC)
A, dA = arrayify(A_dA)
p = primal(p_dp)
q = primal(q_dq)
conjA = primal(conjA_dconjA)
α = primal(α_dα)
dα = tangent(α_dα)
β = primal(β_dβ)
dβ = tangent(β_dβ)
ba = primal.(ba_dba)
# dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC
# dC1 = dβ * C + β * dC
scale!(dC, β)
if !isa(dβ, Mooncake.NoTangent)
add!(dC, C, dβ)
end
if !isa(dα, Mooncake.NoTangent)
TensorOperations.tensortrace!(dC, A, p, q, conjA, dα, One(), ba...)
end
TensorOperations.tensortrace!(dC, dA, p, q, conjA, α, One(), ba...)
TensorOperations.tensortrace!(C, A, p, q, conjA, α, β, ba...)
return C_dC
end

end
Loading
Loading