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
22 changes: 11 additions & 11 deletions ext/VectorInterfaceEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ using Enzyme
using Enzyme.EnzymeCore
using Enzyme.EnzymeCore: EnzymeRules

function project_add!(C, A, α)
TC = Base.promote_op(+, scalartype(A), scalartype(α))
return if !(TC <: Real) && scalartype(C) <: Real
add!(C, real(add!(zerovector(C, TC), A, α)))
else
add!(C, A, α)
end
end

"""
project_scalar(x::Number, dx::Number)

Expand Down Expand Up @@ -104,7 +113,7 @@ function EnzymeRules.reverse(
α::Annotation{<:Number},
) where {RT}
Aval, αval = cache
!isa(A, Const) && !isa(C, Const) && add!(A.dval, C.dval, conj(αval))
!isa(A, Const) && !isa(C, Const) && project_add!(A.dval, C.dval, conj(αval))
Δα = if !isa(α, Const) && !isa(C, Const)
project_scalar(α.val, inner(Aval, C.dval))
elseif !isa(α, Const)
Expand Down Expand Up @@ -186,7 +195,7 @@ function EnzymeRules.reverse(
else
nothing
end
!isa(A, Const) && !isa(C, Const) && add!(A.dval, C.dval, conj(αval))
!isa(A, Const) && !isa(C, Const) && project_add!(A.dval, C.dval, conj(αval))
!isa(C, Const) && scale!(C.dval, conj(βval))
return (nothing, nothing, Δα, Δβ)
end
Expand Down Expand Up @@ -215,15 +224,6 @@ function EnzymeRules.forward(
end
end

function project_add!(C, A, α)
TC = Base.promote_op(+, scalartype(A), scalartype(α))
return if !(TC <: Real) && scalartype(C) <: Real
add!(C, real(add!(zerovector(C, TC), A, α)))
else
add!(C, A, α)
end
end


function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
Expand Down
22 changes: 11 additions & 11 deletions ext/VectorInterfaceMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ For example, we might compute a complex `dx` but only require the real part.
project_scalar(x::Number, dx::Number) = oftype(x, dx)
project_scalar(x::Real, dx::Complex) = project_scalar(x, real(dx))

function project_add!(C, A, α)
TC = Base.promote_op(+, scalartype(A), scalartype(α))
return if !(TC <: Real) && scalartype(C) <: Real
add!(C, real(add!(zerovector(C, TC), A, α)))
else
add!(C, A, α)
end
end

_needs_tangent(x) = _needs_tangent(typeof(x))
_needs_tangent(::Type{T}) where {T <: Number} =
Mooncake.rdata_type(Mooncake.tangent_type(T)) !== NoRData
Expand Down Expand Up @@ -72,7 +81,7 @@ function Mooncake.rrule!!(::CoDual{typeof(scale!)}, C_ΔC::CoDual, A_ΔA::CoDual

function scale_pullback(::NoRData)
copy!(C, C_cache)
add!(ΔA, ΔC, conj(α))
project_add!(ΔA, ΔC, conj(α))
Δαr = _needs_tangent(α) ? project_scalar(α, inner(A, ΔC)) : NoRData()
zerovector!(ΔC)
return NoRData(), NoRData(), NoRData(), Δαr
Expand Down Expand Up @@ -114,7 +123,7 @@ function Mooncake.rrule!!(::CoDual{typeof(add!)}, C_ΔC::CoDual, A_ΔA::CoDual,

Δαr = _needs_tangent(α) ? project_scalar(α, inner(A, ΔC)) : NoRData()
Δβr = _needs_tangent(β) ? project_scalar(β, inner(C, ΔC)) : NoRData()
add!(ΔA, ΔC, conj(α))
project_add!(ΔA, ΔC, conj(α))
scale!(ΔC, conj(β))

return NoRData(), NoRData(), NoRData(), Δαr, Δβr
Expand All @@ -140,15 +149,6 @@ end
# inner
# -----

function project_add!(C, A, α)
TC = Base.promote_op(+, scalartype(A), scalartype(α))
return if !(TC <: Real) && scalartype(C) <: Real
add!(C, real(add!(zerovector(C, TC), A, α)))
else
add!(C, A, α)
end
end

@is_primitive DefaultCtx Tuple{typeof(inner), AbstractArray, AbstractArray}

function Mooncake.rrule!!(::CoDual{typeof(inner)}, A_ΔA::CoDual, B_ΔB::CoDual)
Expand Down
9 changes: 5 additions & 4 deletions test/enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ eltypes = (Float64, ComplexF64)
end
end

@testset "add ($T)" for T in eltypes
@testset "add ($Tx, $Ty)" for Ty in eltypes, Tx in eltypes
n = 12
atol = rtol = n * precision(T)
atol = rtol = n * max(precision(Tx), precision(Ty))
T = Base.promote_op(+, Tx, Ty)

# Vector
x = randn(T, n)
y = randn(T, n)
x = randn(Tx, n)
y = randn(Ty, n)
α = randn(T)
β = randn(T)
for Tα in (Const, Active), Tβ in (Const, Active)
Expand Down
10 changes: 5 additions & 5 deletions test/mooncake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ eltypes = (Float32, Float64, ComplexF64)
Mooncake.TestUtils.test_rule(rng, scale!!, my, mx, α; atol, rtol, is_primitive = false)
end

@testset "add pullbacks ($T)" for T in eltypes
@testset "add ($Tx, $Ty)" for Ty in eltypes, Tx in eltypes
n = 12
atol = rtol = n * precision(T)

atol = rtol = n * max(precision(Tx), precision(Ty))
T = Base.promote_op(+, Tx, Ty)
# Vector
x = randn(T, n)
y = randn(T, n)
x = randn(Tx, n)
y = randn(Ty, n)
α = randn(T)
β = randn(T)
Mooncake.TestUtils.test_rule(rng, add, y, x, α, β; atol, rtol, is_primitive = false)
Expand Down
Loading